awacke1 commited on
Commit
0d3a812
โ€ข
1 Parent(s): b651660

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -36
app.py CHANGED
@@ -811,56 +811,39 @@ def main():
811
  st.markdown("#### Clone a document:")
812
 
813
  for idx, doc in enumerate(documents_to_display):
814
- st.markdown(f"##### Document ID: {doc.get('id', '')}")
815
 
816
  if st.button("๐Ÿ“„ Clone Document", key=f'clone_button_{idx}'):
817
- # Create copy of original document
818
- cloned_doc = doc.copy()
 
819
 
820
- # Generate new unique IDs
821
- new_id = generate_unique_id()
822
- new_name = generate_unique_id()
 
 
 
823
 
824
- # Update ID and name fields at root level
825
- cloned_doc['id'] = new_id
826
- if 'name' in cloned_doc:
827
- cloned_doc['name'] = new_name
828
-
829
- # Update nested ID and name fields if they exist
830
- def update_nested_ids(obj):
831
- if isinstance(obj, dict):
832
- if 'id' in obj:
833
- obj['id'] = new_id
834
- if 'name' in obj:
835
- obj['name'] = new_name
836
- for value in obj.values():
837
- update_nested_ids(value)
838
- elif isinstance(obj, list):
839
- for item in obj:
840
- update_nested_ids(item)
841
-
842
- update_nested_ids(cloned_doc)
843
-
844
- # Display editable JSON
845
- edited_doc_str = st.text_area(
846
- "Edit cloned document before saving:",
847
  value=json.dumps(cloned_doc, indent=2),
848
- height=300
 
849
  )
850
 
851
- if st.button("๐Ÿ’พ Save Cloned Document"):
852
  try:
853
- final_doc = json.loads(edited_doc_str)
854
  success, message = insert_record(container, final_doc)
855
-
856
  if success:
857
- st.success(f"Cloned document saved successfully with ID: {final_doc['id']}")
858
  st.rerun()
859
  else:
860
- st.error(f"Failed to save clone: {message}")
861
  except json.JSONDecodeError as e:
862
  st.error(f"Invalid JSON format: {str(e)}")
863
-
864
 
865
  elif selected_view == 'New Record':
866
  # ๐Ÿ†• New Record
 
811
  st.markdown("#### Clone a document:")
812
 
813
  for idx, doc in enumerate(documents_to_display):
814
+ st.markdown(f"##### Original Document ID: {doc.get('id', '')}")
815
 
816
  if st.button("๐Ÿ“„ Clone Document", key=f'clone_button_{idx}'):
817
+ # Generate new unique identifiers
818
+ new_id = str(uuid.uuid4())
819
+ new_name = f"Clone_{str(uuid.uuid4())[:8]}"
820
 
821
+ # Create new document with unique ID and name
822
+ cloned_doc = {
823
+ 'id': new_id,
824
+ 'name': new_name,
825
+ **{k: v for k, v in doc.items() if k not in ['id', 'name']}
826
+ }
827
 
828
+ # Show editable preview
829
+ edited_doc = st.text_area(
830
+ "Edit cloned document:",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
831
  value=json.dumps(cloned_doc, indent=2),
832
+ height=300,
833
+ key=f'edit_clone_{idx}'
834
  )
835
 
836
+ if st.button("๐Ÿ’พ Save Clone", key=f'save_clone_{idx}'):
837
  try:
838
+ final_doc = json.loads(edited_doc)
839
  success, message = insert_record(container, final_doc)
 
840
  if success:
841
+ st.success(f"Cloned document saved with ID: {new_id}")
842
  st.rerun()
843
  else:
844
+ st.error(message)
845
  except json.JSONDecodeError as e:
846
  st.error(f"Invalid JSON format: {str(e)}")
 
847
 
848
  elif selected_view == 'New Record':
849
  # ๐Ÿ†• New Record