Is there a way to output multiline text while in for loop? First line should print in chat_message and then second line should print.
Steps to reproduce
Code snippet:
with st.chat_message("assistant"):
message_placeholder = st.empty()
assistant_response = "Hey there, I am Line 1 of text! \n Hey there, I am Line 2 of text."
# Simulate stream of response with milliseconds delay
full_response = ""
for chunk in assistant_response.split():
full_response += chunk + " "
time.sleep(0.01)
# Add a blinking cursor to simulate typing
message_placeholder.markdown(full_response + "â–Ś")
Debug info
Streamlit version: (get it with $ streamlit version) – 1.27.0
Python version: (get it with $ python --version) – 3.10
I think the reason this isn’t working with your current code is because you have to put two whitespace characters in front of the newline character in order for it to properly appear as a newline in Streamlit (check out this related thread). When you’re splitting the text with assistant_response.split(), you’re getting rid of the spaces, so the newline isn’t rendering because we don’t have the required two spaces before it.
You can see what I mean if you change your code to split on the exclamation point instead:
with st.chat_message("assistant"):
message_placeholder = st.empty()
assistant_response = "Hey there, I am Line 1 of text! \nHey there, I am Line 2 of text."
# Simulate stream of response with milliseconds delay
full_response = ""
for chunk in assistant_response.split('!'):
full_response += chunk + " "
time.sleep(0.01)
# Add a blinking cursor to simulate typing
message_placeholder.markdown(full_response + "â–Ś")
It directly splits at the ! mark. So the chunk would be just two lines (sentences). Is there a way to split every words and ! mark at end and then line 2?
To keep the original chunks, you could still split on the spaces but preserve them:
import streamlit as st
import time
import re
with st.chat_message("assistant"):
message_placeholder = st.empty()
assistant_response = "Hey there, I am Line 1 of text! \nHey there, I am Line 2 of text."
# Simulate stream of response with milliseconds delay
full_response = ""
for chunk in re.split(r'(\s+)', assistant_response):
full_response += chunk + " "
time.sleep(0.01)
# Add a blinking cursor to simulate typing
message_placeholder.markdown(full_response + "â–Ś")
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.