awacke1 commited on
Commit
ad74e0e
·
verified ·
1 Parent(s): ffe1240

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -2
app.py CHANGED
@@ -684,13 +684,100 @@ def main():
684
  if st.session_state.current_index < total_docs - 1:
685
  st.session_state.current_index += 1
686
  st.rerun()
687
-
688
-
689
 
690
  elif selected_view == 'Show as Code Editor':
691
  Label = '#### 💻 Code editor view'
692
  st.markdown(Label)
693
  total_docs = len(documents)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
694
  doc = documents[st.session_state.current_index]
695
  # st.markdown(f"#### Document ID: {doc.get('id', '')}")
696
  doc_str = st.text_area("Edit Document",
 
684
  if st.session_state.current_index < total_docs - 1:
685
  st.session_state.current_index += 1
686
  st.rerun()
 
 
687
 
688
  elif selected_view == 'Show as Code Editor':
689
  Label = '#### 💻 Code editor view'
690
  st.markdown(Label)
691
  total_docs = len(documents)
692
+
693
+ if total_docs == 0:
694
+ st.warning("No documents available.")
695
+ return
696
+
697
+ doc = documents[st.session_state.current_index]
698
+ doc_str = st.text_area("Edit Document",
699
+ value=json.dumps(doc, indent=2),
700
+ height=300,
701
+ key=f'code_editor_{st.session_state.current_index}')
702
+
703
+ col_prev, col_next = st.columns([1, 1])
704
+ with col_prev:
705
+ if st.button("⬅️ Previous", key='prev_code'):
706
+ if st.session_state.current_index > 0:
707
+ st.session_state.current_index -= 1
708
+ st.rerun()
709
+ with col_next:
710
+ if st.button("➡️ Next", key='next_code'):
711
+ if st.session_state.current_index < total_docs - 1:
712
+ st.session_state.current_index += 1
713
+ st.rerun()
714
+
715
+ col_save, col_delete = st.columns([1, 1])
716
+ with col_save:
717
+ if st.button("💾 Save Changes", key=f'save_button_{st.session_state.current_index}'):
718
+ try:
719
+ updated_doc = json.loads(doc_str)
720
+ response = container.upsert_item(body=updated_doc)
721
+ if response:
722
+ st.success(f"Document {updated_doc['id']} saved successfully.")
723
+ st.session_state.selected_document_id = updated_doc['id']
724
+ st.rerun()
725
+ except json.JSONDecodeError:
726
+ st.error("Invalid JSON format. Please check your edits.")
727
+ except Exception as e:
728
+ st.error(f"Error saving document: {str(e)}")
729
+
730
+ with col_delete:
731
+ if st.button("🗑️ Delete", key=f'delete_button_{st.session_state.current_index}'):
732
+ try:
733
+ current_doc = json.loads(doc_str)
734
+ doc_id = current_doc.get("id")
735
+
736
+ if not doc_id:
737
+ st.error("Document ID not found.")
738
+ return
739
+
740
+ # Confirm deletion
741
+ if 'confirm_delete' not in st.session_state:
742
+ st.session_state.confirm_delete = False
743
+
744
+ if not st.session_state.confirm_delete:
745
+ if st.button("⚠️ Click to confirm deletion", key=f'confirm_delete_{st.session_state.current_index}'):
746
+ st.session_state.confirm_delete = True
747
+ st.rerun()
748
+ else:
749
+ try:
750
+ # Delete the document
751
+ container.delete_item(item=doc_id, partition_key=doc_id)
752
+
753
+ # Update the session state
754
+ st.session_state.confirm_delete = False
755
+
756
+ # Update the current index if necessary
757
+ if total_docs > 1:
758
+ if st.session_state.current_index == total_docs - 1:
759
+ st.session_state.current_index = max(0, total_docs - 2)
760
+ documents.pop(st.session_state.current_index)
761
+ else:
762
+ st.session_state.current_index = 0
763
+ documents.clear()
764
+
765
+ st.success(f"Document {doc_id} deleted successfully.")
766
+ st.rerun()
767
+
768
+ except Exception as e:
769
+ st.error(f"Error deleting document: {str(e)}")
770
+ st.session_state.confirm_delete = False
771
+
772
+ except json.JSONDecodeError:
773
+ st.error("Invalid JSON format. Please check the document.")
774
+ except Exception as e:
775
+ st.error(f"Error processing deletion: {str(e)}")
776
+
777
+ elif selected_view == 'Show as Code Editor - Old':
778
+ Label = '#### 💻 Code editor view'
779
+ st.markdown(Label)
780
+ total_docs = len(documents)
781
  doc = documents[st.session_state.current_index]
782
  # st.markdown(f"#### Document ID: {doc.get('id', '')}")
783
  doc_str = st.text_area("Edit Document",