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
Gate host integration behind `MARIMO_ENABLE_HOST` (#11302)
Trevor Manzc6226418c358
diff --git a/marimo/_config/settings.py b/marimo/_config/settings.pyindex 4e6c1a2374..2711ff194d 100644--- a/marimo/_config/settings.py+++ b/marimo/_config/settings.py@@ -15,6 +15,8 @@ class GlobalSettings:YES: bool = FalseCHECK_STATUS_UPDATE: bool = FalseTRACING: bool = is_env_true("MARIMO_TRACING")+ # Temporary opt-in while the host protocol is experimental.+ ENABLE_HOST: bool = is_env_true("MARIMO_ENABLE_HOST")PROFILE_DIR: str | None = NoneLOG_LEVEL: int = logging.WARNINGMANAGE_SCRIPT_METADATA: bool = is_env_true("MARIMO_MANAGE_SCRIPT_METADATA")diff --git a/marimo/_server/host/lifespan.py b/marimo/_server/host/lifespan.pyindex 93ad3cf930..8af1b900ef 100644--- a/marimo/_server/host/lifespan.py+++ b/marimo/_server/host/lifespan.py@@ -9,6 +9,7 @@ import secretsfrom typing import TYPE_CHECKINGfrom marimo import _loggers+from marimo._config.settings import GLOBAL_SETTINGSfrom marimo._server.api.deps import AppStateBasefrom marimo._server.host.bridge import SessionBridgefrom marimo._server.host.context import HostContext@@ -27,7 +28,7 @@ LOGGER = _loggers.marimo_logger()async def host(app: Starlette) -> AsyncIterator[None]:"""Initialize the host API and discovery record in edit mode."""state = AppStateBase.from_app(app)- if state.mode is not SessionMode.EDIT:+ if not GLOBAL_SETTINGS.ENABLE_HOST or state.mode is not SessionMode.EDIT:yieldreturndiff --git a/marimo/_server/main.py b/marimo/_server/main.pyindex ac98876be0..c3e8b99d1c 100644--- a/marimo/_server/main.py+++ b/marimo/_server/main.py@@ -60,10 +60,12 @@ def create_starlette_app(skew_protection: bool = True,timeout: float | None = None,) -> Starlette:- # Handle host API requests first to bypass the editor's middleware.- final_middlewares: list[Middleware] = [- Middleware(HostApiMiddleware, base_url=base_url)- ]+ final_middlewares: list[Middleware] = []+ if GLOBAL_SETTINGS.ENABLE_HOST:+ # Handle host API requests first to bypass the editor's middleware.+ final_middlewares.append(+ Middleware(HostApiMiddleware, base_url=base_url)+ )if allow_origins is None:allow_origins = ("localhost", "127.0.0.1") + (diff --git a/protocols/host/conformance/README.md b/protocols/host/conformance/README.mdindex b6798253e8..0bdb90efdd 100644--- a/protocols/host/conformance/README.md+++ b/protocols/host/conformance/README.md@@ -3,7 +3,8 @@Python tests for hosts implementing the [host protocol](../spec.md). The suiteuses HTTP and SSE and does not import marimo.-Start a host in a disposable workspace, then run this from the repository root:+Start a host in a disposable workspace (for marimo, use+`MARIMO_ENABLE_HOST=1 marimo edit`), then run this from the repository root:```shuv run --locked --project protocols/host/conformance \diff --git a/tests/_server/host/conftest.py b/tests/_server/host/conftest.pyindex 5efe780541..946498d4d3 100644--- a/tests/_server/host/conftest.py+++ b/tests/_server/host/conftest.py@@ -9,6 +9,7 @@ from unittest.mock import MagicMockimport pytest+from marimo._config.settings import GLOBAL_SETTINGSfrom marimo._core import backend as befrom marimo._messaging.notebook import NotebookDocumentfrom marimo._messaging.notification import NotificationMessage@@ -20,6 +21,11 @@ from marimo._session.types import KernelStatefrom marimo._types.ids import ConsumerId+def enable_host(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(GLOBAL_SETTINGS, "ENABLE_HOST", True)++class FakeSession:"""A session fake with a real scratchpad lock and event bus."""diff --git a/tests/_server/host/test_host_api.py b/tests/_server/host/test_host_api.pyindex 0a069e1f06..dd438a7399 100644--- a/tests/_server/host/test_host_api.py+++ b/tests/_server/host/test_host_api.py@@ -48,6 +48,7 @@ if TYPE_CHECKING:TOKEN = "record-token"AUTH = {"Authorization": f"Bearer {TOKEN}"}+pytestmark = pytest.mark.usefixtures("enable_host")async def analyzed(notebook: Notebook) -> protocol.Analysis:diff --git a/tests/_server/host/test_host_execution.py b/tests/_server/host/test_host_execution.pyindex 1160412cd3..67186c913a 100644--- a/tests/_server/host/test_host_execution.py+++ b/tests/_server/host/test_host_execution.py@@ -32,6 +32,7 @@ if TYPE_CHECKING:from tests._server.host.drive import ResponseAUTH = {"Authorization": "Bearer execution-test"}+pytestmark = pytest.mark.usefixtures("enable_host")def key(value: str) -> dict[str, str]:diff --git a/tests/_server/host/test_host_record.py b/tests/_server/host/test_host_record.pyindex 42e039b85b..bdc892a768 100644--- a/tests/_server/host/test_host_record.py+++ b/tests/_server/host/test_host_record.py@@ -13,11 +13,13 @@ import msgspecimport pytestimport uvicorn+from marimo._config.settings import GLOBAL_SETTINGSfrom marimo._core import backend as be, protocolfrom marimo._server.host import HOST_API_PATH, record as record_modulefrom marimo._server.host.lifespan import host as host_lifespanfrom marimo._server.host.record import host_urlfrom marimo._server.main import create_starlette_app+from marimo._session.model import SessionModefrom marimo._utils.lifespans import Lifespansfrom tests._core.conftest import (ScriptedBackend,@@ -27,11 +29,16 @@ from tests._core.conftest import (runtime_id_of,)from tests._server.host.drive import finished-from tests._server.mocks import get_starlette_server_state_init+from tests._server.mocks import (+ get_mock_session_manager,+ get_starlette_server_state_init,+)if TYPE_CHECKING:from pathlib import Path+pytestmark = pytest.mark.usefixtures("enable_host")+@pytest.mark.parametrize(("host", "loopback"),@@ -79,6 +86,32 @@ async def test_an_unreachable_bind_writes_no_record(assert app.state.host_context is not None+ ("enabled", "mode"), [(False, SessionMode.EDIT), (True, SessionMode.RUN)]+)+async def test_inactive_host_writes_no_record_and_serves_no_api(+ tmp_path: Path,+ monkeypatch: pytest.MonkeyPatch,+ enabled: bool,+ mode: SessionMode,+) -> None:+ monkeypatch.setattr(GLOBAL_SETTINGS, "ENABLE_HOST", enabled)+ monkeypatch.setattr(record_module, "marimo_state_dir", lambda: tmp_path)+ app = create_starlette_app(+ base_url="", enable_auth=False, lifespan=Lifespans([host_lifespan])+ )+ get_starlette_server_state_init(+ session_manager=get_mock_session_manager(mode=mode)+ ).apply(app.state)+ app.state.server = uvicorn.Server(+ uvicorn.Config(app, host="127.0.0.1", port=1234)+ )++ async with app.router.lifespan_context(app):+ assert not (tmp_path / "hosts-v1").exists()+ assert (await finished(app, "GET", "/notebooks")).status == 404++@pytest.mark.parametrize("external_port", [1234, 8443])async def test_the_lifespan_serves_the_host_while_the_server_runs(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, external_port: int