Disable Radio group after Form submission

I’m creating a GPT-based application, wherein the user submits a form and some output is generated dynamically based on the submission; and I don’t want the user to be able to change their response after the LLM’s response has been outputted / is outputting.
And I am having trouble changing the state of the radio group in the form to disabled. I tried using session state, by explicitly changing the value of the session state variable in the code, but it doesn’t work.

This is my code:

# The function to change radio group state
def get_output(el_id):
    st.session_state[f'rad_{el_id}_disabled'] = True
    print(st.session_state[f'rad_{el_id}'])
    
# The main portion of the app where I'll get the user response from
def display_content(txt, opts, el_id):
    container = st.container()   
    col1, col2 = container.columns([0.65, 0.35])
    
    col1.write(txt[0])
    col2.write(txt[1])
    st.divider()
    
    with col1:
        with st.form(key=f'user_choice_{el_id}'):
            st.session_state[f'rad_{el_id}_disabled'] = False
            st.radio(txt[-1], opts, key=f'rad_{el_id}', disabled=st.session_state[f'rad_{el_id}_disabled'])
            st.form_submit_button(label="Let's move on!", on_click=lambda: get_output(el_id))

I hoped that, in accordance with the st.radio docs, my radio group would get disabled as soon as I clicked the form submit button, but it does not get disabled. Instead, the user can very much change the radio selection.

Can someone please help me figure out to disable the radio group, please?

You have disabled=False hardcoded in your call to st.radio.That seems to go against your goal.

There is nothing in the docs about disabling a radio widget by setting a session_state item. It is controled by the parameter disabled to st.radio() instead.

Corrected that by changing it to disabled=st.session_state[f'rad_{el_id}_disabled'] and setting st.session_state[f'rad_{el_id}_disabled'] = False before initializing the radio group, but still it seems to not disable as soon as I press the button.

I am referring to the second example in the docs where checking a checkbox labelled ‘Disable Radio Widget’ leads to radio group being disabled, but I am unable to replicate that behaviour with the button because the form submit button seems to have a temporary activation state.

I have updated the code to match what I’m using right now, but it still doesn’t work.

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