Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -67,27 +67,30 @@ def is_binary(file_path):
|
|
67 |
except:
|
68 |
return True
|
69 |
|
|
|
|
|
|
|
|
|
|
|
70 |
def file_expander(file_path):
|
71 |
with st.expander(os.path.basename(file_path)):
|
72 |
-
#
|
73 |
-
if
|
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)
|
|
|
67 |
except:
|
68 |
return True
|
69 |
|
70 |
+
def is_text_file(file_path):
|
71 |
+
"""Check if a file is a text file."""
|
72 |
+
text_file_types = ['.txt', '.py', '.md', '.html', '.css', '.js', '.json', '.xml']
|
73 |
+
return any(file_path.endswith(ext) for ext in text_file_types)
|
74 |
+
|
75 |
def file_expander(file_path):
|
76 |
with st.expander(os.path.basename(file_path)):
|
77 |
+
# Handle text files
|
78 |
+
if is_text_file(file_path):
|
|
|
|
|
|
|
|
|
79 |
file_content = ''
|
80 |
if 'edit_content' in st.session_state and st.session_state['edit_content'][0] == file_path:
|
81 |
file_content = st.session_state['edit_content'][1]
|
82 |
else:
|
83 |
+
with open(file_path, "r", encoding='utf-8') as f:
|
84 |
file_content = f.read()
|
85 |
|
86 |
edited_content = st.text_area("Edit file content:", value=file_content, height=250, key=f"textarea_{file_path}")
|
87 |
if st.button("💾 Save", key=f"save_{file_path}"):
|
88 |
+
with open(file_path, "w", encoding='utf-8') as f:
|
89 |
f.write(edited_content)
|
90 |
st.success(f"File {os.path.basename(file_path)} saved!")
|
91 |
st.session_state['edit_content'] = (file_path, edited_content)
|
92 |
+
else:
|
93 |
+
st.info("This is a binary file and cannot be edited.")
|
94 |
|
95 |
# Download link
|
96 |
st.markdown(get_download_link(file_path), unsafe_allow_html=True)
|