|
|
import streamlit as st |
|
|
import io |
|
|
from contextlib import contextmanager |
|
|
import contextlib |
|
|
import re |
|
|
|
|
|
@contextmanager |
|
|
def st_capture(output_func): |
|
|
with io.StringIO() as stdout, contextlib.redirect_stdout(stdout): |
|
|
old_write = stdout.write |
|
|
|
|
|
def new_write(string): |
|
|
ret = old_write(string) |
|
|
output_func(stdout.getvalue()) |
|
|
return ret |
|
|
|
|
|
stdout.write = new_write |
|
|
yield |
|
|
|
|
|
class TqdmToStreamlit(io.StringIO): |
|
|
""" |
|
|
A custom file-like object that redirects tqdm's output to Streamlit's st.progress |
|
|
and st.text widgets. |
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, text_element): |
|
|
super().__init__() |
|
|
self.text_element = text_element |
|
|
|
|
|
def write(self, buf): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.text_element.text(buf.strip()) |
|
|
|
|
|
def flush(self): |
|
|
pass |
|
|
|
|
|
class QueueIO(io.StringIO): |
|
|
""" |
|
|
A custom file-like object that writes to a queue. |
|
|
Used to capture stdout/stderr. |
|
|
""" |
|
|
def __init__(self, q): |
|
|
self.queue = q |
|
|
|
|
|
def write(self, buf): |
|
|
self.queue.put(buf) |
|
|
|
|
|
def flush(self): |
|
|
pass |