awacke1 commited on
Commit
c3d4d36
·
verified ·
1 Parent(s): 9e10f5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -10
app.py CHANGED
@@ -570,30 +570,41 @@ def compare_and_delete_files(files):
570
  # Function to get file size
571
  def get_file_size(file_path):
572
  return os.path.getsize(file_path)
573
-
574
  def FileSidebar():
575
- # File Sidebar for files 🌐View, 📂Edit, and 🗑Delete per file
576
- all_files = glob.glob("*.md") + glob.glob("*_abstract.html") + glob.glob("*_abstract.pdf")
577
- all_files = [file for file in all_files if os.path.basename(file) and len(os.path.splitext(file)[0]) >= 10]
578
- all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True)
 
 
 
 
 
 
 
 
 
 
579
 
580
  # ⬇️ Download
581
  Files1, Files2 = st.sidebar.columns(2)
582
  with Files1:
583
  if st.sidebar.button("🗑 Delete All"):
584
- for file in all_files:
585
  try:
586
  os.remove(file)
587
  except Exception as e:
588
  st.sidebar.error(f"Error deleting {file}: {str(e)}")
 
589
  st.rerun()
590
  with Files2:
591
  if st.sidebar.button("⬇️ Download"):
592
- zip_file = create_zip_of_files(all_files)
593
  st.sidebar.markdown(get_zip_download_link(zip_file), unsafe_allow_html=True)
594
 
595
  # Add files 🌐View, 📂Edit, and 🗑Delete per file
596
- for file in all_files:
597
  timestamp = time.strftime("%H%M%S%f")[:-4] # HHMMSSNN format
598
 
599
  col1, col2, col3, col4 = st.sidebar.columns([3,1,1,1])
@@ -612,9 +623,10 @@ def FileSidebar():
612
  try:
613
  os.remove(file)
614
  st.sidebar.success(f"Deleted {file}")
 
 
615
  except Exception as e:
616
  st.sidebar.error(f"Error deleting {file}: {str(e)}")
617
- st.rerun()
618
 
619
  # Display content in main area
620
  if 'current_file' in st.session_state and 'action' in st.session_state:
@@ -624,10 +636,16 @@ def FileSidebar():
624
  edit_file(st.session_state.current_file)
625
 
626
  # Show the result MD from the search
627
- if 'search_result' in st.session_state:
628
  st.markdown("## Search Result")
629
  st.markdown(st.session_state.search_result)
 
630
 
 
 
 
 
 
631
  # Function to be called after search completes
632
  def redraw_sidebar_after_search(search_result):
633
  st.session_state.search_result = search_result
 
570
  # Function to get file size
571
  def get_file_size(file_path):
572
  return os.path.getsize(file_path)
573
+
574
  def FileSidebar():
575
+ # Initialize session state variables if they don't exist
576
+ if 'files_to_display' not in st.session_state:
577
+ st.session_state.files_to_display = []
578
+ if 'search_result' not in st.session_state:
579
+ st.session_state.search_result = None
580
+ if 'sidebar_needs_update' not in st.session_state:
581
+ st.session_state.sidebar_needs_update = False
582
+
583
+ # Check if sidebar needs update
584
+ if st.session_state.sidebar_needs_update:
585
+ st.session_state.files_to_display = glob.glob("*.md") + glob.glob("*_abstract.html") + glob.glob("*_abstract.pdf")
586
+ st.session_state.files_to_display = [file for file in st.session_state.files_to_display if os.path.basename(file) and len(os.path.splitext(file)[0]) >= 10]
587
+ st.session_state.files_to_display.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True)
588
+ st.session_state.sidebar_needs_update = False
589
 
590
  # ⬇️ Download
591
  Files1, Files2 = st.sidebar.columns(2)
592
  with Files1:
593
  if st.sidebar.button("🗑 Delete All"):
594
+ for file in st.session_state.files_to_display:
595
  try:
596
  os.remove(file)
597
  except Exception as e:
598
  st.sidebar.error(f"Error deleting {file}: {str(e)}")
599
+ st.session_state.sidebar_needs_update = True
600
  st.rerun()
601
  with Files2:
602
  if st.sidebar.button("⬇️ Download"):
603
+ zip_file = create_zip_of_files(st.session_state.files_to_display)
604
  st.sidebar.markdown(get_zip_download_link(zip_file), unsafe_allow_html=True)
605
 
606
  # Add files 🌐View, 📂Edit, and 🗑Delete per file
607
+ for file in st.session_state.files_to_display:
608
  timestamp = time.strftime("%H%M%S%f")[:-4] # HHMMSSNN format
609
 
610
  col1, col2, col3, col4 = st.sidebar.columns([3,1,1,1])
 
623
  try:
624
  os.remove(file)
625
  st.sidebar.success(f"Deleted {file}")
626
+ st.session_state.sidebar_needs_update = True
627
+ st.rerun()
628
  except Exception as e:
629
  st.sidebar.error(f"Error deleting {file}: {str(e)}")
 
630
 
631
  # Display content in main area
632
  if 'current_file' in st.session_state and 'action' in st.session_state:
 
636
  edit_file(st.session_state.current_file)
637
 
638
  # Show the result MD from the search
639
+ if st.session_state.search_result:
640
  st.markdown("## Search Result")
641
  st.markdown(st.session_state.search_result)
642
+ st.session_state.search_result = None # Clear the search result after displaying
643
 
644
+ # Function to be called after search completes
645
+ def update_sidebar_after_search(search_result):
646
+ st.session_state.search_result = search_result
647
+ st.session_state.sidebar_needs_update = True
648
+
649
  # Function to be called after search completes
650
  def redraw_sidebar_after_search(search_result):
651
  st.session_state.search_result = search_result