Use button to clear multiselect using session_state

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 = []
    return

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
st.session_state

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

Happy Streamlit-ing!
Marisa

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

2 Likes