Issues displaying text in a thread

Hello all,

I’ve been having some issues similar to those described in this post. I am using streamlit 1.8.1.

Essentially, I am trying to populate an st.empty() placeholder element which is passed into a thread that takes some time to complete. I would like to populate the empty element within the thread, but the text does not display.

The user’s solution in the previous post involved using async.io – I would like to try and accomplish this using only the threading package.

Here is an example:

import streamlit as st
import time
import threading
from streamlit.scriptrunner import add_script_run_ctx

def thread_target_function(empty_element):
    time.sleep(1)
    empty_element.code("Populate empty element with this")

empty_element = st.empty()
if st.button('Do something in a background thread'):
    compile_thread = threading.Thread(target=thread_target_function, args=(empty_element, ))
    add_script_run_ctx(compile_thread)
    compile_thread.start()

st.text("Render this while the thread is running")

If the time.sleep(1) is not present it seems to work as expected.

Are there any workarounds, or am I missing something?

Thank you for your help.