Thanks a lot for the super useful examples above Appreciate it. I am trying to take option 3 further, and synchronize the “select all” checkbox with the multiselect’s selection (same question as @Subham). My solution below requires two clicks, however. Would you know how to make it work with only one click?
if 'toggle_checkbox' not in st.session_state:
st.session_state.toggle_checkbox = True
container = st.container()
all = st.checkbox("Select all",value = st.session_state.toggle_checkbox)
if all:
selected_options = container.multiselect("Select one or more options:",
['A', 'B', 'C'],['A', 'B', 'C'])
else:
selected_options = container.multiselect("Select one or more options:",
['A', 'B', 'C'])
if ('A' in selected_options) and ('B' in selected_options) and ('C' in selected_options):
st.session_state.toggle_checkbox = True
else:
st.session_state.toggle_checkbox = False
I moved your question to its own topic to make it easier for others to search when looking for a similar answer!
You need to change your code to use the on_change call back functions. This will allow you to pre-set the st.selectbox and st.checkbox depending on the assigned values from a previous run.
Here is you code working:
# on the first run add variables to track in state
if "all_option" not in st.session_state:
st.session_state.all_option = True
st.session_state.selected_options = ['A', 'B', 'C']
def check_change():
# this runs BEFORE the rest of the script when a change is detected
# from your checkbox to set selectbox
if st.session_state.all_option:
st.session_state.selected_options = ['A', 'B', 'C']
else:
st.session_state.selected_options = []
return
def multi_change():
# this runs BEFORE the rest of the script when a change is detected
# from your selectbox to set checkbox
if len(st.session_state.selected_options) == 3:
st.session_state.all_option = True
else:
st.session_state.all_option = False
return
selected_options = st.multiselect("Select one or more options:",
['A', 'B', 'C'],key="selected_options", on_change=multi_change)
all = st.checkbox("Select all", key='all_option',on_change= check_change)
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.