跳到主要内容
知仓学习社ZHICANG

choosing-streamlit-selection-widgets

Choosing the right Streamlit selection widget. Use when deciding between radio buttons, selectbox, segmented control, pills, or other option selecti…

不碰外部(只输出文字)无严重或高危命中iusztinpaul/designing-real-world-ai-agents-workshop

它会碰到什么

扫了多少1 个文本文件,4 KB
它会碰到什么不碰外部(只输出文字)
命中总数0 处
命中统计严重 0 · 高 0 · 中 0 · 低 0

这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。

技能内容

Streamlit selection widgets

The right selection widget for the job. Streamlit has evolved—many old patterns are now anti-patterns.

When to use what

Use st.segmented_control or st.pills when you want all options visible at once. Use st.selectbox or st.multiselect when options should be hidden in a dropdown.

| Widget | Best For |

|--------|----------|

| st.segmented_control | 2-5 options, single select, all visible |

| st.pills | 2-5 options, multi-select, all visible |

| st.selectbox | Many options, single select, dropdown |

| st.multiselect | Many options, multi-select, dropdown |

Segmented control (options visible, single select)

# BAD
status = st.radio("Status", ["Draft", "Published"], horizontal=True)

# GOOD
status = st.segmented_control("Status", ["Draft", "Published"])

For vertical layouts, st.radio(..., horizontal=False) is still a great choice.

Cleaner, more modern look than horizontal radio buttons.

Pills (options visible, multi-select)

# Multi-select with few options
selected = st.pills(
    "Tags",
    ["Python", "SQL", "dbt", "Streamlit"],
    selection_mode="multi"
)

Can also be used to mimic an "example" widget, especially with label_visibility="collapsed":

st.pills("Examples", ["Show me sales data", "Top customers"], label_visibility="collapsed")

More visual and easier to use than st.multiselect for small option sets.

Selectbox (many options, single select)

country = st.selectbox(
    "Select a country",
    ["USA", "UK", "Canada", "Germany", "France", ...]
)

Dropdowns scale better than radio/pills for long lists.

Multiselect (many options, multi-select)

countries = st.multiselect(
    "Select countries",
    ["USA", "UK", "Canada", "Germany", "France", ...]
)

Toggle vs checkbox

Use st.toggle for settings that trigger changes in the app. Reserve st.checkbox for forms.

# GOOD: Toggle for app settings
dark_mode = st.toggle("Dark mode")
show_advanced = st.toggle("Show advanced options")

# GOOD: Checkbox in forms
with st.form("signup"):
    agree = st.checkbox("I agree to the terms")
    st.form_submit_button("Sign up")

Forms with border=False

Remove the default form border for cleaner inline forms. Keep the border for longer forms where visual grouping helps.

# Inline form without border
with st.form(key="add_item", border=False):
    with st.container(horizontal=True, vertical_alignment="bottom"):
        st.text_input("New item", label_visibility="collapsed", placeholder="Add item")
        st.form_submit_button("Add", icon=":material/add:")

# Longer form - keep the border for visual grouping
with st.form("signup"):
    st.text_input("Name")
    st.text_input("Email")
    st.selectbox("Role", ["Admin", "User"])
    st.form_submit_button("Submit")

Custom options in selectbox and multiselect

Allow users to add their own options with accept_new_options:

# Works with multiselect
tickers = st.multiselect(
    "Stock tickers",
    options=["AAPL", "MSFT", "GOOGL", "NVDA"],
    default=["AAPL"],
    accept_new_options=True,
    placeholder="Choose stocks or type your own"
)

# Also works with selectbox
country = st.selectbox(
    "Country",
    options=["USA", "UK", "Canada"],
    accept_new_options=True,
    placeholder="Select or type a country"
)

References

想直接用这个技能?

本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。