Update Value of checkbox based off values in MultiSelect

I have a checkbox that selects/deselelects values in a multiselect and this is working as expected. But I noticed a small UI issue, when I manually deselect values from the multiselect the checkbox value stays the same. Example: I check the checkbox to select all values, I deselect the values one by one the checkbox still stays marked true. I would like to update this to false so user can be able to again check the box and select all values.

Is there anyway I can update the value of the checkbox to false when the multiselect is empty or not all options are selected?

Hi @elsuenog. :wave: Can you share a minimal, executable snippet to show what you’ve tried so far?

    def reset_checkbox():
        logging.warning("Inside function")
        if len(selected_pla_options) != len(pla_list):
            logging.warning("Lists do not match")
            st.session_state["TestKey"] = False

    # Create PLA filter
    plaCheckbox = col1.checkbox("Select all Priority learning areas", value=True, key="TestKey")
    
    pla_container = col1.container(height=200, border=False)
    
    if plaCheckbox:
        selected_pla_options = pla_container.multiselect("Select one or more Priority learning areas:",
            pla_list,pla_list, on_change=reset_checkbox)
        logging.warning(selected_pla_options)
    else:
        selected_pla_options =  pla_container.multiselect("Select one or more Priority learning areas:",
            pla_list, on_change=reset_checkbox)

Tried to do this. Got this error below

The widget with key "TestKey" was created with a default value but also had its value set via the Session State API.

Basically. the checkbox is marked by default and that is because the multiselect is full by default as well. So want to uncheck the checkbox if the user removes a selection.

I generally recommend not using value and key together. Instead of setting value initialize the key-value pair in Session State before the widget. Then, when the widget is created for the first time, it will take on the value from Session State.

Try doing something like in this example.

In other words change this:

plaCheckbox = col1.checkbox("Select all Priority learning areas", value=True, key="TestKey")

to this:

if "TestKey" not in st.session_state:
    st.session_state.TestKey = True
plaCheckbox = col1.checkbox("Select all Priority learning areas", key="TestKey")
1 Like

Thanks that was helpful. This is where I get confused a bit about session state and preserving since I am new here to streamlit and also python. So if you noticed in my earlier code I have an if/else statement. That statement is for the checkbox, basically if checked load all values, if unchecked remove selected values. If I am manually unchecking the checkbox now because a user manually removed one selection, this results in turning the checkbox to false but also hits the else portion of my code. Is there a way to bypass this?

Sorry broke this down to two comments. But basically my solution would be to remove the if/else statement and try to do something like the link you provided and do a update_value function but for the multi-select?

I found a next way to achieve what I was trying to achieve.

1 Like

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