This piece of code is writing text from text_area into above the text area.
import streamlit as st
def update():
if "text_area" in st.session_state:
st.write(st.session_state.text_area)
else:
st.write(st.session_state.temp_text)
st.session_state.temp_text = "hello "
st.session_state["text_area"] = st.text_area("example test", "hello form the textbox", on_change=update())
I am wondering why text_area is not reset to “hello form the textbox” whenever script is rerun, but it keep text that I typed in it.
import streamlit as st
# Initialize the session state variable
if "text_area" not in st.session_state:
st.session_state.text_area = "hello from the textbox"
def update():
# your code
st.session_state.temp_text = "hello "
# Display the text area using the session state variable
st.session_state["text_area"] = st.text_area("example test", st.session_state.text_area, on_change=update) # make update() function callable too
This behavior is like this because Streamlit’s session state is designed a bit differently.
Thanks this explain why it run code once before its being initiated.
I still do not understand why st.session_state[“text_area”] = st.text_area(“example test”, “hello form the textbox”, on_change=update) do not reset value of st.text_area to “hello form the textbox” given that is being executed after the update function when I change type different text to text_area
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.