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()
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.