How to make components able to write in the screen

I have that code:

def mywriter(msg: str):
st.markdown(f’

{msg}
')

uploaded_file = st.file_uploader(“Upload your PDF file”, type=[“pdf”])

if uploaded_file is not None:
processor = Processor(uploaded_file.read(),mywriter())

the writer always fail with :
ThreadPoolExecutor-156_2’: missing ScriptRunContext

How can I made that a long process writes partial results or whatever while they are processing the data?

Hi @Eduardo8, welcome to the forum!

It’s hard to know exactly why it’s failing, because it’s not clear what Processor is or what it’s doing. If I do something super simple like this with processor.process(), perhaps that will give you a good example.

My guess is that the issue has something to do with the details of either Processor, or perhaps how you are running your app (because of the “missing ScriptRunContext”). Are you just doing streamlit run my_app.py?

Here’s some basic code that works:

import streamlit as st


def mywriter(msg: str):
    st.markdown(f"{msg}")


class Processor:
    def __init__(self, file, writer):
        self.file = file
        self.writer = writer

    def process(self):
        self.writer(f"Processing file: {self.file.name}")
        bytestring = self.file.read()
        self.writer(f"File size: {len(bytestring)} bytes")


uploaded_file = st.file_uploader("Upload your PDF file", type=["pdf"])

if uploaded_file is not None:
    processor = Processor(uploaded_file, mywriter)
    processor.process()
1 Like

The code of the processor is too big. I will prepare a simple example to show. But the point is I’m using the langgraph library so from langgraph.graph StateGraph. I create a Graph and some nodes had the writer and call them. I suppose that the graph object run some code on another context, and that why it is failing. Is there a way to save the actual context on the writer and using it? I’ve tryied with get_script_run_ctx() but there’s no way to use it after.

You might find this post helpful 🧰 agent-service-toolkit: Full toolkit for running agent as a service built with LangGraph, FastAPI and Streamlit

If not, you might just search for LangGraph on the forum, as there are a few posts about using it with streamlit.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.