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?
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.
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?
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.