awacke1 commited on
Commit
c207879
Β·
verified Β·
1 Parent(s): a8e2221

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +142 -30
app.py CHANGED
@@ -511,6 +511,147 @@ def preprocess_text(text):
511
  return text
512
 
513
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
514
 
515
  # 🎭 Main function - "All the world's a stage, and all the code merely players" -Shakespeare, probably
516
  def main():
@@ -1132,36 +1273,7 @@ def main():
1132
  st.success("File updated successfully! πŸŽ‰")
1133
 
1134
  # πŸ“‚ File management - "Manage many, maintain order"
1135
- st.sidebar.title("πŸ“ File Management")
1136
-
1137
- all_files = glob.glob("*.md")
1138
- all_files.sort(reverse=True)
1139
-
1140
- if st.sidebar.button("πŸ—‘ Delete All Files"):
1141
- for file in all_files:
1142
- os.remove(file)
1143
- st.rerun()
1144
-
1145
- if st.sidebar.button("⬇️ Download All Files"):
1146
- zip_file = create_zip_of_files(all_files)
1147
- st.sidebar.markdown(get_download_link(zip_file), unsafe_allow_html=True)
1148
-
1149
- for file in all_files:
1150
- col1, col2, col3, col4 = st.sidebar.columns([1,3,1,1])
1151
- with col1:
1152
- if st.button("🌐", key="view_"+file):
1153
- st.session_state.current_file = file
1154
- st.session_state.file_content = load_file(file)
1155
- with col2:
1156
- st.markdown(get_download_link(file), unsafe_allow_html=True)
1157
- with col3:
1158
- if st.button("πŸ“‚", key="edit_"+file):
1159
- st.session_state.current_file = file
1160
- st.session_state.file_content = load_file(file)
1161
- with col4:
1162
- if st.button("πŸ—‘", key="delete_"+file):
1163
- os.remove(file)
1164
- st.rerun()
1165
 
1166
  except exceptions.CosmosHttpResponseError as e:
1167
  st.error(f"Failed to connect to Cosmos DB. HTTP error: {str(e)} 🚨")
 
511
  return text
512
 
513
 
514
+
515
+
516
+
517
+
518
+ def load_file_content(file_path):
519
+ """Load and return file content with error handling"""
520
+ try:
521
+ with open(file_path, 'r', encoding='utf-8') as file:
522
+ return file.read()
523
+ except Exception as e:
524
+ st.error(f"Error loading file: {str(e)}")
525
+ return None
526
+
527
+ def save_file_content(file_path, content):
528
+ """Save file content with error handling"""
529
+ try:
530
+ with open(file_path, 'w', encoding='utf-8') as file:
531
+ file.write(content)
532
+ return True
533
+ except Exception as e:
534
+ st.error(f"Error saving file: {str(e)}")
535
+ return False
536
+
537
+ def display_file_viewer(file_path):
538
+ """Display file content in markdown viewer"""
539
+ content = load_file_content(file_path)
540
+ if content:
541
+ st.markdown("### πŸ“„ File Viewer")
542
+ st.markdown(f"**Viewing:** {file_path}")
543
+
544
+ # Add file metadata
545
+ file_stats = os.stat(file_path)
546
+ st.markdown(f"**Last modified:** {datetime.fromtimestamp(file_stats.st_mtime).strftime('%Y-%m-%d %H:%M:%S')}")
547
+ st.markdown(f"**Size:** {file_stats.st_size} bytes")
548
+
549
+ # Display content in markdown
550
+ st.markdown("---")
551
+ st.markdown(content)
552
+
553
+ # Add download button
554
+ st.download_button(
555
+ label="⬇️ Download File",
556
+ data=content,
557
+ file_name=os.path.basename(file_path),
558
+ mime="text/markdown"
559
+ )
560
+
561
+ def display_file_editor(file_path):
562
+ """Display file content in editor with save functionality"""
563
+ content = load_file_content(file_path)
564
+ if content:
565
+ st.markdown("### ✏️ File Editor")
566
+ st.markdown(f"**Editing:** {file_path}")
567
+
568
+ # Create a unique key for the text area
569
+ editor_key = f"editor_{hash(file_path)}"
570
+
571
+ # Editor with syntax highlighting for markdown
572
+ new_content = st.text_area(
573
+ "Edit content below:",
574
+ value=content,
575
+ height=400,
576
+ key=editor_key
577
+ )
578
+
579
+ col1, col2 = st.columns([1, 5])
580
+ with col1:
581
+ if st.button("πŸ’Ύ Save Changes"):
582
+ if save_file_content(file_path, new_content):
583
+ st.success("File saved successfully! πŸŽ‰")
584
+ # Update the file content in session state
585
+ st.session_state.file_content = new_content
586
+ time.sleep(1) # Give user time to see success message
587
+ st.rerun()
588
+
589
+ with col2:
590
+ st.download_button(
591
+ label="⬇️ Download File",
592
+ data=new_content,
593
+ file_name=os.path.basename(file_path),
594
+ mime="text/markdown"
595
+ )
596
+
597
+ # Update the file management section in main()
598
+ def update_file_management_section():
599
+ all_files = glob.glob("*.md")
600
+ all_files.sort(reverse=True)
601
+
602
+ # File management buttons in sidebar
603
+ st.sidebar.title("πŸ“ File Management")
604
+
605
+ if st.sidebar.button("πŸ—‘ Delete All Files"):
606
+ for file in all_files:
607
+ os.remove(file)
608
+ st.rerun()
609
+
610
+ if st.sidebar.button("⬇️ Download All Files"):
611
+ zip_file = create_zip_of_files(all_files)
612
+ st.sidebar.markdown(get_download_link(zip_file), unsafe_allow_html=True)
613
+
614
+ # Initialize session state for view/edit mode if not exists
615
+ if 'file_view_mode' not in st.session_state:
616
+ st.session_state.file_view_mode = None
617
+ if 'current_file' not in st.session_state:
618
+ st.session_state.current_file = None
619
+
620
+ # Display files in sidebar with action buttons
621
+ for file in all_files:
622
+ col1, col2, col3, col4 = st.sidebar.columns([1,3,1,1])
623
+
624
+ with col1:
625
+ if st.button("🌐", key=f"view_{file}"):
626
+ st.session_state.current_file = file
627
+ st.session_state.file_view_mode = 'view'
628
+ st.rerun()
629
+
630
+ with col2:
631
+ st.markdown(get_download_link(file), unsafe_allow_html=True)
632
+
633
+ with col3:
634
+ if st.button("πŸ“‚", key=f"edit_{file}"):
635
+ st.session_state.current_file = file
636
+ st.session_state.file_view_mode = 'edit'
637
+ st.rerun()
638
+
639
+ with col4:
640
+ if st.button("πŸ—‘", key=f"delete_{file}"):
641
+ os.remove(file)
642
+ st.rerun()
643
+
644
+ # Display viewer or editor in main area based on mode
645
+ if st.session_state.current_file:
646
+ if st.session_state.file_view_mode == 'view':
647
+ display_file_viewer(st.session_state.current_file)
648
+ elif st.session_state.file_view_mode == 'edit':
649
+ display_file_editor(st.session_state.current_file)
650
+
651
+
652
+
653
+
654
+
655
 
656
  # 🎭 Main function - "All the world's a stage, and all the code merely players" -Shakespeare, probably
657
  def main():
 
1273
  st.success("File updated successfully! πŸŽ‰")
1274
 
1275
  # πŸ“‚ File management - "Manage many, maintain order"
1276
+ update_file_management_section()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1277
 
1278
  except exceptions.CosmosHttpResponseError as e:
1279
  st.error(f"Failed to connect to Cosmos DB. HTTP error: {str(e)} 🚨")