Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -72,9 +72,11 @@ def is_text_file(file_path):
|
|
| 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
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
| 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:
|
|
@@ -82,21 +84,24 @@ def file_expander(file_path):
|
|
| 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
|
| 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 |
-
|
|
|
|
|
|
|
| 96 |
st.markdown(get_download_link(file_path), unsafe_allow_html=True)
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
|
|
|
| 100 |
os.remove(file_path)
|
| 101 |
st.success(f"File {os.path.basename(file_path)} deleted!")
|
| 102 |
# Update the listing by removing the deleted file
|
|
@@ -107,7 +112,7 @@ def show_download_links(subdir):
|
|
| 107 |
for file in list_files(subdir):
|
| 108 |
file_path = os.path.join(subdir, file)
|
| 109 |
if os.path.isfile(file_path):
|
| 110 |
-
|
| 111 |
|
| 112 |
|
| 113 |
|
|
|
|
| 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_column(file_path):
|
| 76 |
+
col1, col2, col3 = st.columns([3, 1, 1])
|
| 77 |
+
|
| 78 |
+
# Column 1: File content or info
|
| 79 |
+
with col1:
|
| 80 |
if is_text_file(file_path):
|
| 81 |
file_content = ''
|
| 82 |
if 'edit_content' in st.session_state and st.session_state['edit_content'][0] == file_path:
|
|
|
|
| 84 |
else:
|
| 85 |
with open(file_path, "r", encoding='utf-8') as f:
|
| 86 |
file_content = f.read()
|
| 87 |
+
|
| 88 |
+
edited_content = st.text_area(f"Edit {os.path.basename(file_path)}:", value=file_content, height=250, key=f"textarea_{file_path}")
|
| 89 |
if st.button("💾 Save", key=f"save_{file_path}"):
|
| 90 |
with open(file_path, "w", encoding='utf-8') as f:
|
| 91 |
f.write(edited_content)
|
| 92 |
st.success(f"File {os.path.basename(file_path)} saved!")
|
| 93 |
st.session_state['edit_content'] = (file_path, edited_content)
|
| 94 |
else:
|
| 95 |
+
st.info(f"This is a binary file ({os.path.basename(file_path)}) and cannot be edited.")
|
| 96 |
|
| 97 |
+
# Column 2: Download link
|
| 98 |
+
with col2:
|
| 99 |
+
st.markdown("Download")
|
| 100 |
st.markdown(get_download_link(file_path), unsafe_allow_html=True)
|
| 101 |
|
| 102 |
+
# Column 3: Delete button
|
| 103 |
+
with col3:
|
| 104 |
+
if st.button(f"🗑️ Delete", key=f"delete_{file_path}"):
|
| 105 |
os.remove(file_path)
|
| 106 |
st.success(f"File {os.path.basename(file_path)} deleted!")
|
| 107 |
# Update the listing by removing the deleted file
|
|
|
|
| 112 |
for file in list_files(subdir):
|
| 113 |
file_path = os.path.join(subdir, file)
|
| 114 |
if os.path.isfile(file_path):
|
| 115 |
+
file_column(file_path)
|
| 116 |
|
| 117 |
|
| 118 |
|