Option for lazy evaluation st.download_button

Hi there st team!

I want to give the user an option to download an image via st.download_button, but only render the image in case the button is clicked (expensive render). I was thinking there should be some option so that only clicking the button evaluates what would be downloaded, e.g.

st.download_button(label="Download image", data=render_function, file_name="image.png", lazy=True)

The only workaround I can think off right now is sticking the download-button inside a regular button (obviously super ugly), or a custom download button implementation. Any other ideas?

4 Likes

I’m stuck with same issue. Any news on this?

The download button needs to be passed the data upon rendering the button, so if you want to delay the gathering/compiling of that data then you may be forced into using a custom component or script injection.

Here is a post about scripting a download from just before the download button existed so you can modify it to your needs:

Here’s the associated gist: autodownload-csv.py Ā· GitHub

And here’s a GitHub feature request you can vote on to get the requested functionality added to the download button in Streamlit: Defered data for download button Ā· Issue #5053 Ā· streamlit/streamlit Ā· GitHub

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.