awacke1 commited on
Commit
47eb238
Β·
verified Β·
1 Parent(s): 2e57b5f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -47
app.py CHANGED
@@ -174,38 +174,14 @@ def update_record(container, updated_record):
174
  return False, f"An unexpected error occurred: {traceback.format_exc()} 😱"
175
 
176
  # πŸ—‘οΈ Delete record - Saying goodbye to data (it's not you, it's me)
177
- def delete_record(container, id):
178
  try:
179
- # First query to verify document exists and get partition key
180
- query = "SELECT c.id FROM c WHERE c.id = @id"
181
- params = [{"name": "@id", "value": id}]
182
-
183
- # Execute query with cross partition enabled
184
- items = list(container.query_items(
185
- query=query,
186
- parameters=params,
187
- enable_cross_partition_query=True
188
- ))
189
-
190
- if not items:
191
- return False, f"Record with id {id} not found. πŸ•΅οΈβ€β™‚οΈ"
192
-
193
- # Get partition key value from the query result
194
- partition_key = items[0]['id']
195
-
196
- # Create partition key instance
197
- pk = PartitionKey(partition_key)
198
-
199
- # Delete the document using correct partition key
200
- container.delete_item(item=id, partition_key=pk)
201
- return True, f"Successfully deleted record with id: {id} πŸ—‘οΈ"
202
-
203
- except exceptions.CosmosResourceNotFoundError:
204
- return False, f"Record with id {id} not found. πŸ•΅οΈβ€β™‚οΈ"
205
  except exceptions.CosmosHttpResponseError as e:
206
  return False, f"HTTP error occurred: {str(e)} 🚨"
207
  except Exception as e:
208
- return False, f"An unexpected error occurred: {str(e)} 😱"
209
 
210
 
211
  # πŸ’Ύ Save to Cosmos DB - Preserving data for future generations (or just until the next update)
@@ -737,7 +713,7 @@ def main():
737
  st.rerun()
738
 
739
  elif selected_view == 'Show as Code Editor':
740
- Label = '# πŸ’» Code editor view - Code is poetry, bugs are typos'
741
  st.markdown(Label)
742
  total_docs = len(documents)
743
  doc = documents[st.session_state.current_index]
@@ -747,7 +723,6 @@ def main():
747
  height=300,
748
  key=f'code_editor_{st.session_state.current_index}')
749
 
750
- # β¬…οΈβž‘οΈ Navigation controls
751
  col_prev, col_next = st.columns([1, 1])
752
  with col_prev:
753
  if st.button("⬅️ Previous", key='prev_code'):
@@ -760,7 +735,6 @@ def main():
760
  st.session_state.current_index += 1
761
  st.rerun()
762
 
763
- # πŸ’Ύ πŸ—‘οΈ Save and Delete controls
764
  col_save, col_delete = st.columns([1, 1])
765
  with col_save:
766
  if st.button("πŸ’Ύ Save Changes", key=f'save_button_{st.session_state.current_index}'):
@@ -780,17 +754,16 @@ def main():
780
  if st.button("πŸ—‘οΈ Delete", key=f'delete_button_{st.session_state.current_index}'):
781
  try:
782
  current_doc = json.loads(doc_str)
783
- response = container.delete_item(
784
- item=current_doc['id'],
785
- partition_key=current_doc['id']
786
- )
787
- if response:
788
- st.success(f"Document {current_doc['id']} deleted successfully.")
789
  if st.session_state.current_index > 0:
790
  st.session_state.current_index -= 1
791
  st.rerun()
792
- except Exception as e:
793
- st.error(f"Error deleting document: {str(e)}")
 
 
794
 
795
 
796
 
@@ -847,14 +820,24 @@ def main():
847
  search_glossary(json.dumps(editable_doc, indent=2))
848
 
849
  with col_delete:
850
- if st.button("πŸ—‘οΈ Delete Record", key=f'delete_button_{idx}'):
851
- # Use only the 'id' to delete the record
852
- success, message = delete_record(container, id=doc.get('id', ''))
853
- if success:
854
- st.success(message)
855
- st.rerun() # Refresh the page after deletion
856
- else:
857
- st.error(message)
 
 
 
 
 
 
 
 
 
 
858
 
859
  elif selected_view == 'Clone Document':
860
  st.markdown("#### Clone a document:")
 
174
  return False, f"An unexpected error occurred: {traceback.format_exc()} 😱"
175
 
176
  # πŸ—‘οΈ Delete record - Saying goodbye to data (it's not you, it's me)
177
+ def delete_record(container, record):
178
  try:
179
+ container.delete_item(item=record['id'], partition_key=record['id'])
180
+ return True, f"Record with id {record['id']} successfully deleted. πŸ—‘οΈ"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
  except exceptions.CosmosHttpResponseError as e:
182
  return False, f"HTTP error occurred: {str(e)} 🚨"
183
  except Exception as e:
184
+ return False, f"An unexpected error occurred: {traceback.format_exc()} 😱"
185
 
186
 
187
  # πŸ’Ύ Save to Cosmos DB - Preserving data for future generations (or just until the next update)
 
713
  st.rerun()
714
 
715
  elif selected_view == 'Show as Code Editor':
716
+ Label = '# πŸ’» Code editor view'
717
  st.markdown(Label)
718
  total_docs = len(documents)
719
  doc = documents[st.session_state.current_index]
 
723
  height=300,
724
  key=f'code_editor_{st.session_state.current_index}')
725
 
 
726
  col_prev, col_next = st.columns([1, 1])
727
  with col_prev:
728
  if st.button("⬅️ Previous", key='prev_code'):
 
735
  st.session_state.current_index += 1
736
  st.rerun()
737
 
 
738
  col_save, col_delete = st.columns([1, 1])
739
  with col_save:
740
  if st.button("πŸ’Ύ Save Changes", key=f'save_button_{st.session_state.current_index}'):
 
754
  if st.button("πŸ—‘οΈ Delete", key=f'delete_button_{st.session_state.current_index}'):
755
  try:
756
  current_doc = json.loads(doc_str)
757
+ success, message = delete_record(container, current_doc)
758
+ if success:
759
+ st.success(message)
 
 
 
760
  if st.session_state.current_index > 0:
761
  st.session_state.current_index -= 1
762
  st.rerun()
763
+ else:
764
+ st.error(message)
765
+ except json.JSONDecodeError as e:
766
+ st.error(f"Invalid JSON: {str(e)} 🚫")
767
 
768
 
769
 
 
820
  search_glossary(json.dumps(editable_doc, indent=2))
821
 
822
  with col_delete:
823
+ #if st.button("πŸ—‘οΈ Delete Record", key=f'delete_button_{idx}'):
824
+
825
+
826
+ if st.button("πŸ—‘οΈ Delete", key=f'delete_button_{st.session_state.current_index}'):
827
+ try:
828
+ current_doc = json.loads(doc_str)
829
+ success, message = delete_record(container, current_doc)
830
+ if success:
831
+ st.success(message)
832
+ if st.session_state.current_index > 0:
833
+ st.session_state.current_index -= 1
834
+ st.rerun()
835
+ else:
836
+ st.error(message)
837
+ except json.JSONDecodeError as e:
838
+ st.error(f"Invalid JSON: {str(e)} 🚫")
839
+
840
+
841
 
842
  elif selected_view == 'Clone Document':
843
  st.markdown("#### Clone a document:")