"Select-All" Button for Multiselect

Hi all,

i want to create a button, that select all options of the multiselect widget similar to this 'Select All' on a Streamlit Multiselect - #2 by Marisa_Smith but with a button instead of a checkbox

Thanks for any help!

Python 3.11.0
Streamlit, version 1.28.2

Hi @DrLoeppi

You can simply replace st.checkbox with st.button and it should work.

I’ve demonstrated that in an example app here https://sandbox-examples.streamlit.app/

Here’s the code snippet:


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')

Hope this helps!

1 Like

Hi, thanks a lot for the reply!

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?