st.multiselect in Streamlit

The st.multiselect widget in Streamlit allows users to select multiple items from a list of options.

Syntax

st.multiselect(
    label,          # Label displayed above the multiselect box
    options,        # List of items to choose from
    default=None,   # Optional default selected items
    help=None       # Optional help tooltip
)    

Example: Basic Multiselect

Python

# Importing the Streamlit library
import streamlit as st

# List of options
fruits = ["Apple", "Banana", "Cherry", "Date", "Grapes"]

# Multiselect widget
selected_fruits = st.multiselect(
    "Select your favorite fruits:",
    options=fruits
)

# Display selected fruits
st.write("You selected:", selected_fruits)    

Explanation

The output of the above code is shown below:

st.multiselect in Streamlit

Example: With Default Selection and Help

Python

import streamlit as st

colors = ["Red", "Blue", "Green", "Yellow", "Purple"]

selected_colors = st.multiselect(
    label="Pick your favorite colors:",
    options=colors,
    default=["Red", "Blue"],  # Default selected items
    help="You can select multiple colors"
)

st.write("Selected colors:", selected_colors)    

Explanation

Key Points

Example: Multiselect for Filtering a DataFrame

Python

import streamlit as st
import pandas as pd

# Sample DataFrame
data = {
    "Store": ["A", "B", "C", "D"],
    "Sales": [100, 150, 200, 130]
}
df = pd.DataFrame(data)

# Multiselect widget for filtering stores
selected_stores = st.multiselect(
    "Select Stores to View:",
    options=df["Store"].tolist()
)

# Filter DataFrame based on selection
filtered_df = df[df["Store"].isin(selected_stores)]

st.write(filtered_df)    

Explanation