St.radio on_change not working as intended or implementation incorrect

Hello, I’ve tried tried fixing this error for an day now and have unfrtunetly not gotten a response in the discord so I’ll just ask here.

I’m experiencing an issue where the streamlit radio on_change fires the function without the value actually changing but rather on the initial page render which is causing other problems in my app.
I do not know whether my implementation is just not ideal or if it’s an actual bug with the st.radio on_change parameter.

To reproduce:
download / copy the files in here
and run the home.py file using ‘streamlit run home.py’

Is this an unintended bug or am I just not understanding the st.radio correctly ??
Would appreciate any help on this ;D

I think the main part of your question is covered by separating the function assigned to on_change and its arguments.

You have:

st.radio(...on_change=update_settings("number_input"))

There is a separate keyword to pass the arguments. This is so on_change gets assigned a function object and not the output of that function. (Note the extra comma to pass the argument as a tuple of size 1.)

st.radio(...on_change=update_settings, args=("number_input",))

There is also another fact that the radio button does not output its value until after the callback function is run. So number_inpt_choice will not have the intended value until after the page loads.

If you set the radio button to have key="number_inpt_choice" then upon clicking it (and instantaneously before the on_change function is run), you can access st.session_state.number_inpt_choice within the on_change function if you need to.

1 Like

Thank you so much, got the implementation working and it all makes sense now, can finally move onto the next part of my app after finally getting this error resolved ;D