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