Use button to clear multiselect using session_state

Hi @Marisa_Smith , can you show us how to apply st.sessions_state on the mulit-selectbox? Cause I am facing an issue of unchecking all the values on the multi-selectbox? Would be appreciate if you can help to resolve such issue! Thanks

hi @jiffred_teh!

Sorry for my delay in getting this example to you. Here is an example code that does exactly what your looking for:

import streamlit as st

# create a function that sets the value in state back to an empty list
def clear_multi():
    st.session_state.multiselect = []

st.title("Clear multiselect with stateful button")

# create multiselect and automatically put it in state with its key parameter
multi = st.multiselect("Pick an option", ["a","b","c","d"], key="multiselect")

# check state

#create your button to clear the state of the multiselect
st.button("Clear multiselect", on_click=clear_multi)

Happy Streamlit-ing!

P.S. I moved your question to its own topic to make it easier for others to find

1 Like