Session class is the primary interface for executing code. Each session connects to a notebook room in the daemon.
Constructor
Unique identifier for this session. If not provided, a random UUID is generated.
Multiple
Session objects with the same notebook_id will share the same kernel.Properties
Unique identifier for this notebook session.
Whether connected to the daemon.
Whether a kernel is running.
Environment source (e.g.,
"uv:prewarmed", "conda:pyproject") if kernel is running.Connection Methods
connect()
Connect to the daemon.This is called automatically by
start_kernel() if not already connected.
Respects the RUNTIMED_SOCKET_PATH environment variable if set.start_kernel()
Start a kernel for this session.Type of kernel:
"python" or "deno".Environment source. Options:
"auto": Auto-detect from inline deps or project files"uv:prewarmed": Use prewarmed UV environment"conda:prewarmed": Use prewarmed Conda environment- For Deno kernels, this is ignored
Path to the notebook file on disk. Used for project file detection
(
pyproject.toml, pixi.toml, environment.yml) when env_source is "auto".If a kernel is already running for this session’s
notebook_id, this returns immediately without starting a new one.shutdown_kernel()
Shutdown the kernel.interrupt()
Interrupt the currently executing cell.Document Operations
create_cell()
Create a new cell in the automerge document.The cell source code.
Cell type:
"code", "markdown", or "raw".Position to insert the cell. If not provided, appends at end.
The cell ID (e.g.,
"cell-a1b2c3d4-...")set_source()
Update a cell’s source in the automerge document.The cell ID.
The new source code.
get_cell()
Get a cell from the automerge document.The cell ID.
Cell object with
id, cell_type, source, and execution_count properties.RuntimedError if cell not found.
get_cells()
Get all cells from the automerge document.List of Cell objects.
delete_cell()
Delete a cell from the automerge document.The cell ID to delete.
Execution Methods
execute_cell()
Execute a cell by ID.The cell ID to execute.
Maximum time to wait for execution.
Result with outputs, success status, and execution count.
The daemon reads the cell’s source from the automerge document and executes it. This ensures all clients see the same code being executed.If a kernel isn’t running yet, this will start one automatically.
RuntimedError if not connected, cell not found, or timeout.
run()
Convenience method: create a cell, execute it, and return the result.The code to execute.
Maximum time to wait for execution.
Result with outputs, success status, and execution count.
This is a shortcut that combines
create_cell() and execute_cell(). The cell is written to the automerge document before execution, so other connected clients will see it.queue_cell()
Queue a cell for execution without waiting for the result.The cell ID to execute.
RuntimedError if not connected or cell not found.
Metadata Operations
set_metadata()
Set a metadata value in the automerge document.The metadata key.
The metadata value (typically JSON).
get_metadata()
Get a metadata value from the automerge document.The metadata key.
The metadata value or
None if not set.Context Manager
Sessions work as context managers for automatic cleanup:Complete Example
See Also
AsyncSession
Async version of Session
ExecutionResult
Understanding execution results