Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -46,14 +46,27 @@ def zip_subdirs(start_dir):
|
|
46 |
yield zip_filename
|
47 |
|
48 |
def get_zip_download_link(zip_file):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
with open(zip_file, 'rb') as f:
|
50 |
bytes = f.read()
|
51 |
b64 = base64.b64encode(bytes).decode()
|
52 |
link_name = os.path.basename(zip_file)
|
53 |
-
href = f'<a href="data:file/zip;base64,{b64}" download="{link_name}">
|
54 |
return href
|
55 |
-
|
56 |
-
|
57 |
@st.cache_resource
|
58 |
def create_zip_of_files(files):
|
59 |
zip_name = "all_files.zip"
|
@@ -241,6 +254,7 @@ def main():
|
|
241 |
if st.sidebar.button("⬇️ Download All"):
|
242 |
start_directory = '.' # Current directory
|
243 |
for zip_file in zip_subdirs(start_directory):
|
|
|
244 |
st.sidebar.markdown(get_zip_download_link(zip_file), unsafe_allow_html=True)
|
245 |
|
246 |
# Expander for showing URL history and download links
|
|
|
46 |
yield zip_filename
|
47 |
|
48 |
def get_zip_download_link(zip_file):
|
49 |
+
# Count number of files in the zip
|
50 |
+
with zipfile.ZipFile(zip_file, 'r') as zipf:
|
51 |
+
num_files = len(zipf.namelist())
|
52 |
+
# Get the size of the zip file
|
53 |
+
zip_size = os.path.getsize(zip_file)
|
54 |
+
# Convert size to a more readable format (e.g., KB, MB)
|
55 |
+
if zip_size < 1024:
|
56 |
+
size_str = f"{zip_size} B"
|
57 |
+
elif zip_size < 1024**2:
|
58 |
+
size_str = f"{zip_size / 1024:.2f} KB"
|
59 |
+
else:
|
60 |
+
size_str = f"{zip_size / 1024**2:.2f} MB"
|
61 |
+
|
62 |
+
# Create the download link
|
63 |
with open(zip_file, 'rb') as f:
|
64 |
bytes = f.read()
|
65 |
b64 = base64.b64encode(bytes).decode()
|
66 |
link_name = os.path.basename(zip_file)
|
67 |
+
href = f'<a href="data:file/zip;base64,{b64}" download="{link_name}">{link_name} - {num_files} files, {size_str}</a>'
|
68 |
return href
|
69 |
+
|
|
|
70 |
@st.cache_resource
|
71 |
def create_zip_of_files(files):
|
72 |
zip_name = "all_files.zip"
|
|
|
254 |
if st.sidebar.button("⬇️ Download All"):
|
255 |
start_directory = '.' # Current directory
|
256 |
for zip_file in zip_subdirs(start_directory):
|
257 |
+
# Display enhanced download link with file count and size
|
258 |
st.sidebar.markdown(get_zip_download_link(zip_file), unsafe_allow_html=True)
|
259 |
|
260 |
# Expander for showing URL history and download links
|