Change the state of an item from code?

Hi all.

Can I change the state of an item from code? Suppose I have a radio button option. Now, further down in my logic code I want to reset the selection to a certain value as a result of a computation since the outcome determines that a certain option must be selected (i.e. computation is not valid for a certain setting)…

Is this possible? How can I access an existing radio button widget and set it to a certain value by code…?

Hey @cwerner, welcome to the forums! :tada:

Depending on your needings, you could solve that in many ways:

  1. Using an empty element
  2. Using st.cache()
  3. Using “Session State” as shown here

Please let us know if this helps you

Thanks for the pointers @arraydude

However, given your links I do not see how I would write code to set an existing radio button to a new value depending on some outcome from a calculation?

Sorry if I miss the obvious?

Hi @cwerner,

Here’s an example snippet using st.empty()

import streamlit as st
import time

options = ("male", "female")

a = st.empty()

value ="gender", options, 0)



value ="gender", options, 1)


Thanks for the example!

Did not know about st.empty(). What’s the exact purpose in this scenario?

Also, I can change the value via code, but the radio button itself does not change… Is this only on my install?


I’m not sure what you mean exactly but this is what I understood:

value =“choices”, [“choice1”, “choice2”, “choice3”, “choice4”])
#the user select an option

#some computations here that change value to whatever you want

you could use session_state like this
value = SessionState.get(val = “”)
value.val =“choices”, [“choice1”, “choice2”, “choice3”, “choice4”])

#and later change it how ever you want like this
value.val = “something here”

I got the result value changed, but expected the radio button to “jump” to my intended selection (which does not happen)…

Scenario: I have an option that is not yet implemented and when ever it’s selected it should emit a warning and jump to the valid option…

Did not check session state yet… will have a go with this…

I got you this work around, but it’s a little bit messy. I hope someone comes with a better solution.
Here’s an example

import streamlit as st
from session_state import SessionState

session_state = SessionState.get(hist = [], val = "", index=0, radio_key=0)


x = st.empty()

class params():
    def __init__(self, session_state):
        self.radio_key_str = str(session_state.radio_key)
        self.index = session_state.index
        self.val ="options",["paris", "milan", "NY"], self.index, key=self.radio_key_str)

    def reset(self):

    def update_session(self, session_state):
        session_state.val = self.val
        session_state.index = self.index
        session_state.radio_key = session_state.radio_key+1

params = params(session_state)


if st.button("set to default"):
    params.index = 1


Hi @cwerner,

I’m seeing the radio input selection visually jump to the new option.

See video at

Please post a video along with your code snippet and OS version, browser version, python version, etc… if you’re not seeing the same.

st.empty is used to get a placeholder that you can reference later, to fill it with an element/widget.
See docs at

Thanks Jonathan for the video and reply.

I’m not quite sure. I think when I last tried it I was in a python 3.8 env. I now tried it within my current project env and now it jumps…?!?

However, I’m not sure the 2nd st.write(value) work as intended?

I modified my example slightly. This now work 95%, however there’s a small detail I’d like to change.

import streamlit as st
import time

options = ("male", "female")

a = st.empty()

value ="gender", options, 0, key='a')

if value == 'male':
elif value == 'female':
    time.sleep(1)"gender", options, 0, key='b')
    st.write(value)     # this produces a second line, 
                        # ideally it should replace the 
                        # previous st.write output...

Hi @cwerner,

See the following example which you can mimic to get your desired solution.

Thanks, will check it out :+1: