Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,7 @@ import yaml
|
|
10 |
from io import StringIO
|
11 |
import openpyxl
|
12 |
import csv
|
|
|
13 |
|
14 |
# Enhanced state initialization
|
15 |
if 'file_data' not in st.session_state:
|
@@ -21,29 +22,27 @@ if 'md_outline' not in st.session_state:
|
|
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 = []
|
25 |
if 'md_versions' not in st.session_state:
|
26 |
-
st.session_state.md_versions = {}
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
"
|
34 |
-
|
35 |
-
"yaml": "βοΈ YAML",
|
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":
|
47 |
}
|
48 |
st.session_state.file_history.insert(0, history_entry)
|
49 |
|
@@ -53,7 +52,7 @@ def add_to_history(filename, content, action="uploaded"):
|
|
53 |
st.session_state.md_versions[filename] = []
|
54 |
st.session_state.md_versions[filename].append({
|
55 |
"timestamp": timestamp,
|
56 |
-
"content":
|
57 |
"action": action
|
58 |
})
|
59 |
|
@@ -75,20 +74,27 @@ def show_markdown_versions(filename):
|
|
75 |
versions = st.session_state.md_versions[filename]
|
76 |
if len(versions) > 1:
|
77 |
st.markdown("### π Previous Versions")
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
def parse_markdown_outline(content):
|
94 |
"""Generate an outline from markdown content"""
|
|
|
10 |
from io import StringIO
|
11 |
import openpyxl
|
12 |
import csv
|
13 |
+
import base64
|
14 |
|
15 |
# Enhanced state initialization
|
16 |
if 'file_data' not in st.session_state:
|
|
|
22 |
if 'rendered_content' not in st.session_state:
|
23 |
st.session_state.rendered_content = {}
|
24 |
if 'file_history' not in st.session_state:
|
25 |
+
st.session_state.file_history = []
|
26 |
if 'md_versions' not in st.session_state:
|
27 |
+
st.session_state.md_versions = {}
|
28 |
|
29 |
+
def encode_content(content):
|
30 |
+
"""Encode content to base64"""
|
31 |
+
return base64.b64encode(content.encode()).decode()
|
32 |
+
|
33 |
+
def decode_content(encoded_content):
|
34 |
+
"""Decode content from base64"""
|
35 |
+
return base64.b64decode(encoded_content.encode()).decode()
|
|
|
|
|
|
|
36 |
|
37 |
def add_to_history(filename, content, action="uploaded"):
|
38 |
"""Add a file action to the history with timestamp"""
|
39 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
40 |
+
encoded_content = encode_content(content)
|
41 |
history_entry = {
|
42 |
"timestamp": timestamp,
|
43 |
"filename": filename,
|
44 |
"action": action,
|
45 |
+
"content": encoded_content
|
46 |
}
|
47 |
st.session_state.file_history.insert(0, history_entry)
|
48 |
|
|
|
52 |
st.session_state.md_versions[filename] = []
|
53 |
st.session_state.md_versions[filename].append({
|
54 |
"timestamp": timestamp,
|
55 |
+
"content": encoded_content,
|
56 |
"action": action
|
57 |
})
|
58 |
|
|
|
74 |
versions = st.session_state.md_versions[filename]
|
75 |
if len(versions) > 1:
|
76 |
st.markdown("### π Previous Versions")
|
77 |
+
version_idx = st.selectbox(
|
78 |
+
"Select version to view",
|
79 |
+
range(len(versions)-1),
|
80 |
+
format_func=lambda x: f"Version {len(versions)-1-x} - {versions[x]['timestamp']}"
|
81 |
+
)
|
82 |
+
|
83 |
+
if version_idx is not None:
|
84 |
+
version = versions[version_idx]
|
85 |
+
decoded_content = decode_content(version['content'])
|
86 |
+
st.text_area(
|
87 |
+
"Content",
|
88 |
+
decoded_content,
|
89 |
+
height=200,
|
90 |
+
key=f"version_{filename}_{version_idx}",
|
91 |
+
disabled=True
|
92 |
+
)
|
93 |
+
if st.button(f"Restore to this version", key=f"restore_{filename}_{version_idx}"):
|
94 |
+
st.session_state.file_data[filename] = decoded_content
|
95 |
+
st.session_state.md_outline[filename] = parse_markdown_outline(decoded_content)
|
96 |
+
add_to_history(filename, decoded_content, "restored")
|
97 |
+
st.experimental_rerun()
|
98 |
|
99 |
def parse_markdown_outline(content):
|
100 |
"""Generate an outline from markdown content"""
|