Button to unselect/clear all in multiselect

I want a button for my multiselect that has the same function as the x in the window. (see pic)

image

The problem is that I have a very large multiselect (about 100 entries) and otherwise I always have to scroll to the middle.

Now I had already tried something, that I saw in the forum.

if st.button("Clear"):
                del st.session_state.status_filter_2
                st.session_state.status_filter_2 = []

but every time i press the button the page reload. I just want a clear for the multiselect without reload.
Is it possible that this is done by a button that executes JS code? but for this i need a ID or something.

-edit-
by default i select all values

dep_form.multiselect(
                'Choose',
                st.session_state.ini_filter_2,
                st.session_state.status_filter_2, key='choose_filter2', label_visibility="visible")
            dep_form.form_submit_button(label="Submit", on_click=clicked_filter, args=['dep'])

regards
Marco

So Streamlit reruns the page from top to bottom on each widget interaction and that is why the page will reload each time. The clear button in the multiselect widget is an efficient way to avoid page reloading once you clear the options. Can you share the full code snippet so we can troubleshoot on our end as well?

Hi tonykip,

here the full code from my filter function.

ef load_filters():
    with st.expander("Filter", expanded=True):
        containter_filter = st.container()
        col1, col2, col3, col4, col5, col6 = containter_filter.columns([1.5, 1.4, 1.5, 2, 2, 3])
        ########################## Date Filter ####################################
        min_date = datetime.strptime(
            st.session_state.status_filter_1_start, '%Y-%m-%d')
        max_date = datetime.strptime(
            st.session_state.status_filter_1_end, '%Y-%m-%d')
        with col1.form("my_form"):
            st.date_input("Pick a date", (min_date, max_date),
                        key='select_date_detail')
            submit_button = st.form_submit_button(
                label="Submit", on_click=clicked_date_detail)
        CHOICES = {"Bitte auswählen": None, "Last 7 days": 'last_seven', "This month": 'this_month',
                "Last month": 'last_month', "This Year": 'this_year', "Last Year": 'last_year'}
        col1.selectbox("Auswählen", options=list(CHOICES.keys(
        )), on_change=clicked_date, key='select_date', label_visibility="collapsed")
        ########################## Abteilung Filter ####################################
        with col2:
            st.radio(
                "Date ansicht",
                ["Jahr", "Monat", "Days"],
                key="date_view",
                label_visibility="visible",
                disabled=False,
                horizontal=False,
                on_change=change_view
            )
            st.radio(
                "Maschinen ansicht",
                ["Abteilung", "Prozess", "Maschinen-Gruppe", "Maschinen"],
                key="mach_view",
                label_visibility="visible",
                disabled=False,
                horizontal=False,
                on_change=change_view_mach
            )
            dep_form = col3.form(key="select_dep")
            auswahl  = dep_form.multiselect(
                'Choose  Department',
                st.session_state.ini_filter_2,
                st.session_state.status_filter_2, key='choose_filter2', label_visibility="visible")
            dep_form.form_submit_button(label="Submit", on_click=clicked_filter, args=['abteilung'])
            #with col4.expander("Prozess auswählen"):
            pro_form = col4.form(key="select_pro")
            pro_form.multiselect(
                'Prozess Auswählen',
                st.session_state.ini_filter_3,
                st.session_state.status_filter_3, key='choose_filter3', label_visibility="visible")
            pro_form.form_submit_button(label="Submit", on_click=clicked_filter, args=['prozess'])

            #with col5.expander("Choose Maschinen-Gruppe"):
            mach_form = col5.form(key="select_mach")
            mach_form.multiselect(
                'Choose Maschine-Gruppe',
                st.session_state.ini_filter_4,
                st.session_state.status_filter_4, key='choose_filter4', label_visibility="visible")
            mach_form.form_submit_button(
                label="Submit", on_click=clicked_filter, args=['machine'])
            #with col6.expander("Choose Maschine "):
            detail_form = col6.form(key="select_detail")
            detail_form.multiselect(
                'Choose Maschine',
                st.session_state.ini_filter_5,
                st.session_state.status_filter_5, key='choose_filter5', label_visibility="visible")
            detail_form.form_submit_button(
                label="Submit", on_click=clicked_filter, args=['detail'])
            
            col1.button("reset", key="button_reset", on_click=rest_settings)

def rest_settings():
    del st.session_state.ini_allgemein
    del st.session_state.choose_filter2
    st.session_state.choose_filter2 = st.session_state.ini_filter_2
    del st.session_state.choose_filter3
    st.session_state.choose_filter3 = st.session_state.ini_filter_3
    del st.session_state.choose_filter4
    st.session_state.choose_filter4 = st.session_state.ini_filter_4
    del st.session_state.choose_filter5
    st.session_state.choose_filter5 = st.session_state.ini_filter_5
    st.session_state.date_view = 'Jahr'
    st.session_state.select_date = 'Bitte auswählen'
    del st.session_state.select_date_detail

that was my solution for filter my data. It works greate for me, but only the unselected doesn´t works.
I make also a reset Button “rest_settings” to restore the my default values.
I think the unselected button must work on the same way like the “rest_settings” button.

I try this

col1.button("Clear", key="button_clear", on_click=clear_settings)

def clear_settings():
    del st.session_state.choose_filter2
    st.session_state.choose_filter2 = []

but when i use this, i became this
image

but it´s empty :slight_smile:

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