Hi! I need some help with my streamlit app. I have a local zip file and want to add a link to download in my app. Can someone help me with this?, i found some codes but none is working to me, Thanks in advance!
I also face this problem, and need a good solution.
I tried the solution of like this:
with open(zip_path, ārbā) as f:
bytes = f.read()
b64 = base64.b64encode(bytes).decode()
href = f'<a href="data:file/zip;base64,{b64}" download=\'{filename}.zip\'>\
Click to download\
</a>'
st.sidebar.markdown(href, unsafe_allow_html=True)
this solution will report RuntimeError:Data of size 280MB exceeds write limit of 50MB.
related topic link:
How to download file in streamlit - Using Streamlit - Streamlit
After dinner, I found a perfect solution that without download size limitation to download local file on server.
for example:
if our server IP is 1.2.3.4 or domain name is www.a.com
and our local file short path is /Q/W/example.7z
we can solve the problem like this:
import streamlit as st
import streamlit.components.v1 as components
#use ip
components.iframe("1.2.3.4"+"/Q/W/example.7z")
#use domain name
components.iframe("https://www.a.com"+"/Q/W/example.7z")
we also can save the short path of files under a specific local directory
and save the short path into a database
in that way, we can build a local search and download share platform.
Hi @diegovct and @BeyondMyself
With Streamlit version 0.88.0, you can use st.download_button
If your zip file, say myfile.zip
, is in the same location as your app:
import streamlit as st
with open("myfile.zip", "rb") as fp:
btn = st.download_button(
label="Download ZIP",
data=fp,
file_name="myfile.zip",
mime="application/zip"
)
If itās in a folder, say dir/myfile.zip
, you can download from a file pointer:
import streamlit as st
with open("dir/myfile.zip", "rb") as fp:
btn = st.download_button(
label="Download ZIP",
data=fp,
file_name="myfile.zip",
mime="application/zip"
)
Happy Streamlitāing!
Snehan
thanks for your share.
I found the st.download_button is not support downloading rar and 7z format file.
It will report 404: Not Found.
Do we have solution to solve this problem?
Hi @BeyondMyself,
The value for the mime
argument in st.download_button
depends on the file type.
File | MIME type |
---|---|
.zip | application/zip |
.rar | application/octet-stream |
.7z | application/octet-stream |
Have you tried using the appropriate MIME type?
Best,
Snehan
it was not work.
for rar file, I used the code:
with open("myfile.rar", "rb") as fp:
btn = st.download_button(
label="Download RAR",
data=fp,
file_name="myfile.rar",
mime="application/vnd.rar"
)
for 7z file, I used the code:
with open("myfile.7z", "rb") as fp:
btn = st.download_button(
label="Download 7z",
data=fp,
file_name="myfile.7z",
mime="application/x-7z-compressed"
)
Thanks for confirming, @BeyondMyself!
In these cases, we can assume the file to be downloaded is a binary file containing bytes
. As such, the appropriate MIME type for binary data is āapplication/octet-streamā.
Iāve updated my original answer with this correction. I am able to successfully download .rar
files using mime="application/octet-stream"
.
Does this help? Tagging @kajarenc who may have more insight into how st.download_button
handles these filetypes
Best,
Snehan
Yes, test tells us for zip file use mime=āapplication/zipā
for rar and 7z file need to use mime=āapplication/octet-streamā
so the last answer of mime type for rar and 7z is wrong.
Youāre right. Iāve updated the table in my last answer.
Thanks for your kindly help.
After change mime type, now 7z and rar file both can be downloaded successfully.
Hello @BeyondMyself!
This was a bug, and it has been fixed and will be available in the next release!
Thank you very much for your message!
OK, hope download zip, 7z, rar file useage will be clear for common users in next version release.