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…?

1 Like

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

1 Like

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?

1 Like

Hi @cwerner,

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

import streamlit as st
import time

options = ("male", "female")

a = st.empty()

value = a.radio("gender", options, 0)

st.write(value)

time.sleep(2)

value = a.radio("gender", options, 1)

st.write(value)

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?

Cheers

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

value = st.radio(“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 = st.radio(“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)

st.header("testing")

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 = x.radio("options",["paris", "milan", "NY"], self.index, key=self.radio_key_str)

    def reset(self):
        self.__init__(session_state)

    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
        x.empty()
        self.reset()


params = params(session_state)


st.write(params.index)
st.write(params.val)

if st.button("set to default"):
    params.index = 1
    params.update_session(session_state)
    st.write(params.index)
    st.write(params.val)

st.write("end")

Hi @cwerner,

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

See video at https://youtu.be/kkW7-PBJ0m8

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 https://streamlit.io/docs/api.html#streamlit.empty

1 Like

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 = a.radio("gender", options, 0, key='a')

if value == 'male':
    st.write(value)
elif value == 'female':
    st.write(value)
    time.sleep(1)
    a.radio("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:

Hey @cwerner & @Soufiane_Fartit ,

Have some updates regarding Session State! It’s now natively supported in Streamlit via the 0.84.0 release! :partying_face:

Here are some helpful links:

Looking forward to hearing what you think :balloon:

1 Like