Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,7 +11,7 @@ from io import StringIO
|
|
11 |
import openpyxl
|
12 |
import csv
|
13 |
|
14 |
-
#
|
15 |
if 'file_data' not in st.session_state:
|
16 |
st.session_state.file_data = {}
|
17 |
if 'file_types' not in st.session_state:
|
@@ -20,6 +20,10 @@ 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 = {
|
@@ -32,6 +36,60 @@ FILE_TYPES = {
|
|
32 |
"xml": "π XML"
|
33 |
}
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
def parse_markdown_outline(content):
|
36 |
"""Generate an outline from markdown content"""
|
37 |
lines = content.split('\n')
|
@@ -49,7 +107,7 @@ def parse_markdown_outline(content):
|
|
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(
|
@@ -62,6 +120,7 @@ def create_markdown_tabs(content, filename):
|
|
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:
|
@@ -74,6 +133,9 @@ def create_markdown_tabs(content, filename):
|
|
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):
|
@@ -84,6 +146,7 @@ def read_file_content(uploaded_file):
|
|
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':
|
@@ -116,6 +179,7 @@ def save_file_content(content, filename, file_type):
|
|
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))
|
@@ -150,6 +214,9 @@ def main():
|
|
150 |
help="Supports: " + ", ".join([f"{v} (.{k})" for k, v in FILE_TYPES.items()])
|
151 |
)
|
152 |
|
|
|
|
|
|
|
153 |
if uploaded_file:
|
154 |
content, file_type = read_file_content(uploaded_file)
|
155 |
if content is not None:
|
|
|
11 |
import openpyxl
|
12 |
import csv
|
13 |
|
14 |
+
# Enhanced 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:
|
|
|
20 |
st.session_state.md_outline = {}
|
21 |
if 'rendered_content' not in st.session_state:
|
22 |
st.session_state.rendered_content = {}
|
23 |
+
if 'file_history' not in st.session_state:
|
24 |
+
st.session_state.file_history = [] # List to store file history
|
25 |
+
if 'md_versions' not in st.session_state:
|
26 |
+
st.session_state.md_versions = {} # Dictionary to store versions of each file
|
27 |
|
28 |
# Supported file types and their handlers
|
29 |
FILE_TYPES = {
|
|
|
36 |
"xml": "π XML"
|
37 |
}
|
38 |
|
39 |
+
def add_to_history(filename, content, action="uploaded"):
|
40 |
+
"""Add a file action to the history with timestamp"""
|
41 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
42 |
+
history_entry = {
|
43 |
+
"timestamp": timestamp,
|
44 |
+
"filename": filename,
|
45 |
+
"action": action,
|
46 |
+
"content": content
|
47 |
+
}
|
48 |
+
st.session_state.file_history.insert(0, history_entry)
|
49 |
+
|
50 |
+
# Store version if it's a markdown file
|
51 |
+
if filename.endswith('.md'):
|
52 |
+
if filename not in st.session_state.md_versions:
|
53 |
+
st.session_state.md_versions[filename] = []
|
54 |
+
st.session_state.md_versions[filename].append({
|
55 |
+
"timestamp": timestamp,
|
56 |
+
"content": content,
|
57 |
+
"action": action
|
58 |
+
})
|
59 |
+
|
60 |
+
def show_file_history():
|
61 |
+
"""Display the file history in a collapsible section"""
|
62 |
+
if st.session_state.file_history:
|
63 |
+
with st.expander("π File History", expanded=False):
|
64 |
+
st.markdown("### Recent File Activities")
|
65 |
+
for entry in st.session_state.file_history:
|
66 |
+
col1, col2 = st.columns([2, 3])
|
67 |
+
with col1:
|
68 |
+
st.markdown(f"**{entry['timestamp']}**")
|
69 |
+
with col2:
|
70 |
+
st.markdown(f"{entry['action'].title()}: {entry['filename']}")
|
71 |
+
|
72 |
+
def show_markdown_versions(filename):
|
73 |
+
"""Display previous versions of a markdown file"""
|
74 |
+
if filename in st.session_state.md_versions:
|
75 |
+
versions = st.session_state.md_versions[filename]
|
76 |
+
if len(versions) > 1:
|
77 |
+
st.markdown("### π Previous Versions")
|
78 |
+
for idx, version in enumerate(versions[:-1], 1):
|
79 |
+
with st.expander(f"Version {len(versions) - idx} - {version['timestamp']}"):
|
80 |
+
st.text_area(
|
81 |
+
"Content",
|
82 |
+
version['content'],
|
83 |
+
height=200,
|
84 |
+
key=f"version_{filename}_{idx}",
|
85 |
+
disabled=True
|
86 |
+
)
|
87 |
+
if st.button(f"Restore to this version", key=f"restore_{filename}_{idx}"):
|
88 |
+
st.session_state.file_data[filename] = version['content']
|
89 |
+
st.session_state.md_outline[filename] = parse_markdown_outline(version['content'])
|
90 |
+
add_to_history(filename, version['content'], "restored")
|
91 |
+
st.experimental_rerun()
|
92 |
+
|
93 |
def parse_markdown_outline(content):
|
94 |
"""Generate an outline from markdown content"""
|
95 |
lines = content.split('\n')
|
|
|
107 |
|
108 |
def create_markdown_tabs(content, filename):
|
109 |
"""Create tabs for markdown content viewing and editing"""
|
110 |
+
tab1, tab2, tab3 = st.tabs(["π Editor", "π Preview", "π History"])
|
111 |
|
112 |
with tab1:
|
113 |
edited_content = st.text_area(
|
|
|
120 |
if edited_content != content:
|
121 |
st.session_state.file_data[filename] = edited_content
|
122 |
st.session_state.md_outline[filename] = parse_markdown_outline(edited_content)
|
123 |
+
add_to_history(filename, edited_content, "edited")
|
124 |
content = edited_content
|
125 |
|
126 |
with tab2:
|
|
|
133 |
for item in st.session_state.md_outline[filename]:
|
134 |
st.markdown(f"{item['indent']}β’ {item['title']}")
|
135 |
|
136 |
+
with tab3:
|
137 |
+
show_markdown_versions(filename)
|
138 |
+
|
139 |
return content
|
140 |
|
141 |
def read_file_content(uploaded_file):
|
|
|
146 |
content = uploaded_file.getvalue().decode()
|
147 |
st.session_state.md_outline[uploaded_file.name] = parse_markdown_outline(content)
|
148 |
st.session_state.rendered_content[uploaded_file.name] = content
|
149 |
+
add_to_history(uploaded_file.name, content)
|
150 |
return content, "md"
|
151 |
|
152 |
elif file_type == 'csv':
|
|
|
179 |
with open(filename, 'w') as f:
|
180 |
f.write(content)
|
181 |
st.session_state.rendered_content[filename] = content
|
182 |
+
add_to_history(filename, content, "saved")
|
183 |
|
184 |
elif file_type in ["csv", "xlsx"]:
|
185 |
df = pd.read_csv(StringIO(content)) if file_type == "csv" else pd.read_excel(StringIO(content))
|
|
|
214 |
help="Supports: " + ", ".join([f"{v} (.{k})" for k, v in FILE_TYPES.items()])
|
215 |
)
|
216 |
|
217 |
+
# Show file history at the top
|
218 |
+
show_file_history()
|
219 |
+
|
220 |
if uploaded_file:
|
221 |
content, file_type = read_file_content(uploaded_file)
|
222 |
if content is not None:
|