awacke1 commited on
Commit
33501c7
Β·
verified Β·
1 Parent(s): a505c8a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -28
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 = [] # 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 = {
30
- "md": "πŸ“ Markdown",
31
- "txt": "πŸ“„ Text",
32
- "json": "πŸ”§ JSON",
33
- "csv": "πŸ“Š CSV",
34
- "xlsx": "πŸ“— Excel",
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": 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": 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
- 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"""
 
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"""