import streamlit as st
cols = st.columns(3)
with cols[0]:
st.header('Option 1')
selected_options = st.multiselect("Select one or more options:",
['A', 'B', 'C'], key='option')
all_options = st.button("Select all options")
if all_options:
selected_options = ['A', 'B', 'C']
selected_options
with cols[1]:
st.header('Option 2')
selected_option_2 = st.multiselect("Select one or more options:",['A', 'B', 'C', 'All'], key='option_2')
if "All" in selected_option_2:
selected_option_2 = ['A', 'B', 'C']
selected_option_2
with cols[2]:
st.header('Option 3')
container = st.container()
all = st.button("Select all")
if all:
selected_options_3 = container.multiselect("Select one or more options:",
['A', 'B', 'C'],['A', 'B', 'C'], key='option_3')
else:
selected_options_3 = container.multiselect("Select one or more options:",
['A', 'B', 'C'], key='option_3')
Option 3 is what i want, only problem is, that when i want to remove one of the options after selecting all, its removes all the option. I only can access single options through the drop down menu. Is there any solution for that?
I came up with the following solution that improves on method 3 as it does not lose all selected options when removing single ones after clicking “Select all”. It uses the st.session_state and a callback for the “Select all” button and looks a bit hacky :
import streamlit as st
st.header("Option 4")
all_options = ["A", "B", "C"]
selected_options_4 = st.multiselect(
"Select one or more options:", all_options, key="selected_options_4"
)
def _select_all():
st.session_state.selected_options_4 = all_options
st.button("Select all", on_click=_select_all)
selected_options_4
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.