awacke1 commited on
Commit
53afd28
·
1 Parent(s): b70740a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -13
app.py CHANGED
@@ -6,6 +6,7 @@ import base64
6
  from bs4 import BeautifulSoup
7
  import hashlib
8
  import json
 
9
 
10
  EXCLUDED_FILES = ['app.py', 'requirements.txt', 'pre-requirements.txt', 'packages.txt', 'README.md','.gitattributes', "backup.py","Dockerfile"]
11
 
@@ -55,22 +56,38 @@ def get_download_link(file):
55
  href = f'<a href="data:file/octet-stream;base64,{b64}" download=\'{os.path.basename(file)}\'>Click to download {os.path.basename(file)}</a>'
56
  return href
57
 
 
 
 
 
 
 
 
 
 
 
 
58
  def file_expander(file_path):
59
  with st.expander(os.path.basename(file_path)):
60
- # Edit functionality
61
- file_content = ''
62
- if 'edit_content' in st.session_state and st.session_state['edit_content'][0] == file_path:
63
- file_content = st.session_state['edit_content'][1]
64
  else:
65
- with open(file_path, "r") as f:
66
- file_content = f.read()
67
-
68
- edited_content = st.text_area("Edit file content:", value=file_content, height=250, key=f"textarea_{file_path}")
69
- if st.button("💾 Save", key=f"save_{file_path}"):
70
- with open(file_path, "w") as f:
71
- f.write(edited_content)
72
- st.success(f"File {os.path.basename(file_path)} saved!")
73
- st.session_state['edit_content'] = (file_path, edited_content)
 
 
 
 
 
74
 
75
  # Download link
76
  st.markdown(get_download_link(file_path), unsafe_allow_html=True)
 
6
  from bs4 import BeautifulSoup
7
  import hashlib
8
  import json
9
+ import mimetypes
10
 
11
  EXCLUDED_FILES = ['app.py', 'requirements.txt', 'pre-requirements.txt', 'packages.txt', 'README.md','.gitattributes', "backup.py","Dockerfile"]
12
 
 
56
  href = f'<a href="data:file/octet-stream;base64,{b64}" download=\'{os.path.basename(file)}\'>Click to download {os.path.basename(file)}</a>'
57
  return href
58
 
59
+
60
+ def is_binary(file_path):
61
+ """Determine if the given file is binary or text-based."""
62
+ try:
63
+ with open(file_path, 'rb') as f:
64
+ textchars = bytearray({7,8,9,10,12,13,27} | set(range(0x20, 0x100)) - {0x7f})
65
+ is_binary_string = lambda bytes: bool(bytes.translate(None, textchars))
66
+ return is_binary_string(f.read(1024))
67
+ except:
68
+ return True
69
+
70
  def file_expander(file_path):
71
  with st.expander(os.path.basename(file_path)):
72
+ # Check if file is binary
73
+ if is_binary(file_path):
74
+ st.info("This is a binary file and cannot be edited.")
75
+ # Only display download and delete options for binary files
76
  else:
77
+ # Text editing functionality for non-binary files
78
+ file_content = ''
79
+ if 'edit_content' in st.session_state and st.session_state['edit_content'][0] == file_path:
80
+ file_content = st.session_state['edit_content'][1]
81
+ else:
82
+ with open(file_path, "r") as f:
83
+ file_content = f.read()
84
+
85
+ edited_content = st.text_area("Edit file content:", value=file_content, height=250, key=f"textarea_{file_path}")
86
+ if st.button("💾 Save", key=f"save_{file_path}"):
87
+ with open(file_path, "w") as f:
88
+ f.write(edited_content)
89
+ st.success(f"File {os.path.basename(file_path)} saved!")
90
+ st.session_state['edit_content'] = (file_path, edited_content)
91
 
92
  # Download link
93
  st.markdown(get_download_link(file_path), unsafe_allow_html=True)