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...
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.