I have an example app which is just a text field that auto loads and saves to a local text file. The intent is that any changes made to the text in the st.text_area()
will persist across reruns, page changes, and new sessions.
The saving and loading works great but there is the annoying problem that any time text is edited and the script reruns, the text area scrolls to the top. I’m assuming this is a side-effect of the st.text_area() value being reset? Here is what the problem looks like:
Running the app locally. Streamlit version 1.29.0 and Python 3.8.10
import streamlit as st
HELLO_TEXT_PATH = "hello.txt"
# On first render, load previously saved Hello text_area content from file
if "ss_hello_text" not in st.session_state:
with open(HELLO_TEXT_PATH, "r") as file:
st.session_state.ss_hello_text = file.read()
# Hello text_area, with the users input auto-saved to a file
hello_text = st.text_area(
"Hello",
value=st.session_state.ss_hello_text,
key="ss_hello_text",
on_change=lambda: open(HELLO_TEXT_PATH, "w").write(st.session_state.ss_hello_text)
)