如何从Streamlit下载exe文件



我制作了一个.py脚本,将其转换为.exe文件,以使用pyinstaller制作一个windows桌面应用程序。我做到了:

pyinstaller --onefile -w test.py

用户如何使用Streamlit下载此应用程序?特别是使用Streamlight下载按钮。

我试过st.download_button('Download app', data='app.exe', file_name='App.exe')

它不起作用,windows计算机会显示一个蓝框错误(比如电池电量不足(,表示无法运行此应用程序。可能是因为参数data是字符串而不是文件目录,所以我尝试了:

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')

我在requirements.txt文件中添加了pathlib,但它给出了一个错误:

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))

我尝试在requirements.txt中不使用pathlib,因为它是默认的python库,但它给出了相同的错误。如何修复此错误?有没有更好的方法可以使用Streamlight下载桌面应用程序?

版本:python(3.7), streamlit(1.3.0)

尝试以下操作,另请参阅文档。

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"
)

最新更新