π₯ Β Β Capture
Submitted by Alexander Martin
Summary
Capture utility extensions for the standard streamlit library
Functions
redirect
Redirect STDOUT and STDERR to streamlit functions.
Source code in src/streamlit_extras/capture/__init__.py
Import:
- You should add this to the top of your .py file
stdout
Capture STDOUT and redirect it to a callable `dst`
Args:
dst (Callable): A function callable with a single string argument. The entire captured contents will be
passed to this function every time a new string is written. It is designed to be compatible with
st.empty().* functions as callbacks.
terminator (str, optional): If a `terminator` is specified, it is added onto each call to stdout.write/print.
This defaults to a newline which causes them to display on separate lines within an st.empty.write `dst.
If using this with st.empty.code as `dst` it is recommended to set `terminator` to empty string. Defaults to "
".
Source code in src/streamlit_extras/capture/__init__.py
Import:
- You should add this to the top of your .py file
stderr
Capture STDERR and redirect it to a callable dst
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dst |
callable[str]
|
A funciton callable with a single string argument. The entire captured contents will be passed to this function every time a new string is written. It is designed to be compatible with st.empty().* functions as callbacks. |
required |
terminator |
(optional, str)
|
If a |
'\n'
|
Source code in src/streamlit_extras/capture/__init__.py
Import:
- You should add this to the top of your .py file
logcapture
Redirect logging to a streamlit function call dst
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dst |
callable[str]
|
A function callable with a single string argument. The entire log contents will be passed to this function every time a log is written. It is designed to be compatible with st.empty().* functions as callbacks. |
required |
terminator |
(optional, str)
|
If a |
'\n'
|
from_logger |
(optional, Logger or logger)
|
The logger from which logs will be captured.
Defaults to |
None
|
formatter |
(optional, Formatter)
|
If specified, the specified formatter will be added to the logging handler to control how logs are displayed. |
None
|
Source code in src/streamlit_extras/capture/__init__.py
Import:
- You should add this to the top of your .py file
Examples
example_stdout
def example_stdout():
output = st.empty()
with stdout(output.code, terminator=""):
print("This is some captured stdout")
print("How about that, Isn't it great?")
if st.button("Click to print more"):
print("You added another line!")
Output (beta)
example_stderr
def example_stderr():
output = st.empty()
with stderr(output.code, terminator=""):
print("This is some captured stderr", file=sys.stderr)
print(
"For this example, though, there aren't any problems...yet", file=sys.stderr
)
if st.button("Throw an error!"):
print("ERROR: Task failed successfully", file=sys.stderr)
print("Psst....stdout isn't captured here")
Output (beta)
example_logcapture
def example_logcapture():
logger = logging.getLogger("examplelogger")
logger.setLevel("DEBUG")
with logcapture(st.empty().code, from_logger=logger):
logger.error("Roses are red")
logger.info("Violets are blue")
logger.warning("This warning is yellow")
logger.debug("Your code is broke, too")