Updating state within a loop

Summary

As part of a callback I would like to update the contents of a container (have that displayed) do additional work, then update the contents again.

Additional information

This is an extremely easy and common workflow in react but seems extraordinarily hard in streamlit.

Basically I either want the app to update based on a logic induced state change, or have the ability to rerender at any point from within my callback logic.

st.experimental_rerun() appears to stop the flow of logic rather than just remotely triggering a react render (which is what I want).

Alternatively if there were a way to simply append to a given container rather than overwriting I could us this, however any time I call with container: container.write() from a callback it overwrites the contents of the container.

Updating the UI in callbacks often causes issues and I don’t see why you would need it. You can instead manipulate the state in the callback and then use the state to inform the rendering. Also there is nothing in your question that suggests a loop.

With the above in mind, this is how I think you can

update the contents of a container (have that displayed) do additional work, then update the contents again.

import time

if st.button("Update"):
    container = st.empty()
    container.write("provisional update")
    with st.spinner():
        time.sleep(2)  # do additional work
    container.write("final update")

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