liveBook · Manning

Multiselect

As explained in Build Python Web Apps with Streamlit

A multi-select option is a user-interface control that lets a user choose multiple values from a set of options.

A dashboard filter often needs more than one answer: a user might want several product categories or states at once. A multiselect is a drop-down control for choosing any combination of options. In Streamlit, `st.multiselect('Color', ['blue', 'green', 'red'])` displays a labeled control that accepts one or more colors. Its required arguments are a label and a list of options. section 6.3.1

For a data-driven filter panel, populate each control from the distinct values in its dataframe column. `list(df[column].unique())` produces that option list. Put the controls in an expander and use `st.columns` to arrange them side by side; save each control’s returned selections under its column name. section 6.3.1

def filter_panel(df):
  filters = {}
  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)
        filters[dim] = st.multiselect(dim, unique_vals)
  return filters

The filter-panel function shows how to collect selections from several multiselect controls into one dictionary. section 6.3.1

Displaying controls alone does not filter the data. Pass the returned dictionary to a filtering function: for each column with selected values, keep rows whose value is in that selection. Skip columns with no selections. The resulting dataframe can then drive the displayed dashboard data. section 6.3.1

Choose a drilldown breakdown

A drilldown can use a single `st.multiselect('Drilldown fields', [...])` to let users choose which dimensions appear in an aggregated metrics table. When dimensions are selected, group the dataframe by those fields; when none are selected, calculate metrics for the entire dataframe instead. This makes an empty selection a useful overall view rather than a dead end. section 7.5.2 section 7.5.3

How Build Python Web Apps with Streamlit covers it

How deep it goes
Shows required widget arguments, option generation, returned selections, and downstream filtering or aggregation, without detailing widget internals.
Examples
Uses a color drop-down, a sales-dashboard filter panel, and a drilldown-field selector.
What it assumes
Introduces the widget’s required arguments while using Python lists and pandas dataframe columns to build its options.
What you can do after
Apply it via Streamlit to collect multiple selections, filter dataframe rows, and control a drilldown breakdown.
How it connects
Connects multiselect to pandas dataframes, unique values, row filtering, groupby aggregation, and Streamlit layout widgets.

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.