Not to run entire script when a widget value is changed?

Hi,

I started using Streamlit in one of my projects and immediately fell in love with it. But I got a problem that really bothers me, so any help would be greatly appreciated.

My code flows like this:

  1. Load data from files
  2. Do some computation and results populate a list
  3. When use selects an item from the list, some relevant text would be printed out as text.

My problem is that, every time I select a new item from the list, the entire script reruns from the beginning, including data loading and computation, which is uncalled for and time consuming.

How can I make it just update the text printing part when I choose a new item from the list?

Thanks in advance!

Richard

I guess thats a good use case for @st.cache, if you have a large chunk of code that only needs to run once (as long as the parameters don’t change) then wrap it in a function and decorate that function with @st.cache.
E.g.

@st.cache
def long_running_function(p1, p2, p3):
    #your long computation here

Documentation for st.cache is here
https://streamlit.io/docs/api.html#streamlit.cache

Thank you Alexandre!

This works, just a bit awkward when a script gets a little more complex. Hope Streamlit can be more flexible.

Beside the @st.cache the key is really how you structure you script. If you could somehow save the data from the long_running_computation when it’s done and the user could click a button to go to the next step/ page then you might be home free.

You can save to a temporary file or a database. That depends on use case. Whether you save the same file all the time (if only one user) or you need to save to a unique file every time and let the user select it on the next screen is up to you.

I’ve heard there is also work in progress on being able to save the state (for example the result of the calculation) between script reruns.

At work i’m working on an application that will be structured into three steps/ pages.

  1. Run long running backtest/ simulation and save results to file(s).
  2. Let user select file(s) and validate input/ output if needed.
  3. Let user select file(s) and provide interactive analytics to end user based on simulation results.

When state is implemented I can either save the results or the paths of the files so that the user does not need to select them.

@Richard_Xie, did you ever solve this? I’ve got a very similar issue. I have 10 inputs to a model that I allow my user to adjust, THEN they press a button to run the model (that way the model doesn’t try to run until every input has been updated). Once the model runs, I have a dataframe I display that I want to give the user the ability to filter, however, when I make a filter selection, it re-runs the script and resets back to before the button was pushed…

4 Likes

Did you figure something out @christianthieme ?

I made my own small app for work and noticed that any time I changed a slider by the tiniest amount, the whole page refreshed. I thought I was doing something wrong so did some googling and played around with other people’s work.

I found a similar streamlit app similar to yours, hosted on the streamlit share pages. Except here were 30 inputs to the model. Any time I changed a single slider the whole thing updated. Charts, predictions, dataframes etc. I checked the source code and it’s even downloading a dataset (with 100,000 rows) from a url every time I move a slider by even 1 pixel.

Then I googled the issue and found this thread.

As a side note it must be costing streamlit a lot of CPU time to host all these apps constantly refreshing with every touch.

@soundbeans, nope. I don’t think the functionality exists within Streamlit yet. It’s on their roadmap. For apps that require this I actually switched over to Dash. They have “State” implemented that solves this perfectly. Definitely more work – but ultimately what I need an more flexible.

I’ve tried Dash. There’s a steeper learning curve for me personally. I don’t really know enough HTML or CSS to make it look professional so I tried Streamlit, which is a bit more “magic” with the layout.

Thanks for the “state” tip. I’ll give Dash another try with this.

If you use dash bootstrap it abstracts a lot of the HTML and CSS… but definitely nothing close to what streamlit does :slight_smile:

Hi, I found this patch that worked for me that implements State within Streamlit: here. It has to be installed locally. See this topic

Hope it helps !