简体   繁体   English

如何从 Streamlit 下载 exe 文件?

[英]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.我制作了一个 .py 脚本,将其转换为 .exe 文件,以使用 pyinstaller 制作 windows 桌面应用程序。 I did this:我这样做了:

pyinstaller --onefile -w test.py

How can a user download this app using Streamlit?用户如何使用 Streamlit 下载此应用程序? Specifically using Streamlit download button.专门使用 Streamlit 下载按钮。

I tried st.download_button('Download app', data='app.exe', file_name='App.exe')我试过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.它不起作用,并且 windows 计算机出现蓝框错误(例如当您的电池电量不足时),表示无法运行此应用程序。 Probably because the argument data is a string not a file directory so I tried:可能是因为参数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')

I added pathlib to requirements.txt file but it gives a error:我将 pathlib 添加到 requirements.txt 文件,但它给出了一个错误:

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.我尝试在 requirements.txt 中不使用 pathlib,因为它是默认的 python 库,但它给出了相同的错误。 How do I fix this error?如何修复此错误? Is there a better way to download a desktop app using Streamlit?有没有更好的方法来使用 Streamlit 下载桌面应用程序?

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM