Problem with multiselect erase button

Hi everyone, I have a problem. I created a button for clear multiselects, but when I run the button, it creates new multiselect with errased filters, I don鈥檛 want to create new multiselect filters I only want to clean old filters multiselect. In the next section, you can see the link to my code and then you can see the specific part with the button.

I appreciate your comments!

1.link: Analytics_Portfolio/STREAMLIT/Domestic_Abuse/cuaderno_mapa3.0.py at main 路 miguelmag1/Analytics_Portfolio 路 GitHub

  1. Specific code part:

if st.sidebar.button(鈥淟impiar filtros鈥):
genero = st.sidebar.multiselect(
"Seleccionar Genero: ",
options=df_violencia_int_2023[鈥楪ENERO鈥橾.unique(),

)

agrupacion_edad = st.sidebar.multiselect(
    label='Seleccione Grupo Etario',
    options=df_violencia_int_2023['AGRUPA_EDAD_PERSONA'].unique(),

)

departamento = st.sidebar.multiselect(
    label='Seleccione departamento',
    options=df_violencia_int_2023['DEPARTAMENTO'].unique(),

)

mpio = st.sidebar.multiselect(
    label='Seleccione municipio',
    options=df_violencia_int_2023['MUNICIPIO'].unique(),

)

Hi @Miguelmag

Perhaps you can try this:

  1. Store your multiselect widget in a session state
if "genero" not in st.session_state.keys():
    st.session_state. genero = df_violencia_int_2023[鈥楪ENERO鈥橾.unique()
  1. Overwrite the session state of the multiselect widget when you want to clear its filter
def clear_genero():
    st.session_state.genero = df_violencia_int_2023[鈥楪ENERO鈥橾.unique()

st.sidebar.button('Clear filter', on_click=clear_genero)

Hope this helps!

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