> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.conductorquantum.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.conductorquantum.com/_mcp/server.

# 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

| Name | Description |
|------|-------------|
| [`_Tee`](#stanzajupyterstartup_tee) | Tee stream wrapper that duplicates writes to a log file. |

### Functions

| Name | Description |
|------|-------------|
| [`_get_ipython`](#stanzajupyterstartup_get_ipython) | Get the current IPython instance if running in IPython. |
| [`_in_ipykernel`](#stanzajupyterstartup_in_ipykernel) | Check if the IPython instance is running in a Jupyter kernel. |
| [`_kernel_id`](#stanzajupyterstartup_kernel_id) | Extract the kernel ID from the IPython kernel connection file. |
| [`_resolve_notebook_path`](#stanzajupyterstartup_resolve_notebook_path) | Find the notebook path by querying running Jupyter servers for the kernel ID. |
| [`_append`](#stanzajupyterstartup_append) | Append text to the log file, silently ignoring write failures. |
| [`_install_logging`](#stanzajupyterstartup_install_logging) | Install logging hooks for stdout, stderr, and IPython exceptions. |
| [`main`](#stanzajupyterstartupmain) | Entry point: set up auto-logging for the current Jupyter kernel session. |

### API

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

Get the current IPython instance if running in IPython.

**Returns:**

IPython instance if available, None otherwise


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

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

**Parameters:**

<ParamField path="ip" type="typing.Any">
  IPython instance to check
</ParamField>

**Returns:**

True if running in a Jupyter kernel, False otherwise


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

Extract the kernel ID from the IPython kernel connection file.

**Parameters:**

<ParamField path="ip" type="typing.Any">
  IPython instance containing kernel configuration
</ParamField>

**Returns:**

Kernel ID string if found, None otherwise


```python
stanza.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:**

<ParamField path="kernel_id" type="str">
  Kernel ID to search for
</ParamField>

**Returns:**

Absolute path to the notebook file if found, None otherwise


```python
stanza.jupyter.startup._append(
    path: pathlib.Path, text: str
) -> None
```

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

**Parameters:**

<ParamField path="path" type="pathlib.Path">
  Path to the log file
</ParamField>

<ParamField path="text" type="str">
  Text content to append
</ParamField>


```python
class 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


```python
write(text: str) -> int
```

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

**Parameters:**

<ParamField path="text" type="str">
  Text content to write
</ParamField>

**Returns:**

Number of characters written to the original stream


```python
flush() -> None
```

Flush the original stream, ignoring errors.


```python
__getattr__(name: str) -> typing.Any
```

Delegate attribute access to the original stream.

**Parameters:**

<ParamField path="name" type="str">
  Attribute name to access
</ParamField>

**Returns:**

Value of the attribute from the original stream


```python
stanza.jupyter.startup._install_logging(
    ip: typing.Any, log_file: pathlib.Path
) -> 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:**

<ParamField path="ip" type="typing.Any">
  IPython instance to install hooks on
</ParamField>

<ParamField path="log_file" type="pathlib.Path">
  Path to the log file for output
</ParamField>


```python
stanza.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.