Multiple simultaneous `write_stream` elements running at the same time

So let’s say I have two of these in the app, and want to show them side by side. The issue is that they’re streamed one after the other, rather than simultaneously.

For example:

import time
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.03)


if st.button("Stream data"):
    col1, col2 = st.columns(2)

    with col1:
        st.write_stream(stream_data)

    with col2:
        st.write_stream(stream_data)

Works just fine but it does the first column, then the second column. Is there a way to get these to fill in simultaneously?

It is not as straightforward as it should be (streamlit==1.33.0), but it can be done after tinkering a bit with futures and threads:

simul_cols

Code:
from concurrent.futures import ThreadPoolExecutor
from threading import currentThread

import streamlit as st
from streamlit.runtime.scriptrunner.script_run_context import (
    add_script_run_ctx,
    get_script_run_ctx,
)

from time import sleep

_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 generate_data():
    """A generator to pass to st.write_stream"""
    for word in _LOREM_IPSUM.split(" "):
        yield word + " "
        sleep(0.1)


def stream_data_in_column(column, ctx):
    """Populate columns simultaneously"""

    add_script_run_ctx(currentThread(), ctx)
    with column:
        st.write_stream(generate_data)


def main():
    if st.button("Stream data"):
        # Define layout
        columns = st.columns(2)

        # Submit concurrent tasks
        with ThreadPoolExecutor(max_workers=2) as executor:
            ctx = get_script_run_ctx()
            futures = [
                executor.submit(stream_data_in_column, col, ctx) for col in columns
            ]


if __name__ == "__main__":
    main()


I believe this could be way simpler if st.write_stream accepted async_generators and was not restricted to regular generators. There is an enhancement request for that that could use some upvotes: Add support for async generators in `write_stream` · Issue #8161 · streamlit/streamlit · GitHub :+1:

2 Likes

That’s a very neat way of doing it, thanks! And agreed, accepting async generators would be a very clean way of doing it. Upvoted.

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