I have a streamlit UI as follows
import streamlit as st
import time
_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.
"""
@st.experimental_fragment(run_every=0.2)
def counter ():
if "i" not in st.session_state:
st.session_state.i = 0
st.write(f"counter: {st.session_state.i}")
st.session_state.i += 1
def stream_data():
for word in _LOREM_IPSUM.split(" "):
yield word + " "
time.sleep(0.2)
@st.experimental_fragment
def writter (stream, frame):
frame.write_stream(stream)
counter()
frame = st.empty()
st.button("Stream data", key="but")
st.write("bottom text")
if st.session_state["but"]:
writter(stream_data, frame)
The idea is that there is a counter that should always be running and, when the user presses the “Stream data” button, the text from the stream starts to appear ChatGPT-style. My problem is that, when the text is being added to the screen the fragment stops executing (that is, the counter freezes). How can I have a fragment running in parallel as the Python script executes?