liveBook · Manning

Check box

As explained in Build Python Web Apps with Streamlit

st.checkbox is a Streamlit input API element that renders a Boolean checkbox with a label.

Use `st.checkbox` when someone should be able to turn an option on or off. It renders a labeled box and returns a Boolean value, so an `if` statement can show an extra input or choose different behavior. section 3.7.1

Offer rounding only when requested

A unit converter need not show a decimal-places input to everyone. Set `places = None`, then put `st.number_input` inside `if st.checkbox("Round result?", value=False):`. The unchecked box leaves the result at full precision; checking it reveals the input. Initializing `places` first lets later formatting code use it either way. section 3.7.1

A task list needs one checkbox per task. Loop over the tasks and pass each task’s name as the label and its `is_done` Boolean as the initial checked value. Give each checkbox a distinct `key` when building the repeated widgets. Rendering them is only the first step: checking a box must also update the task’s status. section 4.2.1 section 4.5.5

For completed tasks, the label can use Markdown strikethrough: `~~task name~~`. Branch on the checkbox result to call `mark_done` or `mark_not_done`. Test the visible result as well as the status: task-list changes can appear one action late, and calling `st.rerun()` in both branches makes the app rerun without stopping. section 4.5.5

How Build Python Web Apps with Streamlit covers it

How deep it goes
Works through conditional display, repeated checkboxes, task-status updates, and rerun pitfalls rather than widget internals.
Examples
Uses a unit converter’s optional decimal-places input and a to-do list with one checkbox per task.
What it assumes
Introduces the checkbox in plain terms while using Python `if` statements and loops to act on its value.
What you can do after
Build it from scratch: add Streamlit checkboxes that reveal an optional input or update a task’s completion status.
How it connects
Connects checkboxes to Python conditionals, loops, Markdown labels, widget keys, and Streamlit reruns.

Also explained in

Compare how 4 books explain check box →

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.