Rerun the script immediately.
When st.rerun() is called, Streamlit halts the current script run and executes no further statements. Streamlit immediately queues the script to rerun.
When using st.rerun in a fragment, you can scope the rerun to the fragment. However, if a fragment is running as part of a full-app rerun, a fragment-scoped rerun is not allowed.
Function signature[source] | |
---|---|
st.rerun(*, scope="app") | |
Parameters | |
scope ("app" or "fragment") | Specifies what part of the app should rerun. If scope is "app" (default), the full app reruns. If scope is "fragment", Streamlit only reruns the fragment from which this command is called. Setting scope="fragment" is only valid inside a fragment during a fragment rerun. If st.rerun(scope="fragment") is called during a full-app rerun or outside of a fragment, Streamlit will raise a StreamlitAPIException. |
st.rerun
Caveats for
st.rerun
is one of the tools to control the logic of your app. While it is great for prototyping, there can be adverse side effects:
- Additional script runs may be inefficient and slower.
- Excessive reruns may complicate your app's logic and be harder to follow.
- If misused, infinite looping may crash your app.
In many cases where st.rerun
works, callbacks may be a cleaner alternative. Containers may also be helpful.
A simple example in three variations
st.rerun
to update an earlier header
Using import streamlit as st
if "value" not in st.session_state:
st.session_state.value = "Title"
##### Option using st.rerun #####
st.header(st.session_state.value)
if st.button("Foo"):
st.session_state.value = "Foo"
st.rerun()
Using a callback to update an earlier header
##### Option using a callback #####
st.header(st.session_state.value)
def update_value():
st.session_state.value = "Bar"
st.button("Bar", on_click=update_value)
Using containers to update an earlier header
##### Option using a container #####
container = st.container()
if st.button("Baz"):
st.session_state.value = "Baz"
container.header(st.session_state.value)
Still have questions?
Our forums are full of helpful information and Streamlit experts.