Search code examples
pythonwindowspyinstallerstreamlitpathlib

How to download exe file from Streamlit?


I made a .py script converted it to a .exe file to make a windows desktop app using pyinstaller. I did this:

pyinstaller --onefile -w test.py

How can a user download this app using Streamlit? Specifically using Streamlit download button.

I tried st.download_button('Download app', data='app.exe', file_name='App.exe')

It won't work and the windows computer gives a blue box error (like when your battery is low) that say can't run this app. Probably because the argument data is a string not a file directory so I tried:

from pathlib import Path
exe_path = "drowsy_setup.exe"
path = Path(exe_path)
st.download_button('Download app', data=path, file_name='Drowsy_App.exe')

I added pathlib to requirements.txt file but it gives a error:

RuntimeError: This app has encountered an error. The original error message is redacted to prevent data leaks. Full error details have been recorded in the logs.
Traceback:
File "/home/appuser/venv/lib/python3.7/site-packages/streamlit/script_runner.py", line 354, in _run_script
    exec(code, module.__dict__)
File "/app/website-desktop-app/test.py", line 15, in <module>
    st.download_button('Download app', data=path, file_name='Drowsy_App.exe')
File "/home/appuser/venv/lib/python3.7/site-packages/streamlit/elements/button.py", line 212, in download_button
    self.dg._get_delta_path_str(), data, download_button_proto, mime, file_name
File "/home/appuser/venv/lib/python3.7/site-packages/streamlit/elements/button.py", line 313, in marshall_file
    raise RuntimeError("Invalid binary data format: %s" % type(data))

I tried doing it without pathlib in requirements.txt as it is a default python library but it gives the same error. How do I fix this error? Is there a better way to download a desktop app using Streamlit?

versions: python(3.7), streamlit(1.3.0)


Solution

  • Try the following see also the doc.

    binary_file = 'Drowsy_App.exe'
    
    with open(binary_file, "rb") as file:
         btn = st.download_button(
             label="Download Drowsy_App.exe",
             data=file,
             file_name="Drowsy_App.exe",
             mime="application/octet-stream"
    )