Multiselect loses selected values

Hello,

If I change a preselected values, they all disappear. This is the snipet to reproduce it:

fruit_type = [“apple-4”, “apple-17”, “apple-78”, “orange-175”, “orange-176”]
selected_fruit_type = st.multiselect(“Select Fruit”, fruit_type, default=[“apple-4”, “apple-78”])

I’m on 0.80 version. Thank you

Could you please share the whole file.
It seems to work fine for me.

1 Like

The file is 1200 lines of code, but I was able to isolate the problem in this snipet:

import streamlit as st


    my_page = st.sidebar.radio('Sections', ['One', 'Two',])

    if my_page == 'One':

            if st.sidebar.button("Try"):

                fruit_type = ["apple-4", "apple-17", "apple-78", "orange-175", "orange-176"]
                selected_fruit_type = st.multiselect("Select Fruit", fruit_type, default=["apple-4", "apple-78"])

Cool. I am not an expert, but I think that the issue here is the button “Try”.

When you change an entry in the multiselect box the app re-runs (this is expected behaviour).
However, when the app re-runs the button “try” returns to the “unpressed” state. It does not remember that you pushed the button.

I think the solution is the “Session_state”:
https://discuss.streamlit.io/t/multi-page-app-with-session-state/3074
https://discuss.streamlit.io/t/preserving-state-across-sidebar-pages/107
https://discuss.streamlit.io/t/when-is-the-official-session-state-support-due-for-release/10429

1 Like

The button is not the appropriate widget to use for the desired interaction. If you want opened/closed, selected/not selected, or any kind of toggle state to be preserved between Streamlit’s re-runs, then the checkbox should be used.

if st.sidebar.checkbox("Try"):
2 Likes