Hello,
I would like to ask if there is any way to use write_stream in text_area or code component?
Suppose when the button is clicked, so that text simulating typing is entered into the text_area/code?
Pseudocode:
You can simulated the stream effect by updating the text area for each word.
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=150)
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()
Thank you very much! This solves my problem - I was trying to achieve something similar, but somewhere I must have had a mistake in the logic. Thank you again!
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.