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
- Run the app
- Type “one” into text input. Press enter.
- “Result: one” will be displayed
- Delete “one” and type “two” into text input. Press enter.
- 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.