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: don't check server token in run mode (#443)
Akshay Agrawal7dcfd69c79ed
diff --git a/marimo/_server/api/code_complete.py b/marimo/_server/api/code_complete.pyindex 2222fbf4f5..16063d819e 100644--- a/marimo/_server/api/code_complete.py+++ b/marimo/_server/api/code_complete.py@@ -6,7 +6,7 @@ from dataclasses import dataclassfrom marimo._ast.cell import CellId_tfrom marimo._runtime import requestsfrom marimo._server import sessions-from marimo._server.api.validated_handler import ValidatedHandler+from marimo._server.api.edit_handler import EditHandlerfrom marimo._utils.parse_dataclass import parse_raw@@ -17,9 +17,10 @@ class CodeComplete:cell_id: CellId_t-class CodeCompleteHandler(ValidatedHandler):- """Complete a path to subdirectories and Python files."""+class CodeCompleteHandler(EditHandler):+ """Complete a code fragment."""+ @sessions.requires_editdef post(self) -> None:args = parse_raw(self.request.body, CodeComplete)session = sessions.require_session_from_header(self.request.headers)diff --git a/marimo/_server/api/delete.py b/marimo/_server/api/delete.pyindex 0a7281f95c..5ac76f88a8 100644--- a/marimo/_server/api/delete.py+++ b/marimo/_server/api/delete.py@@ -6,7 +6,7 @@ from dataclasses import dataclassfrom marimo._ast.cell import CellId_tfrom marimo._runtime import requestsfrom marimo._server import sessions-from marimo._server.api.validated_handler import ValidatedHandler+from marimo._server.api.edit_handler import EditHandlerfrom marimo._utils.parse_dataclass import parse_raw@@ -15,7 +15,7 @@ class Delete:cell_id: CellId_t-class DeleteHandler(ValidatedHandler):+class DeleteHandler(EditHandler):"""Delete a cell with a given id"""@sessions.requires_editdiff --git a/marimo/_server/api/directory_autocomplete.py b/marimo/_server/api/directory_autocomplete.pyindex 7c375b6602..8d3f6905bf 100644--- a/marimo/_server/api/directory_autocomplete.py+++ b/marimo/_server/api/directory_autocomplete.py@@ -4,7 +4,8 @@ from __future__ import annotationsimport osfrom dataclasses import dataclass-from marimo._server.api.validated_handler import ValidatedHandler+from marimo._server import sessions+from marimo._server.api.edit_handler import EditHandlerfrom marimo._utils.parse_dataclass import parse_raw@@ -13,9 +14,10 @@ class DirectoryAutocomplete:prefix: str-class DirectoryAutocompleteHandler(ValidatedHandler):+class DirectoryAutocompleteHandler(EditHandler):"""Complete a path to subdirectories and Python files."""+ @sessions.requires_editdef post(self) -> None:args = parse_raw(self.request.body, DirectoryAutocomplete)directory = os.path.dirname(args.prefix)diff --git a/marimo/_server/api/validated_handler.py b/marimo/_server/api/edit_handler.pysimilarity index 74%rename from marimo/_server/api/validated_handler.pyrename to marimo/_server/api/edit_handler.pyindex 90f80f44c0..d53458d076 100644--- a/marimo/_server/api/validated_handler.py+++ b/marimo/_server/api/edit_handler.py@@ -6,7 +6,9 @@ import tornado.webfrom marimo._server import sessions-class ValidatedHandler(tornado.web.RequestHandler):+class EditHandler(tornado.web.RequestHandler):+ """Checks that the frontend has a token matching the edit session."""+def prepare(self) -> None:# Validate the server token -- don't allow connections from frontends# that weren't generated by this serverdiff --git a/marimo/_server/api/format.py b/marimo/_server/api/format.pyindex 95d7e88562..bef0c7dcfa 100644--- a/marimo/_server/api/format.py+++ b/marimo/_server/api/format.py@@ -7,7 +7,7 @@ from typing import Dictfrom marimo import _loggersfrom marimo._ast import cellfrom marimo._server import sessions-from marimo._server.api.validated_handler import ValidatedHandler+from marimo._server.api.edit_handler import EditHandlerfrom marimo._utils.parse_dataclass import parse_rawLOGGER = _loggers.marimo_logger()@@ -19,7 +19,7 @@ class Format:codes: Dict[cell.CellId_t, str]-class FormatHandler(ValidatedHandler):+class FormatHandler(EditHandler):"""Save an app to disk."""@sessions.requires_editdiff --git a/marimo/_server/api/function_call.py b/marimo/_server/api/function_call.pyindex 1f9e1db074..e2eb6659c7 100644--- a/marimo/_server/api/function_call.py+++ b/marimo/_server/api/function_call.py@@ -4,10 +4,11 @@ from __future__ import annotationsfrom dataclasses import dataclassfrom typing import Any, Dict+import tornado.web+from marimo import _loggersfrom marimo._runtime import requestsfrom marimo._server import sessions-from marimo._server.api.validated_handler import ValidatedHandlerfrom marimo._utils.parse_dataclass import parse_rawLOGGER = _loggers.marimo_logger()@@ -22,7 +23,7 @@ class FunctionCall:args: Dict[str, Any]-class FunctionHandler(ValidatedHandler):+class FunctionHandler(tornado.web.RequestHandler):"""Invoke an RPC"""def post(self) -> None:diff --git a/marimo/_server/api/instantiate.py b/marimo/_server/api/instantiate.pyindex 3776e21227..1d1102e876 100644--- a/marimo/_server/api/instantiate.py+++ b/marimo/_server/api/instantiate.py@@ -4,10 +4,11 @@ from __future__ import annotationsfrom dataclasses import dataclassfrom typing import Any, List+import tornado.web+from marimo._ast.app import Appfrom marimo._runtime import requestsfrom marimo._server import sessions-from marimo._server.api.validated_handler import ValidatedHandlerfrom marimo._utils.parse_dataclass import parse_raw@@ -19,7 +20,7 @@ class Instantiate:values: List[Any]-class InstantiateHandler(ValidatedHandler):+class InstantiateHandler(tornado.web.RequestHandler):"""Instantiate an appRun all cells, parametrized by values for UI elements, if any.diff --git a/marimo/_server/api/interrupt.py b/marimo/_server/api/interrupt.pyindex 62aecc26fa..5bc584626a 100644--- a/marimo/_server/api/interrupt.py+++ b/marimo/_server/api/interrupt.py@@ -2,12 +2,13 @@from __future__ import annotationsfrom marimo._server import sessions-from marimo._server.api.validated_handler import ValidatedHandler+from marimo._server.api.edit_handler import EditHandler-class InterruptHandler(ValidatedHandler):+class InterruptHandler(EditHandler):"""Interrupt the kernel's execution."""+ @sessions.requires_editdef post(self) -> None:sessions.require_session_from_header(self.request.headersdiff --git a/marimo/_server/api/read_code.py b/marimo/_server/api/read_code.pyindex 928ec9aafd..392a11a645 100644--- a/marimo/_server/api/read_code.py+++ b/marimo/_server/api/read_code.py@@ -4,11 +4,11 @@ from __future__ import annotationsimport tornado.webfrom marimo._server import sessions+from marimo._server.api.edit_handler import EditHandlerfrom marimo._server.api.status import HTTPStatus-from marimo._server.api.validated_handler import ValidatedHandler-class ReadCodeHandler(ValidatedHandler):+class ReadCodeHandler(EditHandler):"""Handler for reading code from the server."""@sessions.requires_editdiff --git a/marimo/_server/api/rename.py b/marimo/_server/api/rename.pyindex f3d9716c8b..fb1f26e9c6 100644--- a/marimo/_server/api/rename.py+++ b/marimo/_server/api/rename.py@@ -9,7 +9,7 @@ import tornado.webfrom marimo import _loggersfrom marimo._server import sessionsfrom marimo._server.api import status-from marimo._server.api.validated_handler import ValidatedHandler+from marimo._server.api.edit_handler import EditHandlerfrom marimo._server.utils import canonicalize_filenamefrom marimo._utils.parse_dataclass import parse_raw@@ -21,7 +21,7 @@ class Rename:filename: str-class RenameHandler(ValidatedHandler):+class RenameHandler(EditHandler):"""Rename the current app."""@sessions.requires_editdiff --git a/marimo/_server/api/run.py b/marimo/_server/api/run.pyindex 29b87d4c89..0e07db2806 100644--- a/marimo/_server/api/run.py+++ b/marimo/_server/api/run.py@@ -7,7 +7,7 @@ from typing import Listfrom marimo._ast.cell import CellId_tfrom marimo._runtime import requestsfrom marimo._server import sessions-from marimo._server.api.validated_handler import ValidatedHandler+from marimo._server.api.edit_handler import EditHandlerfrom marimo._utils.parse_dataclass import parse_raw@@ -19,9 +19,12 @@ class Run:codes: List[str]-class RunHandler(ValidatedHandler):+class RunHandler(EditHandler):"""Run multiple cells (and their descendants).+ Updates cell code in the kernel if needed; registers new cells+ for unseen cell IDs.+Only allowed in edit mode."""diff --git a/marimo/_server/api/save.py b/marimo/_server/api/save.pyindex 9701029312..aacde6b1e7 100644--- a/marimo/_server/api/save.py+++ b/marimo/_server/api/save.py@@ -11,8 +11,8 @@ from marimo import _loggersfrom marimo._ast import codegenfrom marimo._ast.cell import CellConfigfrom marimo._server import sessions+from marimo._server.api.edit_handler import EditHandlerfrom marimo._server.api.status import HTTPStatus-from marimo._server.api.validated_handler import ValidatedHandlerfrom marimo._server.layout import LayoutConfig, save_layout_configfrom marimo._server.utils import canonicalize_filenamefrom marimo._utils.parse_dataclass import parse_raw@@ -34,7 +34,7 @@ class Save:layout: Optional[Dict[str, Any]] = None-class SaveHandler(ValidatedHandler):+class SaveHandler(EditHandler):"""Save an app to disk."""@sessions.requires_editdiff --git a/marimo/_server/api/save_app_config.py b/marimo/_server/api/save_app_config.pyindex 70bc262e52..6d6c061301 100644--- a/marimo/_server/api/save_app_config.py+++ b/marimo/_server/api/save_app_config.py@@ -9,8 +9,8 @@ import tornado.webfrom marimo import _loggersfrom marimo._ast import codegenfrom marimo._server import sessions+from marimo._server.api.edit_handler import EditHandlerfrom marimo._server.api.status import HTTPStatus-from marimo._server.api.validated_handler import ValidatedHandlerfrom marimo._utils.parse_dataclass import parse_rawLOGGER = _loggers.marimo_logger()@@ -22,7 +22,7 @@ class SaveAppConfiguration:config: Dict[str, Any]-class SaveAppConfigurationHandler(ValidatedHandler):+class SaveAppConfigurationHandler(EditHandler):"""Save app configuration to session manager."""@sessions.requires_editdiff --git a/marimo/_server/api/save_user_config.py b/marimo/_server/api/save_user_config.pyindex 7128626db4..d497b0a155 100644--- a/marimo/_server/api/save_user_config.py+++ b/marimo/_server/api/save_user_config.py@@ -13,8 +13,8 @@ from marimo._config.config import MarimoConfig, configurefrom marimo._config.utils import get_config_pathfrom marimo._runtime import requestsfrom marimo._server import sessions+from marimo._server.api.edit_handler import EditHandlerfrom marimo._server.api.status import HTTPStatus-from marimo._server.api.validated_handler import ValidatedHandlerfrom marimo._utils.parse_dataclass import parse_rawLOGGER = _loggers.marimo_logger()@@ -45,7 +45,7 @@ class SaveUserConfiguration:config: MarimoConfig-class SaveUserConfigurationHandler(ValidatedHandler):+class SaveUserConfigurationHandler(EditHandler):"""Save user configuration to disk."""@sessions.requires_editdiff --git a/marimo/_server/api/set_cell_config.py b/marimo/_server/api/set_cell_config.pyindex aadd26b870..be3ed3fd69 100644--- a/marimo/_server/api/set_cell_config.py+++ b/marimo/_server/api/set_cell_config.py@@ -4,10 +4,11 @@ from __future__ import annotationsfrom dataclasses import dataclassfrom typing import Dict+import tornado.web+from marimo._ast.cell import CellId_tfrom marimo._runtime import requestsfrom marimo._server import sessions-from marimo._server.api.validated_handler import ValidatedHandlerfrom marimo._utils.parse_dataclass import parse_raw@@ -17,7 +18,18 @@ class SetCellConfig:configs: Dict[CellId_t, Dict[str, object]]-class SetCellConfigHandler(ValidatedHandler):+class SetCellConfigHandler(tornado.web.RequestHandler):+ # This mutates cell config in the kernel.+ #+ # NB: Checking server token?+ #+ # - To prevent a frontend from accidentally setting the config of a kernel+ # it wasn't started for (eg, frontend created locally on a port that's+ # forwarded to a remote server for a different marimo app), we could+ # require a server token.+ # - But currently we don't check the server token in run mode to allow+ # connecting to a marimo server that died and came back, without+ # refreshing the pagedef post(self) -> None:session = sessions.require_session_from_header(self.request.headers)args = parse_raw(self.request.body, SetCellConfig)diff --git a/marimo/_server/api/set_ui_element_value.py b/marimo/_server/api/set_ui_element_value.pyindex ef2aaf5f66..fd5523c02c 100644--- a/marimo/_server/api/set_ui_element_value.py+++ b/marimo/_server/api/set_ui_element_value.py@@ -4,9 +4,10 @@ from __future__ import annotationsfrom dataclasses import dataclassfrom typing import Any, List+import tornado.web+from marimo._runtime import requestsfrom marimo._server import sessions-from marimo._server.api.validated_handler import ValidatedHandlerfrom marimo._utils.parse_dataclass import parse_raw@@ -18,7 +19,7 @@ class SetUIElementValue:values: List[Any]-class SetUIElementValueHandler(ValidatedHandler):+class SetUIElementValueHandler(tornado.web.RequestHandler):def post(self) -> None:session = sessions.require_session_from_header(self.request.headers)args = parse_raw(self.request.body, SetUIElementValue)