awacke1 commited on
Commit
748e3e8
β€’
1 Parent(s): 7498882

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -19
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
- # 🎲 Function to generate a short, unique alphanumeric ID
122
- def generate_short_id(length=8):
123
- # πŸ§™β€β™‚οΈ Generating a short, unique ID!
124
- return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
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"### πŸ†” ID: {item.get('id', 'Unknown')}")
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
- doc_str = st.text_area("Edit Document", value=json.dumps(selected_doc, indent=2), height=300)
253
- if st.button("πŸ’Ύ Save"):
 
 
 
 
 
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, short ID for the cloned document
268
- cloned_doc['id'] = generate_short_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
- new_doc_str = st.text_area("New Document", value='{}', height=300)
 
 
277
  if st.button("βž• Create New Document"):
278
  try:
279
  new_doc = json.loads(new_doc_str)
280
- if 'id' not in new_doc:
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
- df = pd.DataFrame(documents)
296
- st.dataframe(df)
 
 
 
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()