Use a button when an action should wait for a user’s click. In Streamlit, `st.button("Plot Graph")` displays a labeled control; placing it in an `if` statement runs the indented code when clicked. A die-roll simulator uses this pattern to generate rolls and draw a chart only on request. section 1.3.5
Listing 1.2 A die roll simulator in Streamlit
import streamlit as st
import random
st.title("Die Roll Simulator")
num_rolls = st.slider('Number of die rolls', min_value=10, max_value=100)
if st.button('Plot Graph'):
die_rolls = [random.randint(1, 6) for _ in range(num_rolls)]
st.line_chart(data=die_rolls)
A short example pairs a slider with a button that triggers chart creation. section 1.3.5
As an app grows, a button’s placement and identity matter:
- Put a button in a sidebar or column to position it alongside the action it controls. A to-do list places each Delete button beside its task. section 4.2.1 section 4.2.2
- Give otherwise identical buttons distinct `key` values. Repeating `Delete` in a loop without keys produces a widget error. section 4.2.1
- Use `type="primary"` to emphasize a common action; the default secondary style is less prominent. section 4.2.2
Why did my task disappear?
Clicking Add task reruns the Python script. On that run, its button evaluates to `True` and the task is added. But if the task list is recreated as an empty list at the top of every run, a later interaction loses the earlier task. Clicking an unwired Delete button can appear to remove it for the same reason: the click triggers another run, not a delete operation. section 4.2.2 section 4.3.3
Even a wired Delete button can appear slow. If the app draws a task before calling `delete_task` later in the display loop, the current display still reflects the old list. Another interaction triggers a rerun that shows the change. Trace the order of display and mutation when a click seems ineffective. section 4.5.2 section 4.5.3
Not every action needs a button. A unit converter can show a result directly from its current inputs, updating when they change instead of requiring Convert. By contrast, a New Game button deliberately waits before starting a game; its label and styling can change between starting and restarting. section 3.7.2 section 9.5.1







