awacke1 commited on
Commit
700ec45
β€’
1 Parent(s): 86666f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -2
app.py CHANGED
@@ -498,7 +498,17 @@ def create_new_blank_record(container):
498
  st.error(f"An unexpected error occurred: {str(e)} 😱")
499
 
500
 
 
 
 
 
 
 
501
 
 
 
 
 
502
 
503
 
504
 
@@ -831,11 +841,64 @@ def main():
831
  st.error("Failed to create new document")
832
  except Exception as e:
833
  st.error(f"Error creating document: {str(e)}")
834
-
835
- #elif selected_view == 'New Record':
836
  elif selected_view == 'New Record':
837
  st.markdown("#### Create a new document:")
838
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
839
  if st.button("πŸ€– Insert Auto-Generated Record"):
840
  auto_doc = {
841
  "id": generate_unique_id(),
 
498
  st.error(f"An unexpected error occurred: {str(e)} 😱")
499
 
500
 
501
+ # Function to preprocess the pasted content
502
+ def preprocess_text(text):
503
+ # Replace CRLF with \n
504
+ text = text.replace('\r\n', '\\n')
505
+ text = text.replace('\r', '\\n')
506
+ text = text.replace('\n', '\\n')
507
 
508
+ # Optionally, you can add more replacements for other unsupported characters here
509
+ # Example: handling quotes or other escape sequences if necessary
510
+
511
+ return text
512
 
513
 
514
 
 
841
  st.error("Failed to create new document")
842
  except Exception as e:
843
  st.error(f"Error creating document: {str(e)}")
 
 
844
  elif selected_view == 'New Record':
845
  st.markdown("#### Create a new document:")
846
 
847
+ if st.button("πŸ€– Insert Auto-Generated Record"):
848
+ auto_doc = {
849
+ "id": generate_unique_id(),
850
+ "name": f"Auto-generated Record {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
851
+ "content": "This is an auto-generated record.",
852
+ "timestamp": datetime.now().isoformat()
853
+ }
854
+ success, message = save_or_clone_to_cosmos_db(container, document=auto_doc)
855
+ if success:
856
+ st.success(message)
857
+ st.rerun()
858
+ else:
859
+ st.error(message)
860
+ else:
861
+ new_id = st.text_input("ID", value=generate_unique_id(), key='new_id')
862
+ default_doc = {
863
+ "id": new_id,
864
+ "name": "New Document",
865
+ "content": "",
866
+ "timestamp": datetime.now().isoformat()
867
+ }
868
+ new_doc_str = st.text_area("Document Content (in JSON format)",
869
+ value=json.dumps(default_doc, indent=2),
870
+ height=300)
871
+
872
+ if st.button("βž• Create New Document"):
873
+ try:
874
+ # Preprocess the text before loading it into JSON
875
+ cleaned_doc_str = preprocess_text(new_doc_str)
876
+ new_doc = json.loads(cleaned_doc_str)
877
+ new_doc['id'] = new_id # Ensure ID matches input field
878
+
879
+ success, message = insert_record(container, new_doc)
880
+ if success:
881
+ st.success(f"New document created with id: {new_doc['id']} πŸŽ‰")
882
+ st.session_state.selected_document_id = new_doc['id']
883
+ st.rerun()
884
+ else:
885
+ st.error(message)
886
+ except json.JSONDecodeError as e:
887
+ st.error(f"Invalid JSON: {str(e)} 🚫")
888
+
889
+ st.subheader(f"πŸ“Š Container: {st.session_state.selected_container}")
890
+ if st.session_state.selected_container:
891
+ if documents_to_display:
892
+ Label = '# πŸ“Š Data display - Data tells tales that words cannot'
893
+ st.markdown(Label)
894
+ df = pd.DataFrame(documents_to_display)
895
+ st.dataframe(df)
896
+ else:
897
+ st.info("No documents to display. 🧐")
898
+
899
+ elif selected_view == 'New Record2':
900
+ st.markdown("#### Create a new document:")
901
+
902
  if st.button("πŸ€– Insert Auto-Generated Record"):
903
  auto_doc = {
904
  "id": generate_unique_id(),