Display several pieces of strings incrementally on the same line

It seems that streamlit.write() and streamlit.text() both display the given text starting on a new line. Is there any way to display several pieces of strings on the same line incrementally?

E.g. say I want to build a text generation demo where the model takes ~1 second to generate one word, instead of running the model for tens of seconds and only show the full output at the end, I’d like to instantly print out the word generated every second, but on the same line. Ideally something like

word = model.generate_next_word( '<|start|>')
while word != '<|end|>':
  streamlit.text_on_same_line(word + ' ')  # need to incrementally display a new word at every step
  word  = model.generate_next_word(word)

Hi @Justin1904 -

This is a hilarious hack from @Jonathan_Rhone that shows how to make a scrolling ticker, maybe this will get you close to what you want to do:

import time
import streamlit as st

text = "Welcome to the first day... of the rest... of your life"


t = st.empty()
for i in range(len(text) + 1):
    t.markdown("## %s..." % text[0:i])
    time.sleep(0.1)

It uses st.empty() to make a placeholder, then writes to the placeholder based on the data. You could imagine in your scenario, writing the words to a list, then slicing the list to show the words you want to see.

Thanks a lot for the tip! I just played with this hack a bit and it works well :smile:

1 Like