awacke1 commited on
Commit
5ecf9e9
Β·
verified Β·
1 Parent(s): 22d0d7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -77
app.py CHANGED
@@ -831,90 +831,90 @@ def main():
831
  # Button to initiate cloning of the document
832
  if st.button("πŸ“„ Clone Document", key=f'clone_button_{idx}'):
833
  with st.spinner("Cloning document..."):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
834
 
835
- try:
836
- existing_doc = container.read_item(item=clone_id, partition_key=clone_id)
837
- new_doc = existing_doc.copy()
838
- new_doc['id'] = generate_unique_id() # Generate new unique ID with timestamp
839
- new_doc['name'] = generate_unique_id() # Generate new unique ID with timestamp
840
- new_doc['createdAt'] = datetime.utcnow().isoformat() # Update the creation time
841
- new_doc['_rid'] = None # Reset _rid or any system-managed fields
842
- new_doc['_self'] = None
843
- new_doc['_etag'] = None
844
- new_doc['_attachments'] = None
845
- new_doc['_ts'] = None # Reset timestamp to be updated by Cosmos DB automatically
846
-
847
- # Insert the cloned document
848
- response = container.create_item(body=new_doc)
849
- st.success(f"Cloned document saved successfully with ID: {new_doc['id']} πŸŽ‰")
850
 
851
- # Refresh the documents in session state
 
 
 
852
  st.session_state.documents = list(container.query_items(
853
  query="SELECT * FROM c ORDER BY c._ts DESC",
854
  enable_cross_partition_query=True
855
- ))
856
-
857
- except exceptions.CosmosResourceNotFoundError:
858
- st.error(f"Document with ID {clone_id} not found for cloning.")
859
- except exceptions.CosmosHttpResponseError as e:
860
- st.error(f"HTTP error occurred: {str(e)} 🚨")
861
- except Exception as e:
862
- st.error(f"An unexpected error occurred: {str(e)} 😱")
863
 
 
 
 
 
 
 
 
 
 
 
 
 
 
864
 
865
- #try:
866
- # Copy the document and reset system-managed fields
867
- cloned_doc = doc.copy()
868
- cloned_doc['id'] = generate_unique_id() # Generate new unique ID
869
- cloned_doc['name'] = generate_unique_id() # Generate new unique ID
870
- cloned_doc['createdAt'] = datetime.utcnow().isoformat() # Set new creation time
871
- cloned_doc['_rid'] = None # Clear system-managed fields
872
- cloned_doc['_self'] = None
873
- cloned_doc['_etag'] = None
874
- cloned_doc['_attachments'] = None
875
- cloned_doc['_ts'] = None
876
-
877
- # Save the cloned document
878
-
879
- response = container.create_item(body=cloned_doc)
880
- st.success(f"Cloned document saved successfully with ID: {cloned_doc['id']} πŸŽ‰")
881
-
882
- # Refresh session state documents to show the new cloned document
883
- st.session_state.documents = list(container.query_items(
884
- query="SELECT * FROM c ORDER BY c._ts DESC",
885
- enable_cross_partition_query=True
886
- ))
887
-
888
- #except exceptions.CosmosHttpResponseError as e:
889
- # st.error(f"HTTP error occurred: {str(e)} 🚨")
890
- #except Exception as e:
891
- # st.error(f"An unexpected error occurred: {str(e)} 😱")
892
-
893
- # If clone mode is activated, allow user to edit the cloned document
894
- if st.session_state.get('clone_mode', False):
895
- st.markdown("#### Edit Cloned Document and Name Your Clone:")
896
- cloned_doc_str = st.text_area(
897
- "Cloned Document Content (in JSON format) - Update name before saving!",
898
- value=st.session_state.cloned_doc_str,
899
- height=300
900
- )
901
-
902
- # Save the edited cloned document
903
- if st.button("πŸ’Ύ Save Cloned Document"):
904
- try:
905
- new_doc = json.loads(cloned_doc_str)
906
- success, message = insert_record(container, new_doc)
907
- if success:
908
- st.success(f"Cloned document saved with id: {new_doc['id']} πŸŽ‰")
909
- st.session_state.selected_document_id = new_doc['id']
910
- st.session_state.clone_mode = False
911
- st.session_state.cloned_doc = None
912
- st.session_state.cloned_doc_str = ''
913
- st.rerun()
914
- else:
915
- st.error(message)
916
- except json.JSONDecodeError as e:
917
- st.error(f"Invalid JSON: {str(e)} 🚫")
918
 
919
 
920
 
 
831
  # Button to initiate cloning of the document
832
  if st.button("πŸ“„ Clone Document", key=f'clone_button_{idx}'):
833
  with st.spinner("Cloning document..."):
834
+
835
+ try:
836
+ existing_doc = container.read_item(item=clone_id, partition_key=clone_id)
837
+ new_doc = existing_doc.copy()
838
+ new_doc['id'] = generate_unique_id() # Generate new unique ID with timestamp
839
+ new_doc['name'] = generate_unique_id() # Generate new unique ID with timestamp
840
+ new_doc['createdAt'] = datetime.utcnow().isoformat() # Update the creation time
841
+ new_doc['_rid'] = None # Reset _rid or any system-managed fields
842
+ new_doc['_self'] = None
843
+ new_doc['_etag'] = None
844
+ new_doc['_attachments'] = None
845
+ new_doc['_ts'] = None # Reset timestamp to be updated by Cosmos DB automatically
846
+
847
+ # Insert the cloned document
848
+ response = container.create_item(body=new_doc)
849
+ st.success(f"Cloned document saved successfully with ID: {new_doc['id']} πŸŽ‰")
850
+
851
+ # Refresh the documents in session state
852
+ st.session_state.documents = list(container.query_items(
853
+ query="SELECT * FROM c ORDER BY c._ts DESC",
854
+ enable_cross_partition_query=True
855
+ ))
856
+
857
+ except exceptions.CosmosResourceNotFoundError:
858
+ st.error(f"Document with ID {clone_id} not found for cloning.")
859
+ except exceptions.CosmosHttpResponseError as e:
860
+ st.error(f"HTTP error occurred: {str(e)} 🚨")
861
+ except Exception as e:
862
+ st.error(f"An unexpected error occurred: {str(e)} 😱")
863
+
864
+
865
+ #try:
866
+ # Copy the document and reset system-managed fields
867
+ cloned_doc = doc.copy()
868
+ cloned_doc['id'] = generate_unique_id() # Generate new unique ID
869
+ cloned_doc['name'] = generate_unique_id() # Generate new unique ID
870
+ cloned_doc['createdAt'] = datetime.utcnow().isoformat() # Set new creation time
871
+ cloned_doc['_rid'] = None # Clear system-managed fields
872
+ cloned_doc['_self'] = None
873
+ cloned_doc['_etag'] = None
874
+ cloned_doc['_attachments'] = None
875
+ cloned_doc['_ts'] = None
876
 
877
+ # Save the cloned document
 
 
 
 
 
 
 
 
 
 
 
 
 
 
878
 
879
+ response = container.create_item(body=cloned_doc)
880
+ st.success(f"Cloned document saved successfully with ID: {cloned_doc['id']} πŸŽ‰")
881
+
882
+ # Refresh session state documents to show the new cloned document
883
  st.session_state.documents = list(container.query_items(
884
  query="SELECT * FROM c ORDER BY c._ts DESC",
885
  enable_cross_partition_query=True
886
+ ))
 
 
 
 
 
 
 
887
 
888
+ #except exceptions.CosmosHttpResponseError as e:
889
+ # st.error(f"HTTP error occurred: {str(e)} 🚨")
890
+ #except Exception as e:
891
+ # st.error(f"An unexpected error occurred: {str(e)} 😱")
892
+
893
+ # If clone mode is activated, allow user to edit the cloned document
894
+ if st.session_state.get('clone_mode', False):
895
+ st.markdown("#### Edit Cloned Document and Name Your Clone:")
896
+ cloned_doc_str = st.text_area(
897
+ "Cloned Document Content (in JSON format) - Update name before saving!",
898
+ value=st.session_state.cloned_doc_str,
899
+ height=300
900
+ )
901
 
902
+ # Save the edited cloned document
903
+ if st.button("πŸ’Ύ Save Cloned Document"):
904
+ try:
905
+ new_doc = json.loads(cloned_doc_str)
906
+ success, message = insert_record(container, new_doc)
907
+ if success:
908
+ st.success(f"Cloned document saved with id: {new_doc['id']} πŸŽ‰")
909
+ st.session_state.selected_document_id = new_doc['id']
910
+ st.session_state.clone_mode = False
911
+ st.session_state.cloned_doc = None
912
+ st.session_state.cloned_doc_str = ''
913
+ st.rerun()
914
+ else:
915
+ st.error(message)
916
+ except json.JSONDecodeError as e:
917
+ st.error(f"Invalid JSON: {str(e)} 🚫")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
918
 
919
 
920