stanza.jupyter.startup

Automatic logging of Jupyter notebook cell I/O to disk.

Hooks stdout, stderr, and exceptions. Writes to notebook_name.log if the notebook path can be discovered via the Jupyter sessions API, otherwise jupyter_session.log.

Module Contents

Classes

NameDescription
_TeeTee stream wrapper that duplicates writes to a log file.

Functions

NameDescription
_get_ipythonGet the current IPython instance if running in IPython.
_in_ipykernelCheck if the IPython instance is running in a Jupyter kernel.
_kernel_idExtract the kernel ID from the IPython kernel connection file.
_resolve_notebook_pathFind the notebook path by querying running Jupyter servers for the kernel ID.
_appendAppend text to the log file, silently ignoring write failures.
_install_loggingInstall logging hooks for stdout, stderr, and IPython exceptions.
mainEntry point: set up auto-logging for the current Jupyter kernel session.

API

1stanza.jupyter.startup._get_ipython() -> typing.Any | None

Get the current IPython instance if running in IPython.

Returns:

IPython instance if available, None otherwise

1stanza.jupyter.startup._in_ipykernel(ip: typing.Any) -> bool

Check if the IPython instance is running in a Jupyter kernel.

Parameters:

ip
typing.Any

IPython instance to check

Returns:

True if running in a Jupyter kernel, False otherwise

1stanza.jupyter.startup._kernel_id(ip: typing.Any) -> str | None

Extract the kernel ID from the IPython kernel connection file.

Parameters:

ip
typing.Any

IPython instance containing kernel configuration

Returns:

Kernel ID string if found, None otherwise

1stanza.jupyter.startup._resolve_notebook_path(kernel_id: str) -> pathlib.Path | None

Find the notebook path by querying running Jupyter servers for the kernel ID.

Iterates through all running Jupyter servers and queries their sessions API to match the kernel ID with an active notebook session.

Parameters:

kernel_id
str

Kernel ID to search for

Returns:

Absolute path to the notebook file if found, None otherwise

1stanza.jupyter.startup._append(
2 path: pathlib.Path, text: str
3) -> None

Append text to the log file, silently ignoring write failures.

Parameters:

path
pathlib.Path

Path to the log file

text
str

Text content to append

1class stanza.jupyter.startup._Tee(original: typing.Any, log_path: pathlib.Path)

Tee stream wrapper that duplicates writes to a log file.

Acts as a transparent proxy for a stream (like sys.stdout or sys.stderr) while also writing all output to a log file.

Initialization

Initialize tee with the original stream and log file path.

Parameters:

Parameters:

  • original: Original stream to wrap (e.g., sys.stdout)
  • log_path: Path to the log file for duplicated output
1write(text: str) -> int

Write text to both the original stream and the log file.

Parameters:

text
str

Text content to write

Returns:

Number of characters written to the original stream

1flush() -> None

Flush the original stream, ignoring errors.

1__getattr__(name: str) -> typing.Any

Delegate attribute access to the original stream.

Parameters:

name
str

Attribute name to access

Returns:

Value of the attribute from the original stream

1stanza.jupyter.startup._install_logging(
2 ip: typing.Any, log_file: pathlib.Path
3) -> None

Install logging hooks for stdout, stderr, and IPython exceptions.

Writes a header to the log file, wraps sys.stdout and sys.stderr with _Tee instances, and hooks IPython’s showtraceback method to log exception tracebacks.

Parameters:

ip
typing.Any

IPython instance to install hooks on

log_file
pathlib.Path

Path to the log file for output

1stanza.jupyter.startup.main() -> None

Entry point: set up auto-logging for the current Jupyter kernel session.

Discovers the notebook path from the running Jupyter servers, creates a log file, and installs logging hooks. Falls back to jupyter_session.log if the notebook path cannot be determined.