Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -11,8 +11,6 @@ from git import Repo
|
|
11 |
from datetime import datetime
|
12 |
import base64
|
13 |
import json
|
14 |
-
import random
|
15 |
-
import string # π² For generating random IDs
|
16 |
|
17 |
# π Welcome to our fun-filled Cosmos DB and GitHub Integration app!
|
18 |
st.set_page_config(layout="wide")
|
@@ -118,10 +116,10 @@ def delete_record(container, name, id):
|
|
118 |
except Exception as e:
|
119 |
return False, f"An unexpected error occurred: {traceback.format_exc()} π±"
|
120 |
|
121 |
-
#
|
122 |
-
def
|
123 |
-
#
|
124 |
-
return
|
125 |
|
126 |
# π¦ Function to archive current container
|
127 |
def archive_current_container(database_name, container_name, client):
|
@@ -237,7 +235,7 @@ def main():
|
|
237 |
# ποΈ Show as Markdown
|
238 |
items = get_documents(container)
|
239 |
for item in items:
|
240 |
-
st.markdown(f"###
|
241 |
content = item.get('content', '')
|
242 |
if isinstance(content, dict) or isinstance(content, list):
|
243 |
content = json.dumps(content, indent=2)
|
@@ -246,13 +244,20 @@ def main():
|
|
246 |
# π» Show as Code Editor
|
247 |
items = get_documents(container)
|
248 |
for item in items:
|
|
|
249 |
st.code(json.dumps(item, indent=2), language='python')
|
250 |
elif selected_view == 'Show as Edit and Save':
|
251 |
# βοΈ Show as Edit and Save
|
252 |
-
|
253 |
-
|
|
|
|
|
|
|
|
|
|
|
254 |
try:
|
255 |
updated_doc = json.loads(doc_str)
|
|
|
256 |
success, message = update_record(container, updated_doc)
|
257 |
if success:
|
258 |
st.success(message)
|
@@ -264,8 +269,8 @@ def main():
|
|
264 |
# 𧬠Clone Document
|
265 |
if st.button("π Clone Document"):
|
266 |
cloned_doc = selected_doc.copy()
|
267 |
-
# Generate a unique
|
268 |
-
cloned_doc['id'] =
|
269 |
success, message = insert_record(container, cloned_doc)
|
270 |
if success:
|
271 |
st.success(f"Document cloned with new id: {cloned_doc['id']} π")
|
@@ -273,12 +278,13 @@ def main():
|
|
273 |
st.error(message)
|
274 |
elif selected_view == 'New Record':
|
275 |
# π New Record
|
276 |
-
|
|
|
|
|
277 |
if st.button("β Create New Document"):
|
278 |
try:
|
279 |
new_doc = json.loads(new_doc_str)
|
280 |
-
|
281 |
-
new_doc['id'] = generate_short_id()
|
282 |
success, message = insert_record(container, new_doc)
|
283 |
if success:
|
284 |
st.success(f"New document created with id: {new_doc['id']} π")
|
@@ -292,8 +298,11 @@ def main():
|
|
292 |
# π Main content area
|
293 |
st.subheader(f"π Container: {st.session_state.selected_container}")
|
294 |
if st.session_state.selected_container:
|
295 |
-
|
296 |
-
|
|
|
|
|
|
|
297 |
|
298 |
# π GitHub section
|
299 |
st.subheader("π GitHub Operations")
|
@@ -356,6 +365,3 @@ def main():
|
|
356 |
|
357 |
if __name__ == "__main__":
|
358 |
main()
|
359 |
-
|
360 |
-
|
361 |
-
|
|
|
11 |
from datetime import datetime
|
12 |
import base64
|
13 |
import json
|
|
|
|
|
14 |
|
15 |
# π Welcome to our fun-filled Cosmos DB and GitHub Integration app!
|
16 |
st.set_page_config(layout="wide")
|
|
|
116 |
except Exception as e:
|
117 |
return False, f"An unexpected error occurred: {traceback.format_exc()} π±"
|
118 |
|
119 |
+
# π Function to generate a unique ID based on timestamp
|
120 |
+
def generate_timestamp_id():
|
121 |
+
# β° Time waits for no one, but IDs can!
|
122 |
+
return datetime.now().strftime('%Y%m%d%H%M%S%f')
|
123 |
|
124 |
# π¦ Function to archive current container
|
125 |
def archive_current_container(database_name, container_name, client):
|
|
|
235 |
# ποΈ Show as Markdown
|
236 |
items = get_documents(container)
|
237 |
for item in items:
|
238 |
+
st.markdown(f"### **π ID: {item.get('id', 'Unknown')}**")
|
239 |
content = item.get('content', '')
|
240 |
if isinstance(content, dict) or isinstance(content, list):
|
241 |
content = json.dumps(content, indent=2)
|
|
|
244 |
# π» Show as Code Editor
|
245 |
items = get_documents(container)
|
246 |
for item in items:
|
247 |
+
st.markdown(f"### **π ID: {item.get('id', 'Unknown')}**")
|
248 |
st.code(json.dumps(item, indent=2), language='python')
|
249 |
elif selected_view == 'Show as Edit and Save':
|
250 |
# βοΈ Show as Edit and Save
|
251 |
+
st.markdown("#### Edit the document fields below:")
|
252 |
+
editable_id = st.text_input("ID", value=selected_doc.get('id', ''), key='edit_id')
|
253 |
+
# Remove 'id' from the document for editing other fields
|
254 |
+
editable_doc = selected_doc.copy()
|
255 |
+
editable_doc.pop('id', None)
|
256 |
+
doc_str = st.text_area("Document Content (in JSON format)", value=json.dumps(editable_doc, indent=2), height=300)
|
257 |
+
if st.button("πΎ Save Changes"):
|
258 |
try:
|
259 |
updated_doc = json.loads(doc_str)
|
260 |
+
updated_doc['id'] = editable_id # Include the possibly edited ID
|
261 |
success, message = update_record(container, updated_doc)
|
262 |
if success:
|
263 |
st.success(message)
|
|
|
269 |
# 𧬠Clone Document
|
270 |
if st.button("π Clone Document"):
|
271 |
cloned_doc = selected_doc.copy()
|
272 |
+
# Generate a unique ID based on timestamp
|
273 |
+
cloned_doc['id'] = generate_timestamp_id()
|
274 |
success, message = insert_record(container, cloned_doc)
|
275 |
if success:
|
276 |
st.success(f"Document cloned with new id: {cloned_doc['id']} π")
|
|
|
278 |
st.error(message)
|
279 |
elif selected_view == 'New Record':
|
280 |
# π New Record
|
281 |
+
st.markdown("#### Create a new document:")
|
282 |
+
new_id = st.text_input("ID", value=generate_timestamp_id(), key='new_id')
|
283 |
+
new_doc_str = st.text_area("Document Content (in JSON format)", value='{}', height=300)
|
284 |
if st.button("β Create New Document"):
|
285 |
try:
|
286 |
new_doc = json.loads(new_doc_str)
|
287 |
+
new_doc['id'] = new_id # Use the provided ID
|
|
|
288 |
success, message = insert_record(container, new_doc)
|
289 |
if success:
|
290 |
st.success(f"New document created with id: {new_doc['id']} π")
|
|
|
298 |
# π Main content area
|
299 |
st.subheader(f"π Container: {st.session_state.selected_container}")
|
300 |
if st.session_state.selected_container:
|
301 |
+
if documents:
|
302 |
+
df = pd.DataFrame(documents)
|
303 |
+
st.dataframe(df)
|
304 |
+
else:
|
305 |
+
st.info("No documents to display. π§")
|
306 |
|
307 |
# π GitHub section
|
308 |
st.subheader("π GitHub Operations")
|
|
|
365 |
|
366 |
if __name__ == "__main__":
|
367 |
main()
|
|
|
|
|
|