Hi,
My Streamlit app should copy ContextVar. This is a minimal example code for it.
// test.py
// This script is running locally in Python 3.11 and Streamlit 1.32.0
// streamlit run --server.address localhost test.py
import streamlit as st
from contextvars import ContextVar, copy_context
MY_VAR = ContextVar("my_var")
MY_VAR.set("test")
def on_click():
# LookupError: <ContextVar name='my_var' at 0x7fb7a8333600>
print(f"MY_VAR: {MY_VAR.get()}")
st.title("Test")
st.button("button", on_click=on_click)
If we click the button in this script, it will get LookupError because the MY_VAR is not copied to the Streamlit thread. Is there any method in Streamlit to copy this context like?
ctx = copy_context()
def init_ctx(ctx):
for val, value in ctx.items():
val.set(value)
# There is no function in reality, but to describe my problem
# st.init_thread(init_ctx)