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