Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -512,9 +512,6 @@ def preprocess_text(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:
|
@@ -560,42 +557,58 @@ def display_file_viewer(file_path):
|
|
560 |
|
561 |
def display_file_editor(file_path):
|
562 |
"""Display file content in editor with save functionality"""
|
563 |
-
content
|
564 |
-
if
|
565 |
-
st.
|
566 |
-
|
567 |
-
|
568 |
-
|
569 |
-
|
570 |
-
|
571 |
-
|
572 |
-
|
573 |
-
|
574 |
-
|
575 |
-
|
576 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
@@ -605,18 +618,15 @@ def update_file_management_section():
|
|
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])
|
@@ -625,6 +635,10 @@ def update_file_management_section():
|
|
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:
|
@@ -634,11 +648,20 @@ def update_file_management_section():
|
|
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
|
@@ -650,8 +673,6 @@ def update_file_management_section():
|
|
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():
|
|
|
512 |
|
513 |
|
514 |
|
|
|
|
|
|
|
515 |
def load_file_content(file_path):
|
516 |
"""Load and return file content with error handling"""
|
517 |
try:
|
|
|
557 |
|
558 |
def display_file_editor(file_path):
|
559 |
"""Display file content in editor with save functionality"""
|
560 |
+
# Initialize file content in session state if not already present
|
561 |
+
if 'file_content' not in st.session_state:
|
562 |
+
st.session_state.file_content = {}
|
563 |
+
|
564 |
+
# Load content if not in session state or if it's a different file
|
565 |
+
if file_path not in st.session_state.file_content:
|
566 |
+
content = load_file_content(file_path)
|
567 |
+
if content is not None:
|
568 |
+
st.session_state.file_content[file_path] = content
|
569 |
+
else:
|
570 |
+
return
|
571 |
+
|
572 |
+
st.markdown("### โ๏ธ File Editor")
|
573 |
+
st.markdown(f"**Editing:** {file_path}")
|
574 |
+
|
575 |
+
# Create a unique key for the text area
|
576 |
+
editor_key = f"editor_{hash(file_path)}"
|
577 |
+
|
578 |
+
# Editor with syntax highlighting for markdown
|
579 |
+
new_content = st.text_area(
|
580 |
+
"Edit content below:",
|
581 |
+
value=st.session_state.file_content[file_path],
|
582 |
+
height=400,
|
583 |
+
key=editor_key
|
584 |
+
)
|
585 |
+
|
586 |
+
col1, col2 = st.columns([1, 5])
|
587 |
+
with col1:
|
588 |
+
if st.button("๐พ Save Changes"):
|
589 |
+
if save_file_content(file_path, new_content):
|
590 |
+
st.session_state.file_content[file_path] = new_content
|
591 |
+
st.success("File saved successfully! ๐")
|
592 |
+
time.sleep(1)
|
593 |
+
st.rerun()
|
594 |
+
|
595 |
+
with col2:
|
596 |
+
st.download_button(
|
597 |
+
label="โฌ๏ธ Download File",
|
598 |
+
data=new_content,
|
599 |
+
file_name=os.path.basename(file_path),
|
600 |
+
mime="text/markdown"
|
601 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
602 |
|
|
|
603 |
def update_file_management_section():
|
604 |
+
# Initialize session state variables
|
605 |
+
if 'file_view_mode' not in st.session_state:
|
606 |
+
st.session_state.file_view_mode = None
|
607 |
+
if 'current_file' not in st.session_state:
|
608 |
+
st.session_state.current_file = None
|
609 |
+
if 'file_content' not in st.session_state:
|
610 |
+
st.session_state.file_content = {}
|
611 |
+
|
612 |
all_files = glob.glob("*.md")
|
613 |
all_files.sort(reverse=True)
|
614 |
|
|
|
618 |
if st.sidebar.button("๐ Delete All Files"):
|
619 |
for file in all_files:
|
620 |
os.remove(file)
|
621 |
+
st.session_state.file_content = {} # Clear the file content cache
|
622 |
+
st.session_state.current_file = None
|
623 |
+
st.session_state.file_view_mode = None
|
624 |
st.rerun()
|
625 |
|
626 |
if st.sidebar.button("โฌ๏ธ Download All Files"):
|
627 |
zip_file = create_zip_of_files(all_files)
|
628 |
st.sidebar.markdown(get_download_link(zip_file), unsafe_allow_html=True)
|
629 |
|
|
|
|
|
|
|
|
|
|
|
|
|
630 |
# Display files in sidebar with action buttons
|
631 |
for file in all_files:
|
632 |
col1, col2, col3, col4 = st.sidebar.columns([1,3,1,1])
|
|
|
635 |
if st.button("๐", key=f"view_{file}"):
|
636 |
st.session_state.current_file = file
|
637 |
st.session_state.file_view_mode = 'view'
|
638 |
+
if file not in st.session_state.file_content:
|
639 |
+
content = load_file_content(file)
|
640 |
+
if content is not None:
|
641 |
+
st.session_state.file_content[file] = content
|
642 |
st.rerun()
|
643 |
|
644 |
with col2:
|
|
|
648 |
if st.button("๐", key=f"edit_{file}"):
|
649 |
st.session_state.current_file = file
|
650 |
st.session_state.file_view_mode = 'edit'
|
651 |
+
if file not in st.session_state.file_content:
|
652 |
+
content = load_file_content(file)
|
653 |
+
if content is not None:
|
654 |
+
st.session_state.file_content[file] = content
|
655 |
st.rerun()
|
656 |
|
657 |
with col4:
|
658 |
if st.button("๐", key=f"delete_{file}"):
|
659 |
os.remove(file)
|
660 |
+
if file in st.session_state.file_content:
|
661 |
+
del st.session_state.file_content[file]
|
662 |
+
if st.session_state.current_file == file:
|
663 |
+
st.session_state.current_file = None
|
664 |
+
st.session_state.file_view_mode = None
|
665 |
st.rerun()
|
666 |
|
667 |
# Display viewer or editor in main area based on mode
|
|
|
673 |
|
674 |
|
675 |
|
|
|
|
|
676 |
|
677 |
# ๐ญ Main function - "All the world's a stage, and all the code merely players" -Shakespeare, probably
|
678 |
def main():
|