Skip to content

Add the Python interfaces for host API extensions - #12251

Open
manzt wants to merge 2 commits into
notebook-extensions/foundationfrom
notebook-extensions/scope
Open

manzt wants to merge 2 commits into
notebook-extensions/foundationfrom
notebook-extensions/scope

Conversation

@manzt

@manzt manzt commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

This PR adds the Python API for implementing a notebook extension. From an extension definition, marimo generates the record type, a typed Call and Result for each operation, and the Handlers protocol the extension implements.

The host calls open(scope) once per extension per loaded notebook. It uses the handlers that open yields for requests from all clients and exits open when the notebook closes. The scope survives runtime restarts.

class NotebookScope(Generic[Record]):
    notebook: Notebook
    runtime: RuntimeAccess

    # The extension’s current record, initially None.
    @property
    def record(self) -> Record | None: ...

    # Retain the record for snapshots. Publish it when the value changes.
    def put(self, record: Record, *, request_id: str | None = None) -> Record: ...

    # Drop the record from future snapshots without sending a removal event.
    def clear(self) -> None: ...


class RuntimeAccess:
    # Send a command to the attached kernel. Raises Conflict if none is attached.
    def send(self, command: CommandMessage) -> None: ...

    # Register during open(). The host enters bind(runtime_id) when a session
    # attaches and exits it when that session detaches.
    def while_ready(
        self, bind: Callable[[RuntimeId], AbstractContextManager[None]]
    ) -> AbstractContextManager[None]: ...

The breakpoint extension in #12225 uses while_ready to publish an empty breakpoint set for each new runtime and put to share subsequent edits with connected clients.

@manzt manzt added enhancement New feature or request host-protocol Related to the marimo host protocol or it's implementation. labels Dec 3, 2026
@manzt
manzt added this pull request to stack #12252 December 3, 2026 01:11
@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Report for ./frontend

Status Category Percentage Covered / Total
🔵 Lines 69.97% 29191 / 41718
🔵 Statements 69.92% 29766 / 42566
🔵 Functions 65.49% 7050 / 10765
🔵 Branches 65.42% 17135 / 26192
File CoverageNo changed files found.
Generated in workflow #26592 for commit 94ba32b by the Vitest Coverage Report Action

@manzt
manzt force-pushed the notebook-extensions/scope branch from 77e3b8b to 1e5221d Compare December 3, 2026 19:07
@manzt
manzt force-pushed the notebook-extensions/scope branch 2 times, most recently from 217b953 to fc12173 Compare December 3, 2026 19:14
@manzt
manzt marked this pull request as ready for review December 3, 2026 19:16
Copilot AI balanced review requested due to automatic review settings December 3, 2026 19:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Generated Python can fail on import due to missing imports and incorrect local-schema references, while extension lifecycle failures can leave bindings attached and prevent complete cleanup.

6 open issues
What changed in this PR

Adds Python support for notebook extensions, including generated handler interfaces, notebook-scoped records, and runtime lifecycle hooks that survive kernel restarts.

Changes:

  • Generate Python models, typed calls/results, event factories, and handler dispatch.
  • Integrate extension lifetimes and retained records with host sessions and snapshots.
  • Add lifecycle and generation tests, documentation, and CI coverage.
File Description
tests/​_server/​host/​test_notebook_extensions.py Tests extension lifecycle and snapshots.
packages/​host-protocol/​src/​__tests__/​python.test.ts Tests extension binding generation.
packages/​host-protocol/​scripts/​python/​models.ts Extracts model definitions and supports dotted fields.
packages/​host-protocol/​scripts/​python/​extension.ts Generates extension-specific Python bindings.
packages/​host-protocol/​scripts/​python/​calls.ts Shares operation generation and resolves local types.
packages/​host-protocol/​scripts/​python.ts Writes registered extension modules.
packages/​host-protocol/​README.md Documents Python binding support.
marimo/​_server/​host/​session.py Includes extensions in initial session events.
marimo/​_server/​host/​bridge.py Manages extension lifetimes and snapshots.
marimo/​_server/​host/​backend.py Passes runtime details to attachment callbacks.
marimo/​_server/​host/​api.py Delegates initial events to the bridge.
marimo/​_server/​extensions/​scope.py Provides retained records and runtime access.
marimo/​_server/​extensions/​notebook.py Manages installed extensions and runtime bindings.
marimo/​_server/​extensions/​__init__.py Introduces the extension package.
marimo/​_core/​stream.py Allows event-name lookup from extension classes.
.github/​workflows/​test_host_protocol.yaml Triggers checks for generated extension bindings.

Review effort: Balanced

Comment thread marimo/_server/extensions/notebook.py Resolved
Comment thread marimo/_server/extensions/notebook.py Outdated
Comment on lines +63 to +64
for runtime in self._runtimes:
runtime._detach()
Comment thread marimo/_server/host/bridge.py Resolved
Comment on lines +887 to +888
if extensions := self._extensions.get(notebook_id):
extensions.detach()
@manzt
manzt force-pushed the notebook-extensions/scope branch from fc12173 to a201d0b Compare December 3, 2026 19:56
@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

React Doctor skipped this pull request — it changed no React files.

Reviewed by React Doctor for commit 94ba32b.

@manzt manzt changed the title Add the Python API for notebook extensions Add the Python interfaces for host API extensions Dec 3, 2026
@manzt manzt added internal A refactor or improvement that is not user facing and removed enhancement New feature or request labels Dec 3, 2026
manzt added 2 commits December 3, 2026 15:56
So far the host has nowhere to keep the state of a marimo extension. These changes add `NotebookExtensions`, which opens each extension once per loaded editor notebook and hands it a `NotebookScope`.

    scope.put(record, request_id=key)
    scope.runtime.send(command)

The scope retains the extension's record, publishes it when it changes, and supplies it to snapshots and resets. `scope.runtime.while_ready(bind)` enters `bind` for each runtime while it is ready, and `send` reaches that runtime's kernel.
@manzt
manzt force-pushed the notebook-extensions/scope branch from a201d0b to 94ba32b Compare December 3, 2026 20:56
@marthacryan

Copy link
Copy Markdown
Member

Could you elaborate on the role of "clear"? Why does it not send a removal event?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

host-protocol Related to the marimo host protocol or it's implementation. internal A refactor or improvement that is not user facing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants