nbqol.outputs#
Tools for capturing and managing output streams.
This module provides utilities for capturing stdout, stderr, and logging output from notebook cells or other code execution.
Functions
|
Context manager to capture all outputs (stdout, stderr, and logs). |
Classes
|
A class representing captured log output with associated log records. |
|
A class representing captured output from stdout or stderr. |
A class for capturing and storing outputs from stdout, stderr, and logging. |
|
|
A thread-safe version of StringIO for capturing output in multithreaded environments. |
- capture_output(apply=True, **kwargs)[source]#
Context manager to capture all outputs (stdout, stderr, and logs).
This function provides a simple interface to capture all outputs in a context, returning them as an OutputCapture object.
- Parameters:
apply (bool) – Whether to actually capture outputs. If False, yields None.
**kwargs – Additional keyword arguments to pass to _capture_all_outputs. verbose (bool): Whether to print a message when capturing starts. allow_print (bool): Whether to allow regular print statements to still appear in the console.
- Yields:
OutputCapture or None – An object containing all captured outputs, or None if apply=False.
Examples
>>> from nbqol.outputs import capture_output >>> with capture_output() as captured: ... print("This will be captured") ... import logging ... logging.warning("This warning will be captured") >>> print(captured.stdout) >>> This will be captured >>> print(captured.logs) >>> WARNING:root:This warning will be captured
- class ThreadSafeStringIO(*args, **kwargs)[source]#
Bases:
StringIOA thread-safe version of StringIO for capturing output in multithreaded environments.
This class extends io.StringIO to provide thread safety by using a lock when writing to the underlying buffer.
- Parameters:
*args – Arguments to pass to io.StringIO
**kwargs – Keyword arguments to pass to io.StringIO
- class CapturedOutput(string_io)[source]#
Bases:
objectA class representing captured output from stdout or stderr.
This class wraps a StringIO object and provides string representation for the captured output.
- Parameters:
string_io (ThreadSafeStringIO) – The StringIO object containing the captured output
- class CapturedLogs(string_io, records)[source]#
Bases:
CapturedOutputA class representing captured log output with associated log records.
This class extends CapturedOutput to include the original log records that were captured, allowing for more detailed inspection.
- Parameters:
string_io (ThreadSafeStringIO) – The StringIO object containing the captured logs
records (list) – List of logging.LogRecord objects that were captured
- class OutputCapture[source]#
Bases:
objectA class for capturing and storing outputs from stdout, stderr, and logging.
This class provides a container for outputs captured from different sources, with properties to access each output type as well as a formatted combined output.
- property stdout#
Get the captured standard output.
- Returns:
An object containing the captured stdout
- Return type:
- property stderr#
Get the captured standard error.
- Returns:
An object containing the captured stderr
- Return type:
- property logs#
Get the captured logs.
- Returns:
An object containing the captured logs and their records
- Return type:
- property text#
Get a formatted string representation of all captured outputs.
This property generates a formatted multi-section string containing all non-empty output sections (stdout, stderr, logs) with appropriate headers.
- Returns:
A formatted string containing all captured outputs
- Return type:
str
Modules