How to collapse the multiselect widget in streamlit with default options

I am using streamlit to display a pandas data-frame. I am using st.multiselect( to display some default columns.

When I run the app. The multi-select widget is not collapsed and consumes lot of space.
Is there a way so that the multi-select widget is collapsed and default columns are not shown on the widget.

Here is the simple code:

import pandas as pd
import streamlit as st

data = {
    'Col1': [1, 2, 3, 4, 5],
    'Col2': ['A', 'B', 'C', 'D', 'E'],
    'Col3': [10.1, 20.2, 30.3, 40.4, 50.5],
    'Col4': [True, False, True, False, True],
    'Col5': ['X', 'Y', 'Z', 'W', 'V'],
    'Col6': [100, 200, 300, 400, 500],
    'Col7': ['Apple', 'Banana', 'Orange', 'Grape', 'Mango'],
    'Col8': [0.1, 0.2, 0.3, 0.4, 0.5],
    'Col9': [5, 10, 15, 20, 25],
    'Col10': ['Red', 'Green', 'Blue', 'Yellow', 'Purple']
}
df = pd.DataFrame(data)
print(df.head())

with st.sidebar:
    st.write("Columns to display:")
    default_columns = ['Col1', 'Col2', 'Col3', 'Col4', 'Col5']
    selected_columns = st.multiselect("Select columns", df.columns.tolist(), default=default_columns)



filtered_df = df[selected_columns]
st.write("Filtered DataFrame:")
st.dataframe(filtered_df)

On the UI ['Col1', 'Col2', 'Col3', 'Col4', 'Col5'] is shown in widget. Is it possible to hide/collapse these default column names?

Hey @piam,

Unfortunately, there isn’t a built-in way to avoid displaying your default columns while still having default columns set.

Your best options would be to either not have default values set:

selected_columns = st.multiselect("Select columns", df.columns.tolist())

Or place the st.multiselect within an st.expander:

with st.expander("Choose columns"):
    selected_columns = st.multiselect("Select columns", df.columns.tolist(), default=default_columns)
1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.