liveBook · Manning

Widget key

As explained in Build Python Web Apps with Streamlit

A widget key is an identifier that distinguishes a Streamlit widget from other widgets, especially widgets with identical characteristics.

A widget key is the identifier Streamlit uses to tell one widget from another. Streamlit usually generates it from a widget’s characteristics. Two otherwise identical buttons—for example, two buttons labeled Delete in a task list—can therefore produce the same key and trigger an error. Give each button a distinct key to make the widgets distinguishable. section 4.2.1

One Delete button per task

Loop over the tasks with enumerate(task_list) to obtain both the task and its index. Then pass key=f"delete_{idx}" to each Delete button. The visible labels stay the same, while the keys become delete_0, delete_1, and so on. Apply the same pattern to task checkboxes if they need explicit keys. section 4.2.1

for idx, task in enumerate(task_list):
    task_col, delete_col = st.columns([0.8, 0.2])
    task_col.checkbox(task.name, task.is_done)
    if delete_col.button("Delete", key=f"delete_{idx}"):
        pass

See how the loop gives each identically labeled Delete button a unique key. section 4.2.1

A manually keyed input also exposes its value through st.session_state. For a selectbox with key="select_dim", read or set st.session_state["select_dim"]. To choose its value programmatically, set the session-state entry before the widget runs; do not render the widget first and then overwrite its value. section 7.6.2

Keys can also identify which dashboard inputs belong in a shareable URL. Prefix selected widget keys with w:, load matching query parameters into st.session_state before rendering, and write those widget values back to st.query_params after rendering. Set URL-derived values only when their keys are not already in session state, so later user selections remain changeable. Values such as dates and lists need conversion when moving between widget values and URL strings. section 7.6.3 section 7.6.2

A unique key identifies the clicked button; it does not by itself refresh display logic that already ran. If a deleted task still appears, step through the rerun: the task and button may have been displayed before the deletion changed the list. section 4.5.3

How Build Python Web Apps with Streamlit covers it

How deep it goes
Defines automatic and manual keys, then works through indexed buttons, session-state defaults, and URL synchronization.
Examples
Uses repeated Delete buttons in a task list, a keyed selectbox, and URL-populated dashboard filters.
What it assumes
Assumes basic Python loops and Streamlit widget calls while explaining widget identification and demonstrating enumerate.
What you can do after
Apply it via Streamlit to fix duplicate-widget errors and set or share keyed input values.
How it connects
Connects widget keys to st.button, st.checkbox, st.session_state, st.query_params, and Streamlit reruns.

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.