Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

feat: automatically mount Lens when installed (#10814)

Péter Gyarmati20017b56a920
diff --git a/marimo/_runtime/runner/hooks_lens.py b/marimo/_runtime/runner/hooks_lens.py
new file mode 100644
index 0000000000..91a8439780
--- /dev/null
+++ b/marimo/_runtime/runner/hooks_lens.py
@@ -0,0 +1,52 @@
+# Copyright 2026 Marimo. All rights reserved.
+from __future__ import annotations
+
+from typing import TYPE_CHECKING
+
+from marimo import _loggers
+from marimo._runtime import output
+from marimo._runtime.context.utils import running_in_notebook
+from marimo._utils.flatten import contains_instance
+
+if TYPE_CHECKING:
+ from marimo._ast.cell import CellImpl
+ from marimo._runtime.runner.hook_context import PostExecutionHookContext
+ from marimo._runtime.runner.result import RunResult
+
+LOGGER = _loggers.marimo_logger()
+
+
+# Imports are cached before notebook execution; mount in the importing cell.
+def mount_lens(
+ cell: CellImpl, ctx: PostExecutionHookContext, result: RunResult
+) -> None:
+ if (
+ not result.success()
+ or not running_in_notebook()
+ or cell.namespace_to_variable("marimo") is None
+ ):
+ return
+
+ try:
+ try:
+ from marimo_lens import Lens # type: ignore[import-not-found]
+ except ModuleNotFoundError as exc:
+ if exc.name != "marimo_lens":
+ raise
+ return
+
+ if any(
+ contains_instance(other.output, Lens)
+ for other in ctx.graph.cells.values()
+ ):
+ return
+
+ lens = Lens()
+ cell.set_output((cell.output, lens))
+ if result.output is not None:
+ output.append(result.output)
+ output.append(lens)
+ except Exception:
+ LOGGER.warning(
+ "Failed to automatically mount marimo-lens", exc_info=True
+ )
diff --git a/marimo/_runtime/runner/hooks_post_execution.py b/marimo/_runtime/runner/hooks_post_execution.py
index eb5904497a..f0b71a46fd 100644
--- a/marimo/_runtime/runner/hooks_post_execution.py
+++ b/marimo/_runtime/runner/hooks_post_execution.py
@@ -49,6 +49,7 @@ from marimo._runtime.control_flow import MarimoInterrupt, MarimoStopError
from marimo._runtime.runner import cell_runner
from marimo._runtime.runner.hook_context import PostExecutionHookContext
from marimo._runtime.runner.hooks import PostExecutionHook
+from marimo._runtime.runner.hooks_lens import mount_lens
from marimo._runtime.side_effect import SideEffect
from marimo._sql.engines.duckdb import (
INTERNAL_DUCKDB_ENGINE,
@@ -560,6 +561,7 @@ POST_EXECUTION_HOOKS: list[PostExecutionHook] = [
_broadcast_outputs,
_reset_matplotlib_context,
_delete_local_variables,
+ mount_lens,
]
diff --git a/tests/_runtime/test_lens_autoload.py b/tests/_runtime/test_lens_autoload.py
new file mode 100644
index 0000000000..46fff5a93b
--- /dev/null
+++ b/tests/_runtime/test_lens_autoload.py
@@ -0,0 +1,189 @@
+# Copyright 2026 Marimo. All rights reserved.
+import sys
+from unittest.mock import patch
+
+import pytest
+
+from marimo._runtime.commands import ExecuteCellCommand
+from tests._runtime._helpers.session import mocked_kernel_session
+
+
[email protected]("expression", ["", "mo.md('hello')"])
+async def test_lens_mount_and_rerun(expression):
+ lens = pytest.importorskip("marimo_lens")
+ with mocked_kernel_session() as session:
+ kernel = session.kernel
+ command = ExecuteCellCommand(
+ cell_id="lens", code=f"import marimo as mo\n{expression}"
+ )
+ await kernel.run([command])
+ assert not kernel.errors
+ first_lens = kernel.graph.cells["lens"].output[1]
+ assert isinstance(first_lens, lens.Lens)
+ outputs = [
+ op.output.data
+ for op in session.streams.stream.cell_notifications
+ if op.cell_id == "lens" and op.output is not None
+ ]
+ if expression:
+ assert "hello" in outputs[-1]
+ assert "marimo-anywidget" in outputs[-1]
+ await kernel.run([command])
+ assert not kernel.errors
+ assert kernel.graph.cells["lens"].output[1] is not first_lens
+ await kernel.run(
+ [ExecuteCellCommand(cell_id="other", code="mo.md('other')")]
+ )
+ assert kernel.graph.cells["other"].output is None
+
+
+async def test_lens_absent():
+ with mocked_kernel_session() as session:
+ with (
+ patch.dict(sys.modules, marimo_lens=None),
+ patch(
+ "marimo._runtime.runner.hooks_lens.LOGGER.warning"
+ ) as warning,
+ ):
+ await session.kernel.run(
+ [
+ ExecuteCellCommand(
+ cell_id="lens", code="import marimo as mo"
+ )
+ ]
+ )
+ assert not session.kernel.errors
+ warning.assert_not_called()
+
+
+ "failure",
+ ["import", "dependency", "constructor", "constructor_missing_module"],
+)
+async def test_broken_lens_does_not_interrupt_notebook(failure):
+ import sys
+ from importlib.machinery import ModuleSpec
+ from types import ModuleType
+ from unittest.mock import Mock
+
+ broken_lens = ModuleType("marimo_lens")
+ broken_lens.__spec__ = ModuleSpec("marimo_lens", loader=None)
+ if failure == "dependency":
+ broken_lens.__getattr__ = Mock(
+ side_effect=ModuleNotFoundError(name="anywidget")
+ )
+ elif failure in {"constructor", "constructor_missing_module"}:
+
+ class BrokenLens:
+ def __init__(self):
+ if failure == "constructor":
+ raise RuntimeError("broken Lens")
+ raise ModuleNotFoundError(name="marimo_lens")
+
+ broken_lens.Lens = BrokenLens
+
+ with mocked_kernel_session() as session:
+ with (
+ patch.dict(sys.modules, marimo_lens=broken_lens),
+ patch(
+ "marimo._runtime.runner.hooks_lens.LOGGER.warning"
+ ) as warning,
+ ):
+ await session.kernel.run(
+ [
+ ExecuteCellCommand(
+ cell_id="import",
+ code="import marimo as mo\nmo.md('original output')",
+ ),
+ ExecuteCellCommand(
+ cell_id="dependent",
+ code="answer = mo.md('still runs')",
+ ),
+ ]
+ )
+ assert not session.kernel.errors
+ assert "answer" in session.kernel.globals
+ outputs = [
+ op.output.data
+ for op in session.streams.stream.cell_notifications
+ if op.cell_id == "import" and op.output is not None
+ ]
+ assert "original output" in outputs[-1]
+ warning.assert_called_once()
+
+
+async def test_lens_lifetime_preserves_cell_output():
+ import gc
+ import sys
+ import weakref
+ from importlib.machinery import ModuleSpec
+ from types import ModuleType
+
+ from marimo._plugins.ui._core.ui_element import UIElement
+
+ instances = []
+
+ class Lens:
+ def __init__(self):
+ instances.append(weakref.ref(self))
+
+ def _repr_html_(self):
+ return "<span>Lens</span>"
+
+ module = ModuleType("marimo_lens")
+ module.__spec__ = ModuleSpec("marimo_lens", loader=None)
+ module.Lens = Lens
+
+ with mocked_kernel_session() as session:
+ with patch.dict(sys.modules, marimo_lens=module):
+ await session.kernel.run(
+ [
+ ExecuteCellCommand(
+ cell_id="lens",
+ code="import marimo as mo\nmo.ui.button()",
+ )
+ ]
+ )
+ gc.collect()
+ assert instances[0]() is not None
+ cell = session.kernel.graph.cells["lens"]
+ assert isinstance(cell.output[0], UIElement)
+ previous_output = weakref.ref(cell.output[0])
+
+ await session.kernel.run(
+ [ExecuteCellCommand(cell_id="lens", code="1")]
+ )
+ gc.collect()
+ assert instances[0]() is None
+ assert previous_output() is None
+
+
+async def test_lens_mounts_once_per_notebook():
+ lens = pytest.importorskip("marimo_lens")
+ from marimo._runtime.commands import DeleteCellCommand
+
+ # Separate kernels must each mount their own Lens.
+ for _ in range(2):
+ with mocked_kernel_session() as session:
+ kernel = session.kernel
+ first = ExecuteCellCommand(
+ cell_id="first", code="import marimo as mo"
+ )
+ second = ExecuteCellCommand(
+ cell_id="second", code="import marimo as other_mo"
+ )
+ await kernel.run([first])
+ mounted = kernel.graph.cells["first"].output[1]
+ assert isinstance(mounted, lens.Lens)
+
+ await kernel.run([second])
+ await kernel.run([second])
+ assert kernel.graph.cells["second"].output is None
+ assert kernel.graph.cells["first"].output[1] is mounted
+
+ await kernel.delete_cell(DeleteCellCommand(cell_id="first"))
+ await kernel.run([second])
+ replacement = kernel.graph.cells["second"].output[1]
+ assert isinstance(replacement, lens.Lens)
+ assert replacement is not mounted
+ assert not kernel.errors