ElegantSolutions commited on
Commit
24cf5cc
·
verified ·
1 Parent(s): c464196

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -15
app.py CHANGED
@@ -11,25 +11,26 @@ from urllib.parse import quote_plus
11
  import base64
12
  import zipfile
13
  from datetime import datetime
 
14
 
15
 
16
  # -----------zip_and_auto_download helper function--------------
17
  def zip_and_auto_download(file_bytes, inner_filename, zip_prefix="Download"):
18
  """
19
- Wraps given bytes in a ZIP and auto-downloads it via base64 + fallback button.
 
 
20
  """
21
- # Create temp ZIP file
22
  zip_filename = f"{zip_prefix}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.zip"
23
  zip_file_path = tempfile.NamedTemporaryFile(delete=False, suffix=".zip").name
24
 
25
  with zipfile.ZipFile(zip_file_path, 'w') as zipf:
26
  zipf.writestr(inner_filename, file_bytes)
27
 
28
- # Read ZIP bytes
29
  with open(zip_file_path, "rb") as f:
30
  zip_bytes = f.read()
31
 
32
- # Fallback download button
33
  st.download_button(
34
  label=f"📥 Download {zip_filename}",
35
  data=zip_bytes,
@@ -37,18 +38,19 @@ def zip_and_auto_download(file_bytes, inner_filename, zip_prefix="Download"):
37
  mime="application/zip"
38
  )
39
 
40
- # Auto-download link
41
  b64 = base64.b64encode(zip_bytes).decode()
42
- href = f'<a href="data:application/zip;base64,{b64}" download="{zip_filename}" id="auto-download-link"></a>'
43
- st.markdown(href, unsafe_allow_html=True)
44
- st.markdown(
45
- """
46
- <script>
47
- document.getElementById('auto-download-link').click();
48
- </script>
49
- """,
50
- unsafe_allow_html=True
51
- )
 
52
 
53
  os.remove(zip_file_path)
54
 
 
11
  import base64
12
  import zipfile
13
  from datetime import datetime
14
+ import streamlit.components.v1 as components
15
 
16
 
17
  # -----------zip_and_auto_download helper function--------------
18
  def zip_and_auto_download(file_bytes, inner_filename, zip_prefix="Download"):
19
  """
20
+ Wraps given bytes in a ZIP and provides:
21
+ - Fallback button
22
+ - Guaranteed auto-download using components.html
23
  """
 
24
  zip_filename = f"{zip_prefix}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.zip"
25
  zip_file_path = tempfile.NamedTemporaryFile(delete=False, suffix=".zip").name
26
 
27
  with zipfile.ZipFile(zip_file_path, 'w') as zipf:
28
  zipf.writestr(inner_filename, file_bytes)
29
 
 
30
  with open(zip_file_path, "rb") as f:
31
  zip_bytes = f.read()
32
 
33
+ # Visible fallback
34
  st.download_button(
35
  label=f"📥 Download {zip_filename}",
36
  data=zip_bytes,
 
38
  mime="application/zip"
39
  )
40
 
41
+ # Auto download using components.html
42
  b64 = base64.b64encode(zip_bytes).decode()
43
+ html = f"""
44
+ <html>
45
+ <body>
46
+ <a id="auto-download-link" href="data:application/zip;base64,{b64}" download="{zip_filename}"></a>
47
+ <script>
48
+ document.getElementById('auto-download-link').click();
49
+ </script>
50
+ </body>
51
+ </html>
52
+ """
53
+ components.html(html, height=0, width=0)
54
 
55
  os.remove(zip_file_path)
56