How can I set the default streamlit radio button to nothing?

Hello. Is it possible to have the streamlit radio button to nothing? So for the example below, how could I have streamlit not select any of the options in the beginning.

genre = st.radio(
     "What's your favorite movie genre",
     ('Comedy', 'Drama', 'Documentary'))

if genre == 'Comedy':
     st.write('You selected comedy.')
 else:
     st.write("You didn't select comedy.")

Hi @slee24

Does this work for you?

genre = st.radio("Genre", ('', 'Comedy', 'Drama'), index=0)

if genre != ‘’:

st.write(f'You selected {genre}') 

else:

st.write("No selection')

Cheers

Hi @Shawn_Pereira.This does work. But I am wondering if there is a way to just keep it to two radio buttons as supposed to 3 and have nothing selected.

Hi @slee24 , I don’t think there’s a way to not have any radio button option not selected.

Cheers

Hi!

You can try something like this!

    if 'a' not in st.session_state:
        st.session_state.a = 0
        st.session_state.b = 0
        st.session_state.c = 0

    def ChangeA():
        st.session_state.a,st.session_state.b,st.session_state.c = 1,0,0
    def ChangeB():
        st.session_state.a,st.session_state.b,st.session_state.c = 0,1,0
    def ChangeC():
        st.session_state.a,st.session_state.b,st.session_state.c = 0,0,1
    
    col1, col2, col3, _ = st.columns([1,1,1,5])
    with col1:
        Comedy = st.checkbox('Comedy', value = st.session_state.a, on_change = ChangeA)
    with col2:
        Drama = st.checkbox('Drama', value = st.session_state.b, on_change = ChangeB)
    with col3:
        Documentary = st.checkbox('Documentary', value = st.session_state.c, on_change = ChangeC) 

if Comedy or Drama or Documentary:
    st.write('You selected',st.session_state.a*'Comedy',st.session_state.b*'Drama',st.session_state.c*'Documentary')
else:
    st.write('No selection')

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.