Displaying streaming outputs within an component

Hi streamlit developer community, hope you’re doing great.

I wanted to build an application that streams the output training ( I.e generated by the PyTorch trainer ) shown in the container but with limited number of lines at specific time.

I have written the function that stores the output lines in the deque and then flushes them after the given number of logged lines


def stream_output(command: str, maxlen: int) -> None:
    """
    Streams the output of docker commands linewise for an executed runtime command.
    Args:
        command: The docker command to stream the output.
        maxlen: number of lines to be displayed at a time in the output.
    """
    output = deque(maxlen=maxlen)
    
    try:
        with Popen(command,stdout=PIPE,stderr=PIPE,shell=True) as process:
            while len(output) < maxlen:
                line = process.stdout.readline()
                if not line:
                    break
                line = line.decode("utf-8")
                
                st.write(line)
        output.clean()
        process.wait()

        if process.returncode != 0:
            raise Exception(f"Command failed: {command}")
    except Exception as ex:
        print(f"Exception occurred while streaming output: {ex}")

but issue is that st.write()creates every time a new container and I am not able to get output refreshed in the given container . so can anyone give me the reference regarding how to build this component (with some examples), that will be great . thanks.

Try using st.empty. When you create an st.empty container, it can only hold one thing. Each new thing will replace the last. If you need to write more elements, put an st.container inside st.empty and reinitialize the st.container as needed.

1 Like

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