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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: marimo development preview (#5403)
Akshay Agrawalff59d6766d71
diff --git a/frontend/src/core/network/connection.ts b/frontend/src/core/network/connection.tsindex ea5255ff19..67f58b5b24 100644--- a/frontend/src/core/network/connection.ts+++ b/frontend/src/core/network/connection.ts@@ -5,7 +5,7 @@ import { type ConnectionStatus, WebSocketState } from "../websocket/types";/*** Atom for storing the connection status.- * Initialized to CONNECTING.+ * Initialized to CONNECTING for normal mode, OPEN for static mode.*/export const connectionAtom = atom<ConnectionStatus>({state: WebSocketState.CONNECTING,diff --git a/frontend/src/core/websocket/StaticWebsocket.ts b/frontend/src/core/websocket/StaticWebsocket.tsindex d4120366e6..981a23e297 100644--- a/frontend/src/core/websocket/StaticWebsocket.ts+++ b/frontend/src/core/websocket/StaticWebsocket.ts@@ -17,9 +17,20 @@ export class StaticWebsocket implements IReconnectingWebSocket {onmessage = null;onopen = null;- addEventListener(type: unknown, callback: unknown, options?: unknown): void {- // Noop+ addEventListener(+ type: string,+ callback: EventListener,+ _options?: unknown,+ ): void {+ // Normally this would be a no-op in a mock, but we simulate a synthetic "open" event+ // to mimic the WebSocket transitioning from CONNECTING to OPEN state.+ if (type === "open") {+ queueMicrotask(() => {+ callback(new Event("open"));+ });+ }}+removeEventListener(type: unknown,callback: unknown,diff --git a/marimo/_cli/development/commands.py b/marimo/_cli/development/commands.pyindex 4d44e35548..1290f829a1 100644--- a/marimo/_cli/development/commands.py+++ b/marimo/_cli/development/commands.py@@ -11,7 +11,10 @@ from typing import TYPE_CHECKING, Anyimport clickfrom marimo._cli.print import orange-from marimo._convert.converters import MarimoConvert+from marimo._server.session.serialize import (+ serialize_notebook,+ serialize_session_view,+)from marimo._utils.code import hash_codeif TYPE_CHECKING:@@ -533,13 +536,8 @@ def preview(file_path: Path, port: int, host: str, headless: bool) -> None:from starlette.routing import Routefrom starlette.staticfiles import StaticFiles- from marimo import __version__from marimo._ast.app_config import _AppConfigfrom marimo._config.config import DEFAULT_CONFIG- from marimo._schemas.session import (- NotebookSessionMetadata,- NotebookSessionV1,- )from marimo._server.templates.templates import static_notebook_templatefrom marimo._server.tokens import SkewProtectionTokenfrom marimo._utils.paths import marimo_package_path@@ -548,16 +546,41 @@ def preview(file_path: Path, port: int, host: str, headless: bool) -> None:from starlette.requests import Requesttry:- # Convert to notebook format- notebook_snapshot = MarimoConvert.from_py(- file_path.read_text(encoding="utf-8")- ).to_notebook_v1()-- # Create empty session snapshot since we're not running the code- session_snapshot = NotebookSessionV1(- version="1",- metadata=NotebookSessionMetadata(marimo_version=__version__),- cells=[],+ # Run the notebook to get actual outputs+ click.echo(f"Running notebook {file_path.name}...")+ from marimo._server.export import run_app_until_completion+ from marimo._server.file_router import AppFileRouter+ from marimo._server.utils import asyncio_run+ from marimo._utils.marimo_path import MarimoPath++ # Create file manager for the notebook+ file_router = AppFileRouter.from_filename(MarimoPath(file_path))+ file_key = file_router.get_unique_file_key()+ assert file_key is not None+ file_manager = file_router.get_file_manager(file_key)++ # Run the notebook to completion and get session view+ session_view, did_error = asyncio_run(+ run_app_until_completion(+ file_manager,+ cli_args={},+ argv=None,+ )+ )+ if did_error:+ click.echo(+ "Warning: Some cells had errors during execution", err=True+ )++ # Create session snapshot from the executed session+ session_snapshot = serialize_session_view(+ session_view,+ cell_ids=list(file_manager.app.cell_manager.cell_ids()),+ )++ # Get notebook snapshot from file manager+ notebook_snapshot = serialize_notebook(+ session_view, file_manager.app.cell_manager)# Get the static assets directory@@ -609,6 +632,13 @@ def preview(file_path: Path, port: int, host: str, headless: bool) -> None:name="assets",)+ # Mount other static files (favicon, icons, manifest, etc.)+ app.mount(+ "/",+ StaticFiles(directory=static_root, html=False),+ name="static",+ )+url = f"http://{host}:{port}"click.echo(f"Serving preview at {url}")click.echo("Press Ctrl+C to stop the server")