Download button without storing data in memory

Hi all,

I’m trying to implement a feature to allow users to download a file from a URL.
The download button provided by streamlit works well in terms of when the user presses it the file gets downloaded to their device through the browser. But the fact that it pre-downloads all the files to the memory upon starting the application makes the application start really slow as I have provided many large files to be downloaded.

I tried using the regular button pointing to a piece of code that does ‘file = request.get(url)’, ‘open(localpath,‘wb’) as f’ and ‘f.write(file)’.
This downloads from URL only when the user presses the button which is good without the predownloading. But I’ve been struggling to make it download to the user’s local device. Currently when the button is pressed, the files always get downloaded to my machine which is hosting the app.

Can someone please provide any insight? Is there a way to use the download button without pre downloading the files? Or is there a way to retrieve information from the user end and write the file to their local folder?

Thanks!

1 Like

I think your best best would be to use html to create a download button/link if you want to avoid passing the data through the host. You can pass html to st.markdown as long as you use the optional keyword unsafe_allow_html=True.