Why reruning script doeas not reset the widget?

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.

Hey could you please send github repo link and a image where the problem is happening?

Sorry pressed post accidentally before finishing the post.

No problem, with this could you also share a picture of the overriding, please?

Currently, you are calling the function and then passing None to the on_change argument. You need to pass the callable:

st.session_state["text_area"] = st.text_area("example test", "hello form the textbox", on_change=update)

Try this one:

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.

Hi, sorry no particular picture since I just wanted to understand code behavior.

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

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.