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:
Code:
from concurrent.futures import ThreadPoolExecutor
from threading import currentThread
import streamlit as st
from streamlit.runtime.scriptrunner_utils.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()