liveBook · Manning

Chapter 4: Streamlit’s execution model

What you will learn in chapter 4 of Build Python Web Apps with Streamlit

This chapter introduces Streamlit’s execution model, state management, and troubleshooting through a daily to-do list app with intentionally created bugs.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces Streamlit’s execution model, state management, and troubleshooting through a daily to-do list app with intentionally created bugs.

Key terms: Streamlit’s execution model, app state, st.session_state, st.rerun, daily to-do list app, troubleshooting, bugs, debugging

4.1 A more complex app: Daily to-dos

This section introduces a daily to-do list app as the chapter vehicle for learning Streamlit’s execution model, with only a brief design phase before implementation.

Key terms: to-do list app, tasks, Streamlit’s execution model, six-step development process, concept, requirements, mock design, implementation

4.1.1 Stating the concept

States the concise concept for the daily to-do app before defining its requirements.

Key terms: concept, Streamlit app, daily to-do list, tasks, status

4.1.2 Defining the requirements and what is out of scope

This section defines the daily to-do app’s user requirements and deliberately limits it to a temporary, single-browser-session tool.

Key terms: requirements, daily to-do list, tasks, task-completion status, out of scope, browser session, persisting, external storage, task history, concept

4.1.3 Visualizing the user experience

This section turns the daily to-do app requirements into a mock user interface showing where task creation, task management, and progress tracking appear.

Key terms: mock UI design, sidebar, main area, checkbox, done, strikes through, Delete buttons, tracker

4.1.4 Brainstorming the implementation

This section models the to-do app as an in-memory task list whose user actions modify task data and whose display logic re-renders the current list.

Key terms: task, task list, name, status, done, not done, Add a task, Mark the task as done, Mark the task as not done, Delete the task

4.2 Implementing and troubleshooting the app

This section introduces the deliberately problem-driven build of a to-do list app and prepares the reader to create its source file.

Key terms: troubleshooting, errors, Streamlit, backend, frontend, API, best practices, todo_list.py

4.2.1 Displaying the task list

This section incrementally renders a to-do list by modeling tasks, generating checkbox-and-delete controls in a loop, arranging them in columns, and assigning unique widget keys.

Key terms: Task, dataclass, name, is_done, task_list, st.checkbox, st.button, for loop, st.columns, relative widths

4.2.2 Enabling actions

This section adds task actions to the to-do app, then uses the app’s surprising loss of tasks to motivate examining Streamlit’s execution behavior.

Key terms: task_list, Task, add_task, is_done, st.text_input, st.button, st.sidebar, type="primary", primary action, st.info

4.3 How Streamlit executes an app

This section introduces Streamlit’s execution model as the foundation needed to build more complex apps.

Key terms: execution model, Streamlit apps

4.3.1 Frontend and server

This section explains how a Streamlit server and browser frontend communicate to run a script and display an app.

Key terms: backend Streamlit server, frontend, server, listens, port, localhost, request, executes, top to bottom, HTML

4.3.2 App reruns

This section explains that Streamlit reruns the entire Python script whenever an interaction or display change occurs, and traces the frontend-server update cycle.

Key terms: app reruns, Streamlit server, Python script, widgets, frontend, server, button click, display updates

4.3.3 Applying reruns to the app

This section traces how full Streamlit reruns make the current to-do app lose its in-memory task list after user interactions.

Key terms: rerun, task_list, st.button, True, False, add_task, st.text_input, Task

4.4 Persisting variables across reruns

This section frames the to-do app’s memory problem: full Streamlit reruns reset ordinary variables such as the task list.

Key terms: reruns, task_list, execution model

4.4.1 st.session_state

This section explains how st.session_state preserves per-session values across Streamlit reruns and how to initialize and manipulate that state safely.

Key terms: st.session_state, app session, reruns, key-value pairs, st.session_state["x"], st.session_state.x, items(), del, "number" key, initial check

4.5 Completing the app

This section introduces the return to the to-do list app now that session state can preserve information across Streamlit reruns.

Key terms: st.session_state, reruns, memory, to-do list app

4.5.1 Adding session state

This subsection fixes the to-do app’s disappearing tasks by storing the task list in session state and binding the existing variable to that persistent list.

Key terms: task_list, st.session_state, rerun, st.session_state.task_list

4.5.2 Wiring up the Delete buttons

This section connects each Delete button to the existing delete_task function and exposes the delayed, incorrect UI updates that result.

Key terms: Delete buttons, display loop, pass, delete_task function, Delete, lag, checkboxes, task entry textbox

4.5.3 What’s happening behind the scenes

This section traces why deleting a task updates the list after the display code has already run, producing stale or incorrect to-do UI until a later rerun.

Key terms: rerun, st.button, delete_task(idx), task_list, idx, widget keys, display logic, delete_task, list indices, display loop

4.5.4 Triggering reruns automatically

This section fixes the to-do deletion display problem by explicitly triggering a Streamlit rerun after a task is deleted.

Key terms: st.rerun, rerun, task_list

4.5.5 Wiring up the checkboxes

This section wires task checkboxes to completion state and Markdown labels, then exposes the rerun lag and the hang caused by adding manual reruns.

Key terms: mark_done, mark_not_done, checkbox, is_done, Markdown, strikethrough, label parameter, tildes, rerun, manual rerun

4.5.6 An infinite rerun loop

This section traces how an unchecked task checkbox repeatedly calls st.rerun(), creating an infinite Streamlit rerun loop that makes the app stop responding.

Key terms: infinite rerun loop, task_list, Task, is_done, display loop, else clause

4.5.7 Preventing the infinite rerun

This section fixes an infinite rerun loop by updating a task and calling st.rerun only when its checkbox value represents an actual change in completion status.

Key terms: mark_done, mark_not_done, st.rerun, is_done, checkbox, display for loop

4.5.8 Adding the completion progress indicator

This section finishes the to-do app by adding a large completion-progress metric derived from the task list and presenting the final app.

Key terms: st.metric, task_list, completed tasks, total number of tasks, completed_tasks, list comprehension, st.info, troubleshooting mode

4.6 Summary

This summary recaps how Streamlit identifies widgets, reruns scripts, preserves state, and supports troubleshooting interactive apps.

Where this is used later

Concepts explained in this chapter

← Chapter 3: Taking an app from concept to code Chapter 5: Sharing your apps with the world →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.