Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -48,23 +48,49 @@ def list_files(directory_path='.'):
|
|
48 |
files = [f for f in os.listdir(directory_path) if os.path.isfile(os.path.join(directory_path, f))]
|
49 |
return [f for f in files if f not in EXCLUDED_FILES]
|
50 |
|
51 |
-
def show_download_links(subdir):
|
52 |
-
st.write(f'Files for {subdir}:')
|
53 |
-
for file in list_files(subdir):
|
54 |
-
file_path = os.path.join(subdir, file)
|
55 |
-
if os.path.isfile(file_path):
|
56 |
-
st.markdown(get_download_link(file_path), unsafe_allow_html=True)
|
57 |
-
show_file_operations(file_path)
|
58 |
-
else:
|
59 |
-
st.write(f"File not found: {file}")
|
60 |
-
|
61 |
-
|
62 |
def get_download_link(file):
|
63 |
with open(file, "rb") as f:
|
64 |
bytes = f.read()
|
65 |
b64 = base64.b64encode(bytes).decode()
|
66 |
-
href = f'<a href="data:file/octet-stream;base64,{b64}" download=\'{os.path.basename(file)}\'>
|
67 |
return href
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
def generate_hash_key(path, counter):
|
69 |
"""Generate a unique hash key for a given file path and counter."""
|
70 |
return hashlib.md5(f"{path}_{counter}".encode()).hexdigest()
|
|
|
48 |
files = [f for f in os.listdir(directory_path) if os.path.isfile(os.path.join(directory_path, f))]
|
49 |
return [f for f in files if f not in EXCLUDED_FILES]
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
def get_download_link(file):
|
52 |
with open(file, "rb") as f:
|
53 |
bytes = f.read()
|
54 |
b64 = base64.b64encode(bytes).decode()
|
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)
|
77 |
+
|
78 |
+
# Delete button
|
79 |
+
if st.button(f"🗑️ Delete {os.path.basename(file_path)}", key=f"delete_{file_path}"):
|
80 |
+
os.remove(file_path)
|
81 |
+
st.success(f"File {os.path.basename(file_path)} deleted!")
|
82 |
+
# Update the listing by removing the deleted file
|
83 |
+
st.experimental_rerun()
|
84 |
+
|
85 |
+
def show_download_links(subdir):
|
86 |
+
st.write(f'Files in {subdir}:')
|
87 |
+
for file in list_files(subdir):
|
88 |
+
file_path = os.path.join(subdir, file)
|
89 |
+
if os.path.isfile(file_path):
|
90 |
+
file_expander(file_path)
|
91 |
+
|
92 |
+
|
93 |
+
|
94 |
def generate_hash_key(path, counter):
|
95 |
"""Generate a unique hash key for a given file path and counter."""
|
96 |
return hashlib.md5(f"{path}_{counter}".encode()).hexdigest()
|