streamlit: Make sure that clicking on a download button does not reload the entire app
Problem
The Streamlit app reloads entirely when clicking on a st.download_button. In my case, I think it should not: download buttons are different from buttons and should not trigger a reload of the app. They should only trigger a download.
I perfectly understand that a click on a button does reload the app (to recompute) but downloads don’t change the view, downloads don’t trigger computations etc.
I also know about the cache, but it is not always a proper solution: what about apps with a st.forms? The user should not have to click on yet another button to generate the same view that has not changed one bit. The same goes if several files can be downloaded: it can be very cumbersome to do such a simple task.
Solution
Several options:
- Stop reloading the app for a
st.download_button - Add a parameter to trigger (or not) a reload of the page when clicking on a
st.download_button.
Additional context
See this discourse: https://discuss.streamlit.io/t/stop-page-from-reloading-after-using-downloader/18744
And this code to reproduce the problem exposed above:
import streamlit as st
def long_computation():
import time
time.sleep(10)
st.header("TEST DOWNLOAD BUTTON")
with st.spinner("Running a long computation.."):
long_computation()
st.download_button("Download", "a")
Community voting on feature requests enables the Streamlit team to understand which features are most important to our users.
If you’d like the Streamlit team to prioritize this feature request, please use the 👍 (thumbs up emoji) reaction in response to the initial post.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 177
- Comments: 53
Commits related to this issue
- Download button: Allow to not trigger rerun (#10296) ## Describe your changes Add the ability to specify the `on_click` parameter equal to `"rerun"` (by default) or `"ignore"` to not rerun the app wh... — committed to streamlit/streamlit by kajarenc 2 years ago
- Download button: Allow to not trigger rerun (#10296) ## Describe your changes Add the ability to specify the `on_click` parameter equal to `"rerun"` (by default) or `"ignore"` to not rerun the app wh... — committed to streamlit/streamlit by kajarenc 2 years ago
- Download button: Allow to not trigger rerun (#10296) ## Describe your changes Add the ability to specify the `on_click` parameter equal to `"rerun"` (by default) or `"ignore"` to not rerun the app wh... — committed to akramsystems/streamlit by kajarenc 2 years ago
Hey all! Just wanted to give a quick update: we are not actively working on this at the moment but it’s something we definitely want to do. The complication is that we probably need to find a solution that controls reruns across all interactive widget. Don’t want to do a one-off thing for download button and then have to revamp it once we want to give more control over reruns in other elements. I’ll update here once we have more news!
Update
We’ve been looking into this for the past few weeks. We probably just want to change the behavior of
st.download_buttonto not rerun the app anymore, so it works similar to the newst.link_button. Unfortunately, that’s a breaking change, so we’d have to wait until Streamlit 2.0 to do it. We’re thinking about introducing a config option in the meantime that would disable the rerun, so y’all are unblocked for now. Will update once we reached a final decision!@VolgardTheScientist Correct, this will not affect
st.button.It is in the roadmap now.
Hey! We just launched fragments in Streamlit, which offer at least a temporary workaround for this:
When clicking the download button, only the fragment function will rerun, and not the rest of the app. See the video below: the loading spinner only shows up on initial page load but not if I click the download button.
https://github.com/streamlit/streamlit/assets/5103165/d0246a97-48f0-43a6-b9c2-d1291f2b2719
This is just a workaround and we have a plan for a better, final solution, which lets you disable the rerun via the
on_clickparameter. But that is dependent on another feature we’re currently working on (chart selections), so it will still take us a bit to ship it.Following, I have faced this issue and would love a fix 😃
I have three files available for download on my Hugging Face App and it’s frustrating for the end user to have to run the application three times to be able to actually download each of those files.
+1 After initial enthusiasm, I went back to css styled hrefs in all of my use cases due to this.
Hi @jrieke - is there any progress on this? This functionality would be extremely useful. Thanks!
@GitHunter0 @jenitivecase Oh in case that wasn’t clear from my answer, we are only planning to change the rerun behavior of
st.download_button, not of other widgets. Updated the wording in my answer to make that more obvious.Great to know @jrieke , that will be a major contribution since this is the biggest drawback of Streamlit in my opinion. I’m cheering for it.
Update
Hey all! We’re currently working on this in #10296 and expect to ship it in the next release in early March. You’ll then be able to disable the rerun for download buttons by:
By default, download button will still rerun the page. We might change this default behavior at a later point in time but since it’s a breaking change, we’d want to wait until we ship a major version.
+1 I understand that the Streamlit team has their design principles but reloading the app after clicking on download makes it unusable for me. The same applies to other widgets as well.
How about semi-solution heavily based on this:
I tried using st.experimental_frament but my page contents and I get bellow error: RuntimeError: Could not find fragment with id 94641ed4d7d52a980d1a8ddd789e1e12
This is the code snippet:
I tried Streamlit for the first time today. Man I was surprised how good it is. My app takes input and generates two downloadable files, a certificate and a key. CRASH BOOM. I can only download one file, because the page reloads… I guess I have to look elsewhere. A pitty .
+1 This is a top priority, Streamlit team. This is a serious flaw in Streamlit design that prevents many developers from adopting Streamlit for large scale projects.
+1 - last blocker for Streamlit vs. R Shiny here.
Is this the solution that made you close the other issue @jrieke ?