Using a selectbox and text_input, how does one reset the text_input default value back to it’s original state if a different option within selectbox is chosen?
Steps to reproduce
Run the code snippet below
Change the text_input value from 5 to 10
Change the selectbox from Project 1 to Project 2
Code snippet:
import streamlit as st
def on_text_input_change():
current_input = st.session_state.change_input
prev_input = st.session_state.prev_input
if current_input != prev_input:
del st.session_state['change_input']
def change_input(calc_default):
default = st.text_input('Change Field', calc_default, key='change_input')
return default
def choose_project():
projects = st.selectbox('Project', ['Project 1', 'Project 2', 'Project 3'], key='project', on_change=on_text_input_change)
return projects
calc_def = 5
st.session_state.prev_input = calc_def
choosen_project = choose_project()
input_text = change_input(calc_def)
for key, value in st.session_state.items():
st.write(f"{key}: {value}")
Expected behavior:
The text_input defualt value changes back to default value when a new selectbox option is selected.
Actual behavior:
You can see the session state prev_input and change_input update as you alter the two widgets. Yet, when selectbox widget is updated, the text_input value does not change back to the default value.
Hey @Binx,
I made some changes on given code snippet.
import streamlit as st
def on_text_input_change():
current_input = st.session_state.change_input
prev_input = st.session_state.prev_input
if current_input != prev_input:
st.session_state.prev_input = current_input
def change_input(calc_default):
default = st.text_input('Change Field', calc_default, key='change_input')
return default
def choose_project():
projects = st.selectbox('Project', ['Project 1', 'Project 2', 'Project 3'], key='project', on_change=on_text_input_change)
return projects
calc_def = 5
if 'prev_input' not in st.session_state:
st.session_state.prev_input = calc_def
choosen_project = choose_project()
input_text = change_input(st.session_state.prev_input)
for key, value in st.session_state.items():
st.write(f"{key}: {value}")
When the code is run initially, it checks the prev_input is not in st.session_state. If not, initializes prev_input returns to its default value.
This script will slove your problem!
I don’t think del st.session_state['change_input'] is going to make streamlit forget the state of the textbox. You need to explicitly change the state to the desired value: st.session_state["change_input"] = calc_def.
Now you can only set the state of a textbox to a string. You can just set calc_def = "5" or if you really need the number, use str(calc_def).
Streamlit will complain with a warning because you are setting the textbox state both with the value parameter and using the key, so you better remove the value: st.text_input('Change Field', key='change_input').
By doing that you no longer have an initial value for the textbox, you can add st.session_state.setdefault("change_input", calc_default) right before instantiating the textbox.
There is still room for improvement: on_text_input_change should be on_project_change and it should take the default value as a parameter, change_input is a weird name, st.session_state.prev_input is always equal to calc_def so it is not needed, etc. But it works as is.
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.