Issue with auto-saving st.text_area that scrolls to top on every edit

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:

231228-924-chrome

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)
)

Nevermind, its a pretty easy fix, just remove the value=st.session_state.ss_hello_text param from st_text_area().

1 Like

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