Updating state variable doesn't rerun the app

Summary

I expect the app to rerun as soon as a state variable is changed. But that is not happening.

update_state("NAME", value)

This doesn’t rerun the app. The app only reruns after I interact with some widget and then the changes are visible according to the new state value.
Currently, I’m manually rerunning which works fine:

update_state("NAME", value)
st.experimental_rerun()

But I’m not sure if this is the correct/efficient way of doing things.

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Debug info

  • Streamlit version: version 1.24.1
  • Python version: 3.11
  • virtualenv
  • OS version: Mac 13.0
  • Browser version:

Requirements file

Hi there @AyushExel!

In Streamlit, updating a session state variable does not trigger a rerun.

What you are doing is correct (i.e. explicitly triggering a rerun), but if I can suggest an alternative, look into callback functions in widgets. That way you can update the state before the app rerun that is triggered whenever a user interacts with a widget.

Cool app by the way :rocket:

Thanks for quick reply @marduk . Glad you like it :slight_smile:
So you mean something like - st.button(..., on_click=update_state) ?
that seems nice! but can’t really pass the arguments to the function signature, so I was using:

button = st.button(...)
if button:
   update_state

So what’s the usual workflow in this case. There’s an update function for each widget hardcoded to that state variable?

1 Like

You actually can pass arguments to a widget callback, that is the trick :slight_smile:

Use the args parameter and pass an iterable with the parameters.

Ahh my brain somehow skips every ‘kwargs’ mention as not important :joy: . It was there the whole time. Neat touch. I’ll try that. Thanks

1 Like

@marduk If I may, I have one off topic question about streamlit.
So after looking at many apps, I’ve finally settled to streamlit because of multiple reasons. I’m not building my own backend+frontend because I want to iterate quickly. But I also I want to make this app usable(hosted locally by each user so nothing production heavy [for now]). The app will be data heavy(load 1000s of images in browser, even more) and the performance is key.

Up until now my experience has been seamless. There was one plugin that I had to redo a bit but other than that, I’m pretty impressed.
Now, my question is - is streamlit intended to be used for such applications? Or should I think if this as stretching a prototyping tool? I read some articles saying streamlit doesn’t have the best performance so it’s only good for prototyping. While it might be true, I have not had any major performance issues as I expand tabs and pages so I’m thinking to double down on it.

So, it’d be great if I could get a response from the team about a few things:

  • Where does supporting large data apps like this rank in the priority list?
  • What performance improvements are in the radar? (for example the re-rendering performance could be a bit better (see streamsync vs streamlit Like Streamlit, but fast. Enabling low-latency data apps. | by Ramiro Medina | Medium )
  • I’m happy to invest more time in streamlit and I also want to know if roadmap is to evolve the tool and its image from rapid protyping solution to no-code web solution for python. (See anvil)

Happy to hop on a call with the team to talk about by use cases in detail. I rarely come across tools that work exactly as expected. Very impressive work :slight_smile:

EDIT: I assumed you are from streamlit team. If not, please put me in touch with the right person if you know them :slight_smile:

Not part of Streamlit, but some thoughts in case useful:

  • You can keep track of upcoming features and performance improvements in the GitHub repo or the Streamlit roadmap.

  • No UI framework is perfect. Evaluate any choice in terms of:

    • Rendering performance (is the UI you want rendering quickly enough?)
    • Resource usage (are there any memory leaks?)
    • Configurability (can you create new widgets and styles?)
    • Security
    • Support
    • Iteration speed
  • In my opinion your app is a good use case for Streamlit because it is Python based, for internal use and run locally by each user (not deployed online, so you don’t have to worry as much about things like security), and you want to iterate quickly.

2 Likes

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