liveBook · Manning

Callback

As explained in Build Python Web Apps with Streamlit

A callback is a function supplied to another component for execution in response to a later event.

A callback is a function supplied to another component for execution in response to a later event. In Streamlit, a button callback runs before the page reruns, so state changes are already reflected without manually calling st.rerun(). section 9.6.2

Advance a trivia game

A Next question button can register a callback that changes the game state: st.button("Next question", type="primary", on_click=lambda: game.proceed_to_next_question()). section 9.6.2

The alternative if st.button structure is often easier for simple apps because it makes the control flow more explicit: handle the click, change state, then manually rerun when needed. Callbacks are more restricted; a callback cannot trigger an app rerun. section 9.6.2

st.button(
  "Next question",
  type='primary',
  on_click=lambda: game.proceed_to_next_question()
)

Browse the complete button callback used to move the trivia game to its next question. section 9.6.2

How Build Python Web Apps with Streamlit covers it

How deep it goes
Explains execution order, automatic reruns, and one restriction while stopping short of broader callback patterns.
Examples
Uses a Next question button to call game.proceed_to_next_question in a trivia game.
What you can do after
Apply it via Streamlit's st.button to update game state before the resulting rerun.
How it connects
Connects callbacks with button events, page reruns, application state, and manual reruns.

Also explained in

Compare how 19 books explain callback →

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.