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.

fix: attach screenshots to the active notebook session (#11049)

Shahmir Varqhadbe84a7491af
diff --git a/.github/workflows/playwright.yml b/.github/workflows/playwright.yml
index 80161381e3..64e6180637 100644
--- a/.github/workflows/playwright.yml
+++ b/.github/workflows/playwright.yml
@@ -119,6 +119,15 @@ jobs:
env:
VITE_MARIMO_VERSION: ${{ env.MARIMO_VERSION }}
+ - name: 🎭 Verify code-mode screenshots with Chromium
+ # Python and JavaScript Playwright releases can differ. Install the
+ # browser revision expected by Python instead of using the image's.
+ run: |
+ uv run --group test --with playwright==1.63.0 python -m playwright install chromium
+ uv run --group test --with playwright==1.63.0 pytest tests/_code_mode/test_screenshot_integration.py
+ env:
+ MARIMO_TEST_SCREENSHOTS: "1"
+
# - name: ☁️ Google Auth
# uses: google-github-actions/auth@c200f3691d83b41bf9bbd8638997a462592937ed # v2
# # Skip on forks
diff --git a/marimo/_code_mode/_context.py b/marimo/_code_mode/_context.py
index ecc4c87968..9602d1f2ba 100644
--- a/marimo/_code_mode/_context.py
+++ b/marimo/_code_mode/_context.py
@@ -60,6 +60,7 @@ from marimo._code_mode._plan import (
)
from marimo._code_mode.screenshot_meta import (
SCREENSHOT_AUTH_TOKEN_KEY,
+ SCREENSHOT_FILE_KEY,
SCREENSHOT_SERVER_URL_KEY,
)
from marimo._messaging.cell_output import CellOutput
@@ -317,8 +318,8 @@ class NotebookCell:
The cell's last main (rich display) output, or `None` if no
output was captured. **Frozen snapshot** — taken at
scratchpad-start, not refreshed when `ctx.run_cell` produces
- new outputs in the same batch. Re-enter `cm.get_context()` to
- see fresh outputs.
+ new outputs in the same batch. Start a new code-mode invocation
+ to see fresh outputs.
console_outputs : list[CellOutput]
Buffered stdout/stderr outputs from the cell's last execution.
Same frozen-snapshot caveat as `output`.
@@ -456,8 +457,8 @@ class NotebookCell:
"""The cell's last main (rich display) output, or `None`.
Frozen at scratchpad-start — does not reflect outputs produced
- by `ctx.run_cell` in the same batch. Re-enter
- `cm.get_context()` to see fresh outputs.
+ by `ctx.run_cell` in the same batch. Start a new code-mode
+ invocation to see fresh outputs.
"""
if self._outputs is None:
return None
@@ -1428,6 +1429,9 @@ class AsyncCodeModeContext:
Launches a headless Chromium browser (reused across calls)
connected to this server in kiosk mode.
+ Captures the current browser-rendered output, which may be newer
+ than the frozen `NotebookCell.output` snapshot.
+
Requires `playwright` + its Chromium binary::
ctx.install_packages("playwright")
@@ -1494,12 +1498,16 @@ class AsyncCodeModeContext:
screenshot_auth_token = cast(
"str | None", request.meta.get(SCREENSHOT_AUTH_TOKEN_KEY)
)
+ screenshot_file = cast(
+ "str | None", request.meta.get(SCREENSHOT_FILE_KEY)
+ )
# Lazy-init the screenshot session (browser reuse).
if self._screenshot_session is None:
self._screenshot_session = _ScreenshotSession(
server_url,
screenshot_auth_token=screenshot_auth_token,
+ file_key=screenshot_file,
)
image = await self._screenshot_session.capture(
diff --git a/marimo/_code_mode/screenshot.py b/marimo/_code_mode/screenshot.py
index aad4f1fc64..9343369a7a 100644
--- a/marimo/_code_mode/screenshot.py
+++ b/marimo/_code_mode/screenshot.py
@@ -11,6 +11,7 @@ import asyncio
import base64
import time
from typing import TYPE_CHECKING, Any
+from urllib.parse import parse_qsl, urlencode, urlsplit, urlunsplit
from marimo import _loggers
from marimo._export._pdf_raster import (
@@ -98,10 +99,15 @@ class _ScreenshotSession:
"""
def __init__(
- self, server_url: str, screenshot_auth_token: str | None = None
+ self,
+ server_url: str,
+ screenshot_auth_token: str | None = None,
+ *,
+ file_key: str | None = None,
) -> None:
self._server_url = server_url
self._screenshot_auth_token = screenshot_auth_token
+ self._file_key = file_key
self._playwright: Any = None
self._browser: Any = None
self._page: Any = None
@@ -109,11 +115,7 @@ class _ScreenshotSession:
async def _ensure_ready(self) -> None:
"""Launch browser and navigate to the notebook if not already done."""
- if self._page is not None:
- return
-
async with self._init_lock:
- # Re-check after acquiring the lock.
if self._page is not None:
return
await self._init_browser()
@@ -152,13 +154,13 @@ class _ScreenshotSession:
page = await context.new_page()
await page.emulate_media(reduced_motion="reduce")
- # Commit only after full success.
- self._playwright = pw
- self._browser = browser
self._page = page
await self._navigate(initial=True)
+ self._playwright = pw
+ self._browser = browser
LOGGER.debug("Screenshot session: ready")
except BaseException:
+ self._page = None
# Clean up partially-created resources.
if browser is not None:
with _suppress():
@@ -171,15 +173,13 @@ class _ScreenshotSession:
"""Navigate (initial=True) or reload (initial=False) the kiosk page."""
assert self._page is not None
- params = "kiosk=true"
- if self._screenshot_auth_token:
- params += f"&access_token={self._screenshot_auth_token}"
- page_url = f"{self._server_url}?{params}"
if initial:
LOGGER.debug(
"Screenshot session: navigating to %s", self._server_url
)
- await self._page.goto(page_url, wait_until="domcontentloaded")
+ await self._page.goto(
+ self._page_url(), wait_until="domcontentloaded"
+ )
else:
LOGGER.debug("Screenshot session: reloading page")
await self._page.reload(wait_until="domcontentloaded")
@@ -200,6 +200,24 @@ class _ScreenshotSession:
except Exception:
pass
+ def _page_url(self) -> str:
+ """Build an authenticated kiosk URL for the active notebook."""
+ url = urlsplit(self._server_url)
+ params = {"kiosk": "true"}
+ # Session IDs also identify consumers. The kiosk must create its
+ # own consumer and join the live session by file key.
+ if self._file_key is not None:
+ params["file"] = self._file_key
+ if self._screenshot_auth_token:
+ params["access_token"] = self._screenshot_auth_token
+ query = [
+ (key, value)
+ for key, value in parse_qsl(url.query, keep_blank_values=True)
+ if key not in params and key != "session_id"
+ ]
+ query.extend(params.items())
+ return urlunsplit(url._replace(query=urlencode(query)))
+
async def capture(
self,
cell_id: CellId_t,
@@ -403,11 +421,16 @@ class _ScreenshotSession:
async def close(self) -> None:
"""Release browser resources."""
- if self._browser is not None:
- await self._browser.close()
+ async with self._init_lock:
+ browser = self._browser
+ playwright = self._playwright
self._browser = None
- if self._playwright is not None:
- await self._playwright.stop()
self._playwright = None
- self._page = None
+ self._page = None
+ try:
+ if browser is not None:
+ await browser.close()
+ finally:
+ if playwright is not None:
+ await playwright.stop()
LOGGER.debug("Screenshot session: closed")
diff --git a/marimo/_code_mode/screenshot_meta.py b/marimo/_code_mode/screenshot_meta.py
index bb3f94e1c4..0285b4acea 100644
--- a/marimo/_code_mode/screenshot_meta.py
+++ b/marimo/_code_mode/screenshot_meta.py
@@ -12,3 +12,4 @@ from __future__ import annotations
SCREENSHOT_SERVER_URL_KEY = "screenshot_server_url"
SCREENSHOT_AUTH_TOKEN_KEY = "screenshot_auth_token"
+SCREENSHOT_FILE_KEY = "screenshot_file"
diff --git a/marimo/_messaging/notebook/outputs.py b/marimo/_messaging/notebook/outputs.py
index f52b2ba8da..e4c9373771 100644
--- a/marimo/_messaging/notebook/outputs.py
+++ b/marimo/_messaging/notebook/outputs.py
@@ -10,8 +10,8 @@ each layer's concerns crisp.
The snapshot is *frozen* at the start of a scratchpad invocation. It
will not reflect outputs produced by `ctx.run_cell` calls in the same
-batch — agents that need fresh outputs should re-enter
-`cm.get_context()`.
+batch — callers that need fresh output snapshots must start a new
+code-mode invocation.
"""
from __future__ import annotations
diff --git a/marimo/_server/api/endpoints/execution.py b/marimo/_server/api/endpoints/execution.py
index 82e7a1a0be..b1cb35d79e 100644
--- a/marimo/_server/api/endpoints/execution.py
+++ b/marimo/_server/api/endpoints/execution.py
@@ -11,6 +11,7 @@ from starlette.responses import JSONResponse, StreamingResponse
from marimo import _loggers
from marimo._code_mode.screenshot_meta import (
SCREENSHOT_AUTH_TOKEN_KEY,
+ SCREENSHOT_FILE_KEY,
SCREENSHOT_SERVER_URL_KEY,
)
from marimo._runtime.commands import HTTPRequest, UpdateUIElementCommand
@@ -383,6 +384,10 @@ async def execute_code(
)
http_req.meta[SCREENSHOT_SERVER_URL_KEY] = server_url
http_req.meta[SCREENSHOT_AUTH_TOKEN_KEY] = auth_token
+ http_req.meta[SCREENSHOT_FILE_KEY] = (
+ session.app_file_manager.path
+ or session.initialization_id
+ )
notebook_cells, cell_outputs = snapshot_for_scratchpad(
session
)
diff --git a/marimo/_server/scratchpad.py b/marimo/_server/scratchpad.py
index 4372846af5..620d5d7814 100644
--- a/marimo/_server/scratchpad.py
+++ b/marimo/_server/scratchpad.py
@@ -11,6 +11,7 @@ from uuid import uuid4
from marimo._ai._tools.types import CodeExecutionResult
from marimo._code_mode.screenshot_meta import (
SCREENSHOT_AUTH_TOKEN_KEY,
+ SCREENSHOT_FILE_KEY,
SCREENSHOT_SERVER_URL_KEY,
)
from marimo._messaging.cell_output import CellChannel
@@ -384,6 +385,9 @@ async def run_scratchpad_code(
http_req = HTTPRequest.from_request(request)
http_req.meta[SCREENSHOT_SERVER_URL_KEY] = server_url
http_req.meta[SCREENSHOT_AUTH_TOKEN_KEY] = auth_token
+ http_req.meta[SCREENSHOT_FILE_KEY] = (
+ session.app_file_manager.path or session.initialization_id
+ )
session.instantiate(
InstantiateNotebookRequest(object_ids=[], values=[], auto_run=False),
diff --git a/tests/_code_mode/test_screenshot.py b/tests/_code_mode/test_screenshot.py
index a3bbbc12c6..32a9ce3c11 100644
--- a/tests/_code_mode/test_screenshot.py
+++ b/tests/_code_mode/test_screenshot.py
@@ -1,9 +1,12 @@
# Copyright 2026 Marimo. All rights reserved.
from __future__ import annotations
+import asyncio
import base64
from types import SimpleNamespace
from typing import TYPE_CHECKING
+from unittest.mock import AsyncMock, MagicMock, patch
+from urllib.parse import parse_qs, urlsplit
import pytest
@@ -17,14 +20,30 @@ from marimo._code_mode.screenshot import (
_ScreenshotSession,
_to_data_url,
)
+from marimo._code_mode.screenshot_meta import (
+ SCREENSHOT_AUTH_TOKEN_KEY,
+ SCREENSHOT_FILE_KEY,
+ SCREENSHOT_SERVER_URL_KEY,
+)
+from marimo._messaging.cell_output import CellChannel, CellOutput
+from marimo._messaging.context import http_request_context
from marimo._messaging.notebook.document import (
NotebookCell as _DocNotebookCell,
+ NotebookDocument,
+ notebook_document_context,
+)
+from marimo._messaging.notebook.outputs import (
+ CellOutputs,
+ notebook_outputs_context,
)
+from marimo._runtime.commands import HTTPRequest
from marimo._types.ids import CellId_t
if TYPE_CHECKING:
from typing import Any
+ from marimo._runtime.runtime import Kernel
+
class TestToDataUrl:
def test_round_trip(self) -> None:
@@ -54,27 +73,40 @@ class TestScreenshotSessionAuthUrl:
def test_page_url_includes_screenshot_auth_token(self) -> None:
"""The kiosk page URL must include the access_token query param."""
session = _ScreenshotSession(
- "http://localhost:9999", screenshot_auth_token="secret"
+ "http://localhost:9999",
+ screenshot_auth_token="secret+&=#?",
+ file_key="notebooks/my notebook + #1.py",
)
- # Replicate the URL-building logic from _ensure_ready.
- params = "kiosk=true"
- if session._screenshot_auth_token:
- params += f"&access_token={session._screenshot_auth_token}"
- page_url = f"{session._server_url}?{params}"
-
- assert "access_token=secret" in page_url
- assert "kiosk=true" in page_url
+ assert parse_qs(urlsplit(session._page_url()).query) == {
+ "access_token": ["secret+&=#?"],
+ "file": ["notebooks/my notebook + #1.py"],
+ "kiosk": ["true"],
+ }
def test_page_url_omits_token_when_none(self) -> None:
session = _ScreenshotSession("http://localhost:9999")
- params = "kiosk=true"
- if session._screenshot_auth_token:
- params += f"&access_token={session._screenshot_auth_token}"
- page_url = f"{session._server_url}?{params}"
+ page_url = session._page_url()
assert "access_token" not in page_url
assert page_url == "http://localhost:9999?kiosk=true"
+ def test_page_url_preserves_base_path_query_and_fragment(self) -> None:
+ session = _ScreenshotSession(
+ "http://localhost:9999/base/?theme=dark&theme=light&kiosk=false"
+ "&session_id=editor&file=old.py&access_token=old#output",
+ screenshot_auth_token="secret",
+ file_key="__new__notebook",
+ )
+ url = urlsplit(session._page_url())
+ assert url.path == "/base/"
+ assert url.fragment == "output"
+ assert parse_qs(url.query) == {
+ "theme": ["dark", "light"],
+ "kiosk": ["true"],
+ "file": ["__new__notebook"],
+ "access_token": ["secret"],
+ }
+
class _FakeCells:
"""Minimal stand-in for `_CellsView` used by the resolver tests.
@@ -181,3 +213,236 @@ class TestResolveScreenshotTarget:
_resolve(ctx, 3.14)
with pytest.raises(TypeError, match="Unsupported"):
_resolve(ctx, object())
+
+
+def doc_cell() -> _DocNotebookCell:
+ return _DocNotebookCell(
+ id=CellId_t("cell-a"),
+ code="value = 1",
+ name="",
+ config=CellConfig(),
+ )
+
+
[email protected]("output", [None, CellOutput.empty()])
[email protected]("new_context", [False, True])
+async def test_screenshot_after_run_ignores_stale_empty_snapshot(
+ k: Kernel,
+ doc_cell: _DocNotebookCell,
+ screenshot_request: HTTPRequest,
+ output: CellOutput | None,
+ new_context: bool,
+) -> None:
+ doc_cell.code = "value = 42\nvalue"
+ outputs = CellOutputs(
+ output={} if output is None else {doc_cell.id: output},
+ console_outputs={},
+ )
+ with (
+ notebook_document_context(NotebookDocument([doc_cell])),
+ notebook_outputs_context(outputs),
+ http_request_context(screenshot_request),
+ patch(
+ "marimo._code_mode.screenshot._ScreenshotSession", autospec=True
+ ) as session,
+ ):
+ session.return_value.capture.return_value = b"png"
+ ctx = AsyncCodeModeContext(k, skip_staleness_check=True)
+ async with ctx:
+ ctx.run_cell(doc_cell.id)
+ assert k.globals["value"] == 42
+ if new_context:
+ ctx = AsyncCodeModeContext(k, skip_staleness_check=True)
+ assert ctx.cells[doc_cell.id].output is output
+ assert await ctx.screenshot(doc_cell.id) == b"png"
+ session.return_value.capture.assert_awaited_once_with(
+ doc_cell.id, timeout_ms=30_000
+ )
+ await ctx.close_screenshot_session()
+
+
+def screenshot_request() -> HTTPRequest:
+ return HTTPRequest(
+ url={"path": "/api/kernel/execute"},
+ base_url={},
+ headers={},
+ query_params={},
+ path_params={},
+ cookies={},
+ meta={
+ SCREENSHOT_SERVER_URL_KEY: "http://localhost:1234/base",
+ SCREENSHOT_AUTH_TOKEN_KEY: "secret",
+ SCREENSHOT_FILE_KEY: "notebooks/my notebook.py",
+ },
+ user={},
+ )
+
+
[email protected]("has_snapshot", [True, False])
+async def test_screenshot_passes_credentials_and_reuses_browser(
+ k: Kernel,
+ doc_cell: _DocNotebookCell,
+ screenshot_request: HTTPRequest,
+ has_snapshot: bool,
+) -> None:
+ outputs = CellOutputs(
+ output={
+ doc_cell.id: CellOutput(
+ channel=CellChannel.OUTPUT, mimetype="text/plain", data="1"
+ )
+ },
+ console_outputs={},
+ )
+ with (
+ notebook_document_context(NotebookDocument([doc_cell])),
+ notebook_outputs_context(outputs if has_snapshot else None),
+ http_request_context(screenshot_request),
+ patch(
+ "marimo._code_mode.screenshot._ScreenshotSession", autospec=True
+ ) as session,
+ ):
+ session.return_value.capture.return_value = b"png"
+ ctx = AsyncCodeModeContext(k)
+ assert await ctx.screenshot(doc_cell.id) == b"png"
+ assert await ctx.screenshot(doc_cell.id, as_data_url=True) == (
+ _to_data_url(b"png")
+ )
+ session.assert_called_once_with(
+ "http://localhost:1234/base",
+ screenshot_auth_token="secret",
+ file_key="notebooks/my notebook.py",
+ )
+ session.return_value.capture.assert_awaited_with(
+ doc_cell.id, timeout_ms=30_000
+ )
+ await ctx.close_screenshot_session()
+ session.return_value.close.assert_awaited_once()
+
+
+async def test_screenshot_rejects_currently_empty_browser_output() -> None:
+ session = _ScreenshotSession("http://localhost:1234")
+ session._page = MagicMock()
+ with (
+ patch.object(session, "_ensure_ready", new_callable=AsyncMock),
+ patch.object(session, "_wait_for_container", new_callable=AsyncMock),
+ patch.object(
+ session,
+ "_container_has_content",
+ new_callable=AsyncMock,
+ return_value=False,
+ ) as has_content,
+ patch.object(
+ session, "_resolve_output_locator", new_callable=AsyncMock
+ ) as resolve_output,
+ ):
+ with pytest.raises(ScreenshotError, match="has no rendered content"):
+ await session.capture(CellId_t("cell-a"))
+ has_content.assert_awaited_once_with("#output-cell-a")
+ resolve_output.assert_not_awaited()
+
+
+def playwright_mock() -> MagicMock:
+ playwright = MagicMock()
+ playwright.stop = AsyncMock()
+ browser = AsyncMock()
+ page = AsyncMock()
+ browser.new_context.return_value.new_page.return_value = page
+ playwright.chromium.launch = AsyncMock(return_value=browser)
+ return playwright
+
+
+ ("stage", "error"),
+ [
+ ("launch", RuntimeError("launch failed")),
+ ("navigation", RuntimeError("navigation failed")),
+ ("navigation", asyncio.CancelledError()),
+ ],
+)
+async def test_browser_initialization_failure_releases_resources_and_retries(
+ playwright_mock: MagicMock, stage: str, error: BaseException
+) -> None:
+ browser = playwright_mock.chromium.launch.return_value
+ page = browser.new_context.return_value.new_page.return_value
+ if stage == "launch":
+ playwright_mock.chromium.launch.side_effect = [error, browser]
+ else:
+ page.goto.side_effect = [error, None]
+ manager = MagicMock(start=AsyncMock(return_value=playwright_mock))
+ session = _ScreenshotSession("http://localhost:1234", file_key="app.py")
+ with patch(
+ "marimo._code_mode.screenshot._require_playwright",
+ return_value=lambda: manager,
+ ):
+ with pytest.raises(type(error)):
+ await session._ensure_ready()
+ assert session._page is None
+ assert session._browser is None
+ assert session._playwright is None
+ if stage == "launch":
+ browser.close.assert_not_awaited()
+ else:
+ browser.close.assert_awaited_once()
+ playwright_mock.stop.assert_awaited_once()
+
+ await session._ensure_ready()
+ page.goto.assert_awaited_with(
+ session._page_url(), wait_until="domcontentloaded"
+ )
+ assert playwright_mock.chromium.launch.await_count == 2
+ await session.close()
+
+
+async def test_concurrent_initialization_waits_for_navigation(
+ playwright_mock: MagicMock,
+) -> None:
+ browser = playwright_mock.chromium.launch.return_value
+ page = browser.new_context.return_value.new_page.return_value
+ navigating = asyncio.Event()
+ finish_navigation = asyncio.Event()
+
+ async def navigate(*_args: object, **_kwargs: object) -> None:
+ navigating.set()
+ await finish_navigation.wait()
+
+ page.goto.side_effect = navigate
+ manager = MagicMock(start=AsyncMock(return_value=playwright_mock))
+ session = _ScreenshotSession("http://localhost:1234")
+ with patch(
+ "marimo._code_mode.screenshot._require_playwright",
+ return_value=lambda: manager,
+ ):
+ first = asyncio.create_task(session._ensure_ready())
+ await navigating.wait()
+ second = asyncio.create_task(session._ensure_ready())
+ await asyncio.sleep(0)
+ assert not second.done()
+ finish_navigation.set()
+ await asyncio.gather(first, second)
+ playwright_mock.chromium.launch.assert_awaited_once()
+ await session.close()
+
+
+async def test_close_stops_playwright_even_if_browser_close_fails(
+ playwright_mock: MagicMock,
+) -> None:
+ browser = playwright_mock.chromium.launch.return_value
+ browser.close.side_effect = RuntimeError("close failed")
+ manager = MagicMock(start=AsyncMock(return_value=playwright_mock))
+ session = _ScreenshotSession("http://localhost:1234")
+ with patch(
+ "marimo._code_mode.screenshot._require_playwright",
+ return_value=lambda: manager,
+ ):
+ await session._ensure_ready()
+ with pytest.raises(RuntimeError, match="close failed"):
+ await session.close()
+ playwright_mock.stop.assert_awaited_once()
+ assert session._page is None
+ assert session._browser is None
+ assert session._playwright is None
+ await session.close()
+ playwright_mock.stop.assert_awaited_once()
diff --git a/tests/_code_mode/test_screenshot_integration.py b/tests/_code_mode/test_screenshot_integration.py
new file mode 100644
index 0000000000..64b746d562
--- /dev/null
+++ b/tests/_code_mode/test_screenshot_integration.py
@@ -0,0 +1,249 @@
+# Copyright 2026 Marimo. All rights reserved.
+"""Run with MARIMO_TEST_SCREENSHOTS=1 after building the frontend.
+
+The Playwright CI job supplies built assets and matching Chromium binaries.
+"""
+
+from __future__ import annotations
+
+import asyncio
+import json
+import os
+import signal
+import socket
+import sys
+import time
+from typing import TYPE_CHECKING
+from urllib.parse import parse_qs, urlencode, urlsplit
+
+import httpx
+import pytest
+
+if TYPE_CHECKING:
+ from pathlib import Path
+
+ from playwright.async_api import Page, WebSocket
+
+
+async def _execute(
+ client: httpx.AsyncClient, session_id: str, code: str
+) -> str:
+ response = await client.post(
+ "/api/kernel/execute",
+ headers={"Marimo-Session-Id": session_id},
+ json={"code": code},
+ timeout=90,
+ )
+ response.raise_for_status()
+ event = ""
+ stdout = ""
+ stderr = ""
+ completed = False
+ for line in response.text.splitlines():
+ if line.startswith("event: "):
+ event = line.removeprefix("event: ")
+ elif line.startswith("data: "):
+ data = json.loads(line.removeprefix("data: "))
+ if event == "stdout":
+ stdout += data["data"]
+ elif event == "stderr":
+ stderr += data["data"]
+ elif event == "done":
+ assert data["success"], stderr
+ completed = True
+ assert completed, response.text
+ return stdout.strip()
+
+
+ os.environ.get("MARIMO_TEST_SCREENSHOTS") != "1",
+ reason="Requires built frontend assets and Playwright Chromium",
+)
+async def test_screenshots_attach_to_live_notebook(tmp_path: Path) -> None:
+ from playwright.async_api import async_playwright
+
+ token = "screenshot-test+&=#?"
+ token_file = tmp_path / "token.txt"
+ token_file.write_text(token)
+ token_file.chmod(0o600)
+ files = [tmp_path / "Notebook A + & # ?.py", tmp_path / "Notebook B.py"]
+ for file in files:
+ file.write_text(
+ "import marimo\napp = marimo.App()\n"
+ "@app.cell\ndef _():\n value = 1\n return (value,)\n"
+ "if __name__ == '__main__':\n app.run()\n"
+ )
+ with socket.socket() as sock:
+ sock.bind(("127.0.0.1", 0))
+ port = sock.getsockname()[1]
+ base = f"http://127.0.0.1:{port}/capture-check"
+ log_path = tmp_path / "server.log"
+ with log_path.open("w") as log:
+ server = await asyncio.create_subprocess_exec(
+ sys.executable,
+ "-m",
+ "marimo",
+ "edit",
+ str(tmp_path),
+ "--headless",
+ "--no-sandbox",
+ "--skip-update-check",
+ "--port",
+ str(port),
+ "--base-url",
+ "/capture-check",
+ "--token-password-file",
+ str(token_file),
+ stdout=log,
+ stderr=asyncio.subprocess.STDOUT,
+ start_new_session=True,
+ )
+ try:
+ async with httpx.AsyncClient(
+ base_url=base + "/",
+ headers={"Authorization": f"Bearer {token}"},
+ ) as client:
+ deadline = time.monotonic() + 30
+ while True:
+ try:
+ response = await client.get("/api/sessions")
+ if response.status_code == 200:
+ break
+ except httpx.TransportError:
+ pass
+ assert server.returncode is None, log_path.read_text()
+ assert time.monotonic() < deadline, log_path.read_text()
+ await asyncio.sleep(0.1)
+ async with httpx.AsyncClient() as anonymous:
+ assert (
+ await anonymous.get(base + "/api/sessions")
+ ).status_code != 200
+
+ async with async_playwright() as playwright:
+ browser = await playwright.chromium.launch()
+ keys = [str(file) for file in files] + [
+ "__new__capture-test"
+ ]
+ editors: list[tuple[str, Page, WebSocket, str]] = []
+ for key in keys:
+ context = await browser.new_context()
+ page = await context.new_page()
+ sockets: list[WebSocket] = []
+ page.on(
+ "websocket",
+ lambda ws, records=sockets: records.append(ws),
+ )
+ await page.goto(
+ base
+ + "/?"
+ + urlencode({"file": key, "access_token": token})
+ )
+ deadline = time.monotonic() + 30
+ while (
+ not sockets
+ or parse_qs(urlsplit(sockets[0].url).query)[
+ "session_id"
+ ][0]
+ not in (await client.get("/api/sessions")).json()
+ ):
+ assert time.monotonic() < deadline
+ await asyncio.sleep(0.1)
+ session_id = parse_qs(urlsplit(sockets[0].url).query)[
+ "session_id"
+ ][0]
+ editors.append((key, page, sockets[0], session_id))
+ sessions_before = (
+ await client.get("/api/sessions")
+ ).json()
+ assert len(sessions_before) == 3
+ for index, (key, page, websocket, session_id) in enumerate(
+ editors
+ ):
+ marker = f"LIVE NOTEBOOK {index}"
+ source = f"import marimo as mo\nmo.Html('<div>{marker}</div>')"
+ if index == 2:
+ operation = (
+ f"cell_id = ctx.create_cell({source!r})"
+ )
+ else:
+ operation = (
+ "cell_id = ctx.cells[-1].id\n"
+ " previous_code = ctx.cells[cell_id].code\n"
+ f" ctx.edit_cell(cell_id, code={source!r})"
+ )
+ # Keep the same context: its empty snapshot must not
+ # prevent capturing output rendered after run_cell.
+ code = f"""
+import marimo._code_mode as cm
+from urllib.parse import parse_qs, urlsplit
+ctx = cm.get_context()
+assert all(cell.output is None or cell.output.data == "" for cell in ctx.cells)
+async with ctx:
+ {operation}
+ ctx.run_cell(cell_id)
+image = await ctx.screenshot(cell_id)
+session = ctx._screenshot_session
+assert image.startswith(b"\\x89PNG\\r\\n\\x1a\\n")
+assert {marker!r} in await session._page.locator("#output-" + cell_id).inner_text()
+assert parse_qs(urlsplit(session._page.url).query)["file"] == [{key!r}]
+assert parse_qs(urlsplit(session._page.url).query)["kiosk"] == ["true"]
+browser = session._browser
+page = session._page
+await ctx.close_screenshot_session()
+assert not browser.is_connected()
+assert page.is_closed()
+
+# A bound, non-listening port makes real Chromium navigation fail.
+import socket
+from playwright.async_api import Error
+with socket.socket() as unavailable:
+ unavailable.bind(("127.0.0.1", 0))
+ session._server_url = "http://127.0.0.1:" + str(unavailable.getsockname()[1])
+ try:
+ await session.capture(cell_id)
+ except Error as error:
+ assert "net::ERR_CONNECTION_" in str(error)
+ else:
+ raise AssertionError("Navigation should fail")
+assert session._page is None
+assert session._browser is None
+assert session._playwright is None
+session._server_url = {base!r}
+retry_image = await session.capture(cell_id)
+assert retry_image.startswith(b"\\x89PNG\\r\\n\\x1a\\n")
+assert {marker!r} in await session._page.locator("#output-" + cell_id).inner_text()
+await session.close()
+print("CELL_ID=" + cell_id)
+"""
+ stdout = await _execute(client, session_id, code)
+ cell_id = next(
+ line.removeprefix("CELL_ID=")
+ for line in stdout.splitlines()
+ if line.startswith("CELL_ID=")
+ )
+ await page.get_by_text(marker, exact=True).wait_for()
+ assert not websocket.is_closed()
+ assert (
+ await _execute(
+ client, session_id, "print('still connected')"
+ )
+ == "still connected"
+ )
+ assert (
+ await page.locator("#output-" + cell_id).count()
+ == 1
+ )
+ assert set(sessions_before) == set(
+ (await client.get("/api/sessions")).json()
+ )
+ await browser.close()
+ finally:
+ if server.returncode is None:
+ os.killpg(server.pid, signal.SIGINT)
+ try:
+ await asyncio.wait_for(server.wait(), timeout=15)
+ except TimeoutError:
+ os.killpg(server.pid, signal.SIGKILL)
+ await asyncio.wait_for(server.wait(), timeout=5)
diff --git a/tests/_server/api/endpoints/test_execution.py b/tests/_server/api/endpoints/test_execution.py
index 6f85f6e5f3..d64f0def4f 100644
--- a/tests/_server/api/endpoints/test_execution.py
+++ b/tests/_server/api/endpoints/test_execution.py
@@ -12,6 +12,7 @@ import pytest
from marimo._code_mode.screenshot_meta import (
SCREENSHOT_AUTH_TOKEN_KEY,
+ SCREENSHOT_FILE_KEY,
SCREENSHOT_SERVER_URL_KEY,
)
from marimo._messaging.notification import ConsumerCapabilities
@@ -262,12 +263,10 @@ class TestExecutionRoutes_EditMode:
@staticmethod
@with_session(SESSION_ID)
def test_execute_injects_screenshot_meta(client: TestClient) -> None:
- """`/api/kernel/execute` injects a trusted server URL + auth token
- into `HTTPRequest.meta` so `ctx.screenshot()` can authenticate
- Playwright against this server. Regression guard: deleting either
- injection line in the endpoint should fail this test.
+ """Inject the trusted server URL, auth token, and notebook key
+ so screenshots authenticate and attach to the active notebook.
"""
- from unittest.mock import patch
+ from unittest.mock import PropertyMock, patch
from marimo._runtime.commands import ExecuteScratchpadCommand
from marimo._server import scratchpad as scratchpad_mod
@@ -284,35 +283,48 @@ class TestExecutionRoutes_EditMode:
if False:
yield "" # makes this an async generator that yields nothing
- with (
- patch.object(session, "put_control_request", side_effect=capture),
- patch.object(
- scratchpad_mod.ScratchCellListener,
- "stream",
- empty_stream,
- ),
- ):
- response = client.post(
- "/api/kernel/execute",
- headers=HEADERS,
- json={"code": "x = 1"},
+ for path in [None, "notebooks/my notebook.py"]:
+ captured.clear()
+ with (
+ patch.object(
+ type(session.app_file_manager),
+ "path",
+ new_callable=PropertyMock,
+ return_value=path,
+ ),
+ patch.object(
+ session, "put_control_request", side_effect=capture
+ ),
+ patch.object(
+ scratchpad_mod.ScratchCellListener,
+ "stream",
+ empty_stream,
+ ),
+ ):
+ response = client.post(
+ "/api/kernel/execute",
+ headers=HEADERS,
+ json={"code": "x = 1"},
+ )
+
+ assert response.status_code == 200, response.text
+
+ scratchpad_cmds = [
+ c for c in captured if isinstance(c, ExecuteScratchpadCommand)
+ ]
+ assert len(scratchpad_cmds) == 1, (
+ f"expected one ExecuteScratchpadCommand, got {captured!r}"
+ )
+ http_req = scratchpad_cmds[0].request
+ assert http_req is not None
+ # Mock server uses host="localhost", port=1234, base_url=""
+ assert http_req.meta[SCREENSHOT_SERVER_URL_KEY] == (
+ "http://localhost:1234"
+ )
+ assert http_req.meta[SCREENSHOT_AUTH_TOKEN_KEY] == "fake-token"
+ assert http_req.meta[SCREENSHOT_FILE_KEY] == (
+ path or session.initialization_id
)
-
- assert response.status_code == 200, response.text
-
- scratchpad_cmds = [
- c for c in captured if isinstance(c, ExecuteScratchpadCommand)
- ]
- assert len(scratchpad_cmds) == 1, (
- f"expected one ExecuteScratchpadCommand, got {captured!r}"
- )
- http_req = scratchpad_cmds[0].request
- assert http_req is not None
- # Mock server uses host="localhost", port=1234, base_url=""
- assert http_req.meta[SCREENSHOT_SERVER_URL_KEY] == (
- "http://localhost:1234"
- )
- assert http_req.meta[SCREENSHOT_AUTH_TOKEN_KEY] == "fake-token"
@staticmethod
@with_session(SESSION_ID)
diff --git a/tests/_server/test_scratchpad.py b/tests/_server/test_scratchpad.py
index 1f6212943f..36bf681b0a 100644
--- a/tests/_server/test_scratchpad.py
+++ b/tests/_server/test_scratchpad.py
@@ -14,6 +14,7 @@ from inline_snapshot import snapshot
from marimo._ai._tools.types import CodeExecutionResult
from marimo._code_mode.screenshot_meta import (
SCREENSHOT_AUTH_TOKEN_KEY,
+ SCREENSHOT_FILE_KEY,
SCREENSHOT_SERVER_URL_KEY,
)
from marimo._messaging.cell_output import CellChannel, CellOutput
@@ -165,6 +166,8 @@ class _FakeSession:
_pre_complete_notifs: list[NotificationMessage]
def __init__(self, *, auto_complete: bool = True) -> None:
+ self.app_file_manager = SimpleNamespace(path=None)
+ self.initialization_id = "__new__notebook"
self.cell_outputs = {}
self.console_outputs = {}
self.document = SimpleNamespace(cells=(), cell_ids=())
@@ -829,14 +832,16 @@ class TestRunScratchpadCode:
return cmds[0]
@pytest.mark.asyncio
+ @pytest.mark.parametrize("path", [None, "notebooks/my notebook.py"])
async def test_stamps_screenshot_meta_and_run_id_on_command(
- self,
+ self, path: str | None
) -> None:
"""Regression guard: `run_id` and screenshot meta must reach
the `ExecuteScratchpadCommand` unchanged. Without `run_id`,
`ScratchCellListener` filters out the completion event and
every code-mode tool call hangs ~30s before timing out."""
session = _FakeSession()
+ session.app_file_manager.path = path
result = await run_scratchpad_code(
session.as_session(),
@@ -854,6 +859,9 @@ class TestRunScratchpadCode:
"http://localhost:1234"
)
assert cmd.request.meta[SCREENSHOT_AUTH_TOKEN_KEY] == "fake-token"
+ assert cmd.request.meta[SCREENSHOT_FILE_KEY] == (
+ path or session.initialization_id
+ )
@pytest.mark.asyncio
async def test_snapshots_cell_outputs_onto_command(self) -> None: