liveBook · Manning

St.expander

As explained in Build Python Web Apps with Streamlit

st.expander is a Streamlit layout element that displays content inside a panel users can expand or collapse.

Use st.expander to place optional content in a collapsible panel. In a dashboard, hiding filters keeps attention on the displayed data while allowing users to reveal controls when needed. section 6.3.1

Collapsible filter panel

Wrap the filter controls in with st.expander("Filters"): create layout columns inside the panel, populate multiselect widgets, and let users expand or collapse the complete filter area. section 6.3.1

Listing 6.4 A starting draft of filter_panel.py
import streamlit as st
from data_wrangling import get_unique_values

filter_dims = ["age_group", "gender", "category", "segment",
               "product_name", "state"]

def filter_panel(df):
  with st.expander("Filters"):
    filter_cols = st.columns(len(filter_dims))
    for idx, dim in enumerate(filter_dims):
      with filter_cols[idx]:
        unique_vals = get_unique_values(df, dim)
        st.multiselect(dim, unique_vals)

Inspect the complete starting filter_panel implementation, including the expander, column layout, unique values, and multiselect controls. section 6.3.1

How Build Python Web Apps with Streamlit covers it

How deep it goes
Explains the collapsible-panel role and demonstrates it around filter widgets, but does not cover advanced expander behavior.
Examples
Demonstrates a six-field filter panel containing multiselect controls for age group, gender, category, segment, product name, and state.
What it assumes
Assumes basic Streamlit widgets, dataframes, and dashboard layout concepts.
What you can do after
Operate/configure Streamlit dashboards with optional filter controls that users can expand or collapse.
How it connects
Connects st.expander with Streamlit dashboards, columns, multiselect widgets, and data filtering.

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.