Any way to slow st.write_stream()

Used to have a nice typewriter effect from streaming openAI but now the response is so fast that it just looks impossibly silly (speed typing that is). Even the demo for Streamlit st.write_stream()'s typewriter effect runs really fast. Any way to slow that down?

You can add a delay in a wrapper iterator.

import time

import numpy as np
import pandas as pd
import streamlit as st

_LOREM_IPSUM = """
Lorem ipsum dolor sit amet, **consectetur adipiscing** elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
"""


def stream_data():
    for word in _LOREM_IPSUM.split(" "):
        yield word + " "
        time.sleep(0.02)

    yield pd.DataFrame(
        np.random.randn(5, 10),
        columns=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"],
    )

    for word in _LOREM_IPSUM.split(" "):
        yield word + " "
        time.sleep(0.02)


def delayed_stream(src, delay):
    for item in src:
        yield (item)
        time.sleep(delay)


if st.button("Stream data"):
    st.write_stream(delayed_stream(src=stream_data(), delay=1))
2 Likes

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