I’ve read the streamlit docs for statefullness apps, but the only examples they gave was by using a button with on_click, but they didn’t add even a single one with on_change, so I didn’t get the right way to use it, could you please show me an example?
I tried using something like: answer = st.radio(on_change=name_funtion, args=(answer)) but it says that it cannot use answer as args cause it wans’t declared yet, so how would I get the input from the user?
I realized you might not even need a callback. Here is a minimal example, I hope it can help you get started. Let me know if it doesn’t work for your use case.
import streamlit as st
if "text" not in st.session_state:
st.session_state["text"] = "Left"
st.write(st.session_state["text"])
st.radio("Side", ["Left", "Right"], key="text")
Actually I had to write a little more of code to work on my case. I my project I needed to do some conditionals to change the output on the header, not just get the exact input from the radio option.
So I did something like this and it worked:
import streamlit as st
if 'text' not in st.session_state:
st.session_state.text = 'Left'
if st.session_state.text == 'Left':
title = 'Left Foo'
else:
title = 'Right Foo'
st.write(f' ### The | {title}')
st.radio('Side', ['Left', 'Right'], key='text')
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.