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