Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,21 +6,22 @@ import time
|
|
6 |
from datetime import datetime
|
7 |
import re
|
8 |
import pandas as pd
|
9 |
-
#import markdown
|
10 |
import yaml
|
11 |
from io import StringIO
|
12 |
import openpyxl
|
13 |
import csv
|
14 |
|
15 |
-
#
|
16 |
if 'file_data' not in st.session_state:
|
17 |
st.session_state.file_data = {}
|
18 |
if 'file_types' not in st.session_state:
|
19 |
-
st.session_state.file_types = {}
|
20 |
if 'md_outline' not in st.session_state:
|
21 |
-
st.session_state.md_outline = {}
|
|
|
|
|
22 |
|
23 |
-
#
|
24 |
FILE_TYPES = {
|
25 |
"md": "π Markdown",
|
26 |
"txt": "π Text",
|
@@ -32,12 +33,12 @@ FILE_TYPES = {
|
|
32 |
}
|
33 |
|
34 |
def parse_markdown_outline(content):
|
35 |
-
"""
|
36 |
lines = content.split('\n')
|
37 |
outline = []
|
38 |
for line in lines:
|
39 |
if line.strip().startswith('#'):
|
40 |
-
level = len(line.split()[0])
|
41 |
title = line.strip('#').strip()
|
42 |
outline.append({
|
43 |
'level': level,
|
@@ -46,13 +47,43 @@ def parse_markdown_outline(content):
|
|
46 |
})
|
47 |
return outline
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
def read_file_content(uploaded_file):
|
50 |
-
"""
|
51 |
file_type = uploaded_file.name.split('.')[-1].lower()
|
52 |
try:
|
53 |
if file_type == 'md':
|
54 |
content = uploaded_file.getvalue().decode()
|
55 |
st.session_state.md_outline[uploaded_file.name] = parse_markdown_outline(content)
|
|
|
56 |
return content, "md"
|
57 |
|
58 |
elif file_type == 'csv':
|
@@ -79,14 +110,14 @@ def read_file_content(uploaded_file):
|
|
79 |
return None, None
|
80 |
|
81 |
def save_file_content(content, filename, file_type):
|
82 |
-
"""
|
83 |
try:
|
84 |
if file_type == "md":
|
85 |
with open(filename, 'w') as f:
|
86 |
f.write(content)
|
|
|
87 |
|
88 |
elif file_type in ["csv", "xlsx"]:
|
89 |
-
# Convert string back to DataFrame
|
90 |
df = pd.read_csv(StringIO(content)) if file_type == "csv" else pd.read_excel(StringIO(content))
|
91 |
if file_type == "csv":
|
92 |
df.to_csv(filename, index=False)
|
@@ -110,22 +141,9 @@ def save_file_content(content, filename, file_type):
|
|
110 |
st.error(f"π¨ Error saving {filename}: {str(e)}")
|
111 |
return False
|
112 |
|
113 |
-
def show_markdown_preview(content):
|
114 |
-
"""π Show pretty markdown preview!"""
|
115 |
-
st.markdown("### π Markdown Preview")
|
116 |
-
st.markdown(content)
|
117 |
-
|
118 |
-
def show_markdown_outline(filename):
|
119 |
-
"""π Display pretty markdown outline!"""
|
120 |
-
if filename in st.session_state.md_outline:
|
121 |
-
st.markdown("### π Document Outline")
|
122 |
-
for item in st.session_state.md_outline[filename]:
|
123 |
-
st.markdown(f"{item['indent']}β’ {item['title']}")
|
124 |
-
|
125 |
def main():
|
126 |
st.title("πβ¨ Super Smart File Handler with Markdown Magic! β¨π")
|
127 |
|
128 |
-
# π Enhanced file upload with type support
|
129 |
uploaded_file = st.file_uploader(
|
130 |
"π€ Upload your file!",
|
131 |
type=list(FILE_TYPES.keys()),
|
@@ -138,34 +156,27 @@ def main():
|
|
138 |
st.session_state.file_data[uploaded_file.name] = content
|
139 |
st.session_state.file_types[uploaded_file.name] = file_type
|
140 |
st.success(f"π Loaded {FILE_TYPES.get(file_type, 'π')} file: {uploaded_file.name}")
|
141 |
-
|
142 |
-
# π Special handling for markdown files
|
143 |
-
if file_type == "md":
|
144 |
-
show_markdown_preview(content)
|
145 |
-
show_markdown_outline(uploaded_file.name)
|
146 |
|
147 |
-
# π File Display and Editing
|
148 |
if st.session_state.file_data:
|
149 |
st.subheader("π Your Files")
|
150 |
for filename, content in st.session_state.file_data.items():
|
151 |
file_type = st.session_state.file_types[filename]
|
152 |
with st.expander(f"{FILE_TYPES.get(file_type, 'π')} {filename}"):
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
if
|
163 |
-
st.session_state.
|
164 |
-
|
165 |
-
show_markdown_outline(filename)
|
166 |
|
167 |
if st.button(f"πΎ Save {filename}"):
|
168 |
-
if save_file_content(
|
169 |
st.success(f"β¨ Saved {filename} successfully!")
|
170 |
|
171 |
if __name__ == "__main__":
|
|
|
6 |
from datetime import datetime
|
7 |
import re
|
8 |
import pandas as pd
|
|
|
9 |
import yaml
|
10 |
from io import StringIO
|
11 |
import openpyxl
|
12 |
import csv
|
13 |
|
14 |
+
# State initialization
|
15 |
if 'file_data' not in st.session_state:
|
16 |
st.session_state.file_data = {}
|
17 |
if 'file_types' not in st.session_state:
|
18 |
+
st.session_state.file_types = {}
|
19 |
if 'md_outline' not in st.session_state:
|
20 |
+
st.session_state.md_outline = {}
|
21 |
+
if 'rendered_content' not in st.session_state:
|
22 |
+
st.session_state.rendered_content = {}
|
23 |
|
24 |
+
# Supported file types and their handlers
|
25 |
FILE_TYPES = {
|
26 |
"md": "π Markdown",
|
27 |
"txt": "π Text",
|
|
|
33 |
}
|
34 |
|
35 |
def parse_markdown_outline(content):
|
36 |
+
"""Generate an outline from markdown content"""
|
37 |
lines = content.split('\n')
|
38 |
outline = []
|
39 |
for line in lines:
|
40 |
if line.strip().startswith('#'):
|
41 |
+
level = len(line.split()[0])
|
42 |
title = line.strip('#').strip()
|
43 |
outline.append({
|
44 |
'level': level,
|
|
|
47 |
})
|
48 |
return outline
|
49 |
|
50 |
+
def create_markdown_tabs(content, filename):
|
51 |
+
"""Create tabs for markdown content viewing and editing"""
|
52 |
+
tab1, tab2 = st.tabs(["π Editor", "π Preview"])
|
53 |
+
|
54 |
+
with tab1:
|
55 |
+
edited_content = st.text_area(
|
56 |
+
"Edit your markdown",
|
57 |
+
content,
|
58 |
+
height=300,
|
59 |
+
key=f"edit_{filename}"
|
60 |
+
)
|
61 |
+
|
62 |
+
if edited_content != content:
|
63 |
+
st.session_state.file_data[filename] = edited_content
|
64 |
+
st.session_state.md_outline[filename] = parse_markdown_outline(edited_content)
|
65 |
+
content = edited_content
|
66 |
+
|
67 |
+
with tab2:
|
68 |
+
st.markdown("### Preview")
|
69 |
+
st.markdown(content)
|
70 |
+
|
71 |
+
if filename in st.session_state.md_outline:
|
72 |
+
st.markdown("---")
|
73 |
+
st.markdown("### π Document Outline")
|
74 |
+
for item in st.session_state.md_outline[filename]:
|
75 |
+
st.markdown(f"{item['indent']}β’ {item['title']}")
|
76 |
+
|
77 |
+
return content
|
78 |
+
|
79 |
def read_file_content(uploaded_file):
|
80 |
+
"""Smart file reader with enhanced markdown handling"""
|
81 |
file_type = uploaded_file.name.split('.')[-1].lower()
|
82 |
try:
|
83 |
if file_type == 'md':
|
84 |
content = uploaded_file.getvalue().decode()
|
85 |
st.session_state.md_outline[uploaded_file.name] = parse_markdown_outline(content)
|
86 |
+
st.session_state.rendered_content[uploaded_file.name] = content
|
87 |
return content, "md"
|
88 |
|
89 |
elif file_type == 'csv':
|
|
|
110 |
return None, None
|
111 |
|
112 |
def save_file_content(content, filename, file_type):
|
113 |
+
"""Smart file saver with enhanced markdown handling"""
|
114 |
try:
|
115 |
if file_type == "md":
|
116 |
with open(filename, 'w') as f:
|
117 |
f.write(content)
|
118 |
+
st.session_state.rendered_content[filename] = content
|
119 |
|
120 |
elif file_type in ["csv", "xlsx"]:
|
|
|
121 |
df = pd.read_csv(StringIO(content)) if file_type == "csv" else pd.read_excel(StringIO(content))
|
122 |
if file_type == "csv":
|
123 |
df.to_csv(filename, index=False)
|
|
|
141 |
st.error(f"π¨ Error saving {filename}: {str(e)}")
|
142 |
return False
|
143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
def main():
|
145 |
st.title("πβ¨ Super Smart File Handler with Markdown Magic! β¨π")
|
146 |
|
|
|
147 |
uploaded_file = st.file_uploader(
|
148 |
"π€ Upload your file!",
|
149 |
type=list(FILE_TYPES.keys()),
|
|
|
156 |
st.session_state.file_data[uploaded_file.name] = content
|
157 |
st.session_state.file_types[uploaded_file.name] = file_type
|
158 |
st.success(f"π Loaded {FILE_TYPES.get(file_type, 'π')} file: {uploaded_file.name}")
|
|
|
|
|
|
|
|
|
|
|
159 |
|
|
|
160 |
if st.session_state.file_data:
|
161 |
st.subheader("π Your Files")
|
162 |
for filename, content in st.session_state.file_data.items():
|
163 |
file_type = st.session_state.file_types[filename]
|
164 |
with st.expander(f"{FILE_TYPES.get(file_type, 'π')} {filename}"):
|
165 |
+
if file_type == "md":
|
166 |
+
content = create_markdown_tabs(content, filename)
|
167 |
+
else:
|
168 |
+
edited_content = st.text_area(
|
169 |
+
"Content",
|
170 |
+
content,
|
171 |
+
height=300,
|
172 |
+
key=f"edit_{filename}"
|
173 |
+
)
|
174 |
+
if edited_content != content:
|
175 |
+
st.session_state.file_data[filename] = edited_content
|
176 |
+
content = edited_content
|
|
|
177 |
|
178 |
if st.button(f"πΎ Save {filename}"):
|
179 |
+
if save_file_content(content, filename, file_type):
|
180 |
st.success(f"β¨ Saved {filename} successfully!")
|
181 |
|
182 |
if __name__ == "__main__":
|