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

capture_output([apply])

Context manager to capture all outputs (stdout, stderr, and logs).

Classes

CapturedLogs(string_io, records)

A class representing captured log output with associated log records.

CapturedOutput(string_io)

A class representing captured output from stdout or stderr.

OutputCapture()

A class for capturing and storing outputs from stdout, stderr, and logging.

ThreadSafeStringIO(*args, **kwargs)

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: StringIO

A 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

write(*args, **kwargs)[source]#

Thread-safe write operation to the StringIO buffer.

Parameters:
  • *args – Arguments to pass to the parent write method

  • **kwargs – Keyword arguments to pass to the parent write method

Returns:

The return value from the parent write method

class CapturedOutput(string_io)[source]#

Bases: object

A 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: CapturedOutput

A 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: object

A 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.

__init__()[source]#

Initialize an OutputCapture object with empty buffers for each output type.

property stdout#

Get the captured standard output.

Returns:

An object containing the captured stdout

Return type:

CapturedOutput

property stderr#

Get the captured standard error.

Returns:

An object containing the captured stderr

Return type:

CapturedOutput

property logs#

Get the captured logs.

Returns:

An object containing the captured logs and their records

Return type:

CapturedLogs

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

capture