Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,6 +11,127 @@ from io import StringIO
|
|
11 |
import openpyxl
|
12 |
import csv
|
13 |
import base64
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
# π¨ File type emojis - Making file types fun and visual!
|
16 |
FILE_TYPES = {
|
|
|
11 |
import openpyxl
|
12 |
import csv
|
13 |
import base64
|
14 |
+
import glob
|
15 |
+
import os
|
16 |
+
|
17 |
+
# Add this function after the imports and before other functions
|
18 |
+
def scan_and_load_files():
|
19 |
+
"""π File Detective - Scans directory for supported files and loads them automatically"""
|
20 |
+
loaded_files = []
|
21 |
+
|
22 |
+
# Get all files with supported extensions
|
23 |
+
for ext in FILE_TYPES.keys():
|
24 |
+
files = glob.glob(f"*.{ext}")
|
25 |
+
for filepath in files:
|
26 |
+
try:
|
27 |
+
with open(filepath, 'r', encoding='utf-8') as f:
|
28 |
+
content = f.read()
|
29 |
+
file_type = filepath.split('.')[-1].lower()
|
30 |
+
|
31 |
+
# Store file content and type
|
32 |
+
st.session_state.file_data[filepath] = content
|
33 |
+
st.session_state.file_types[filepath] = file_type
|
34 |
+
|
35 |
+
# Special handling for markdown files
|
36 |
+
if file_type == 'md':
|
37 |
+
if filepath not in [f["filename"] for f in st.session_state.md_files_history]:
|
38 |
+
st.session_state.md_outline[filepath] = parse_markdown_outline(content)
|
39 |
+
st.session_state.rendered_content[filepath] = content
|
40 |
+
add_to_history(filepath, content, "auto-loaded")
|
41 |
+
|
42 |
+
loaded_files.append(filepath)
|
43 |
+
except Exception as e:
|
44 |
+
st.error(f"π¨ Error loading {filepath}: {str(e)}")
|
45 |
+
|
46 |
+
return loaded_files
|
47 |
+
|
48 |
+
# Modify the main function to include initial file scanning
|
49 |
+
def main():
|
50 |
+
st.title("πβ¨ Super Smart File Handler with Markdown Magic! β¨π")
|
51 |
+
|
52 |
+
# Scan for existing files on startup
|
53 |
+
if 'files_scanned' not in st.session_state:
|
54 |
+
loaded_files = scan_and_load_files()
|
55 |
+
if loaded_files:
|
56 |
+
st.success(f"π Auto-loaded {len(loaded_files)} existing files: {', '.join(loaded_files)}")
|
57 |
+
st.session_state.files_scanned = True
|
58 |
+
|
59 |
+
# Show markdown history in sidebar
|
60 |
+
show_sidebar_history()
|
61 |
+
|
62 |
+
# Add tabs for different upload methods
|
63 |
+
upload_tab, book_tab = st.tabs(["π€ File Upload", "π Book View"])
|
64 |
+
|
65 |
+
with upload_tab:
|
66 |
+
# Add a rescan button
|
67 |
+
if st.button("π Rescan Directory"):
|
68 |
+
st.session_state.files_scanned = False
|
69 |
+
st.experimental_rerun()
|
70 |
+
|
71 |
+
col1, col2 = st.columns(2)
|
72 |
+
|
73 |
+
with col1:
|
74 |
+
single_uploaded_file = st.file_uploader(
|
75 |
+
"π€ Upload single file",
|
76 |
+
type=list(FILE_TYPES.keys()),
|
77 |
+
help="Supports: " + ", ".join([f"{v} (.{k})" for k, v in FILE_TYPES.items()]),
|
78 |
+
key="single_uploader"
|
79 |
+
)
|
80 |
+
|
81 |
+
with col2:
|
82 |
+
multiple_uploaded_files = st.file_uploader(
|
83 |
+
"π Upload multiple files",
|
84 |
+
type=list(FILE_TYPES.keys()),
|
85 |
+
accept_multiple_files=True,
|
86 |
+
help="Upload multiple files to view as a book",
|
87 |
+
key="multiple_uploader"
|
88 |
+
)
|
89 |
+
|
90 |
+
# Process single file upload
|
91 |
+
if single_uploaded_file:
|
92 |
+
content, file_type = read_file_content(single_uploaded_file)
|
93 |
+
if content is not None:
|
94 |
+
st.session_state.file_data[single_uploaded_file.name] = content
|
95 |
+
st.session_state.file_types[single_uploaded_file.name] = file_type
|
96 |
+
st.success(f"π Loaded {FILE_TYPES.get(file_type, 'π')} file: {single_uploaded_file.name}")
|
97 |
+
|
98 |
+
# Process multiple file upload
|
99 |
+
if multiple_uploaded_files:
|
100 |
+
for uploaded_file in multiple_uploaded_files:
|
101 |
+
content, file_type = read_file_content(uploaded_file)
|
102 |
+
if content is not None:
|
103 |
+
st.session_state.file_data[uploaded_file.name] = content
|
104 |
+
st.session_state.file_types[uploaded_file.name] = file_type
|
105 |
+
st.success(f"π Loaded {len(multiple_uploaded_files)} files")
|
106 |
+
|
107 |
+
# Show file history
|
108 |
+
show_file_history()
|
109 |
+
|
110 |
+
# Show individual files
|
111 |
+
if st.session_state.file_data:
|
112 |
+
st.subheader("π Your Files")
|
113 |
+
for filename, content in st.session_state.file_data.items():
|
114 |
+
file_type = st.session_state.file_types[filename]
|
115 |
+
with st.expander(f"{FILE_TYPES.get(file_type, 'π')} {filename}"):
|
116 |
+
if file_type == "md":
|
117 |
+
content = create_markdown_tabs(content, filename)
|
118 |
+
else:
|
119 |
+
edited_content = st.text_area(
|
120 |
+
"Content",
|
121 |
+
content,
|
122 |
+
height=300,
|
123 |
+
key=f"edit_{filename}"
|
124 |
+
)
|
125 |
+
if edited_content != content:
|
126 |
+
st.session_state.file_data[filename] = edited_content
|
127 |
+
content = edited_content
|
128 |
+
|
129 |
+
if st.button(f"πΎ Save {filename}"):
|
130 |
+
if save_file_content(content, filename, file_type):
|
131 |
+
st.success(f"β¨ Saved {filename} successfully!")
|
132 |
+
|
133 |
+
with book_tab:
|
134 |
+
show_book_view()
|
135 |
|
136 |
# π¨ File type emojis - Making file types fun and visual!
|
137 |
FILE_TYPES = {
|