Elements ignoring every second edit

Every second edit of my text_input element is being ignored. I’m not sure why.

import streamlit as st

if "text" not in st.session_state:
    st.session_state.text = ""
text = st.session_state.text

new_text = st.text_input("Text", value=text)
st.session_state.text = new_text
st.write("new_text:", new_text)

Steps to reproduce

  1. Run the app
  2. Type “one” into text input. Press enter.
  3. “Result: one” will be displayed
  4. Delete “one” and type “two” into text input. Press enter.
  5. The text input will revert to “one” and “new_text: one” will be displayed

I would have expected the text input to continue showing “two” and to have “Result: two” displayed.

If I then replace “one” with “three” and press enter, everything works as expected. In other words the issue only happens every second edit.

Key thing in the code is that I’m reusing the output of the text_input as its value on the next rerun. I do this by storing it in st.session_state.

If I replace value=text with value="my original value", then the application behaves as I would expect.

Can anyone shed some light on what is happening here?

Running locally.
Streamlit version 1.41.0.
Python version 3.10.15.

Why am I trying to do this you ask? I have a multipage app and am sending the original value from another page so I don’t want to hardcode it.

This is an anti pattern. When you change the value parameter, you are initializing a new widget which deletes information from the previous instance. Instead of shuffling information between the widget and Session State, use a key on the widget. This ties the widget to the key-value pair in Session State.

import streamlit as st

if "text" not in st.session_state:
    st.session_state.text = ""

new_text = st.text_input("Text", key="text")
st.write("new_text:", new_text)