liveBook · Manning

St.rerun

As explained in Build Python Web Apps with Streamlit

st.rerun is a Streamlit API function that stops the current script execution and starts a new execution from the beginning.

st.rerun() stops the current Streamlit execution and starts the app again from the top. It is useful after changing data so the interface immediately reflects the update. section 4.5.4

Refreshing after a deletion

After a Delete button removes a task, calling st.rerun() refreshes the displayed task list and removes the deleted item from the interface. section 4.5.4

Use reruns selectively:

  • Trigger the rerun only when an interaction actually changes state. section 4.5.7
  • Avoid unconditional reruns inside branches that run again with the same state; they can create an infinite loop and make the app stop responding. section 4.5.6
  • After saving a haiku successfully, rerunning refreshes the app and closes the dialog because its triggering button is no longer clicked. section 8.5.2
checked = task_col.checkbox(label, task.is_done, key=f"task_{idx}")    #1
if checked and not task.is_done:    #2
    mark_done(task)
    st.rerun()
elif not checked and task.is_done:    #3
    mark_not_done(task)
    st.rerun()

This conditional pattern updates checkbox state and reruns only when the stored status differs from the user’s selection. section 4.5.7

How Build Python Web Apps with Streamlit covers it

How deep it goes
Explains restart behavior, demonstrates applications, and diagnoses infinite loops caused by unconditional calls.
Examples
Uses task deletion, checkbox completion changes, and haiku creation to demonstrate successful and faulty reruns.
What it assumes
Assumes basic Streamlit widgets, event branches, mutable task state, and application reruns.
What you can do after
Diagnose and prevent rerun loops while refreshing Streamlit UI after state changes.
How it connects
Connects reruns with Streamlit execution flow, widgets, session state, dialogs, and CRUD operations.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.