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’t 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(“Limpiar filtros”):
genero = st.sidebar.multiselect(
"Seleccionar Genero: ",
options=df_violencia_int_2023[‘GENERO’].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[‘GENERO’].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[‘GENERO’].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.