With form radio button and select box not working

Ask the community or our support engineers for answers to questions.

import streamlit as st

with st.form("test1"):
    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.")
    check_fn=st.form_submit_button("Check_function")

With in form If select the radio buttons the content is not changing . Can you help how to handle this .

1 Like

Not sure what the problem is. Looks like it works on my side.

What i mean is before clicking on submit button the radio button is not working.
As per radio button want to do some operations .Simillar to like below.
Something like dynamic form as per options.

import streamlit as st

with st.form("test1"):
    genre = st.radio("What's your favorite movie genre",('Comedy', 'Drama', 'Documentary'))
    dict1=dict()
    if genre == 'Comedy':
        fun_name=st.text_input("please enter function name")
        if len(fun_name.split())>0:
            col1,col2 =st.beta_columns([5,2])
            dict1=dict()
            for i in fun_name.split():
                col1.write(i)
                dict1[i]=col2.text_input('Enter number of times')
    else:

        fun_input=st.text_area("Please enter details")
        dict1={v.split()[0] : v.split()[1]for v in fun_input.split('\n')}


    check_fn=st.form_submit_button("Check_function")
    if check_fn:
        pass

Iโ€™m with the same problem here. @Naveen_Bhaskar did you get how to sort it?

The issue is that the code below fails when the user doesnโ€™t type a comment, so you donโ€™t get to the check_fn

dict1={v.split()[0] : v.split()[1]for v in fun_input.split(โ€˜\nโ€™)}

A way to fix this is to change your else to

else:
fun_input=st.text_area(โ€œPlease enter detailsโ€)
st.write(โ€œYou didnโ€™t select comedy.โ€)
try:
dict1={v.split()[0] : v.split()[1]for v in fun_input.split(โ€˜\nโ€™)}
except:
pass
That way when the user does not insert a comment the rest of the code still works.

1 Like

Hello guys I am also having this problem, I need radio buttons in form, and when i clicked submit the data from the radio buttons should be send on st.session.variable. but it does not work. I only get blank string in the session.state

That seems to be a bug in your code.

1 Like

Thanks sir, I will check the code

I have also same problem. I used radio button and slider in form, but both of them dont work.

The values of the widgets in a form are not available until the submit button is pressed.

Thanks Goyo, They didnt work even though I used form_submit_button as follows:

    col1, col2 = st.columns(2)
    with col1:
        st.markdown("**Select**")
        selected_recommendation = st.radio(
            "Please select the most suitable one from the options.",
            my_list)
        

    with col2:
        st.markdown("**Rating**")
        if selected_recommendation is not None:
            rating = st.slider(
                "Please rate the selected one between 1 and 5.",
                1, 5)
         

    st.form_submit_button('Submit',on_click=handle_submit, args=(selected_recommendation,rating,))

I send the arguments with the send button, but default values are sent for both the radio button and the slider. Even if I make changes, the default values are sent instead of new values in the button and slider.

I also tried st.session_state to save selected_recommendation and rating. But again they gave same results.

By the time the callback and its arguments are set, the button has not been clicked and the new values of the widgets are not available, so the old values are passed to the callback.

Assign keys to the widgets and use session_state in the callback instead.