I am running Streamlit 1.44.1 locally on Python 3.11.11. I am using the technique described here for streaming output to a text area, and it works as shown in that post.
My problem is that I am trying to use this technique to stream a kind of log output, so as the text area is updated I want the viewport to follow the latest updates to the text at the bottom.
Instead, what currently happens is that the viewport always resets to the very top of the text on every update, so following the text as it streams in at the bottom is impossible:
Here’s the test code, copied from the original post I linked to above and modified to make the text area a bit shorter.
Test code
import streamlit as st
import time
# Define the text to simulate typing
_LONG_TEXT = """Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
Never gonna say goodbye
Never gonna tell a lie and hurt you
"""
# Define a generator function to yield text incrementally
def stream_text():
current_text = ""
for char in _LONG_TEXT:
current_text += char
yield current_text
time.sleep(0.01) # Adjust the speed of typing
# Define the function to update the text area
def generate_description():
text_generator = stream_text()
text_area = st.empty()
for text in text_generator:
text_area.text_area('Description', value=text, height=80)
time.sleep(0.01) # Adjust the speed of typing
# Ensure the session state has a key for description
if 'description' not in st.session_state:
st.session_state.description = ''
# Display the text area with the current session state value
text_area_placeholder = st.empty()
# Button to start the typing simulation
if st.button('Generate Description'):
generate_description()
Is there a way to get the viewport to “follow” the text as it streams in at the bottom?