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
feat: support unix-style piping for marimo edit (#3867)
Akshay Agrawale894df88e2e6
diff --git a/marimo/_cli/cli.py b/marimo/_cli/cli.pyindex f9b88321d5..ba272e8b5a 100644--- a/marimo/_cli/cli.py+++ b/marimo/_cli/cli.py@@ -35,7 +35,8 @@ from marimo._tutorials import (create_temp_tutorial_file,tutorial_order,)-from marimo._utils.marimo_path import MarimoPath+from marimo._utils.marimo_path import MarimoPath, create_temp_notebook_file+from marimo._utils.platform import is_windowsdef helpful_usage_error(self: Any, file: Any = None) -> None:@@ -330,6 +331,34 @@ def edit() -> None:from marimo._cli.sandbox import prompt_run_in_sandbox+ # We support unix-style piping, e.g. cat notebook.py | marimo edit+ # Utility to support unix-style piping, e.g. cat notebook.py | marimo edit+ #+ # This check is complicated, because we need to support running+ #+ # marimo edit+ #+ # without a filename as well. To distinguish between `marimo edit` and+ # `... | marimo edit`, we need to check if sys.stdin() has data on it in a+ # nonblocking way. This does not seem to be possible on Windows, but it+ # is possible on unix-like systems with select.+ if name is None and not is_windows():+ import select++ try:+ if (+ not sys.stdin.isatty()+ and select.select([sys.stdin], [], [], 0)[0]+ and (contents := sys.stdin.read().strip())+ ):+ temp_dir = tempfile.TemporaryDirectory()+ path = create_temp_notebook_file(+ "notebook.py", "py", contents, temp_dir+ )+ name = path.absolute_name+ except Exception:+ pass+# If file is a url, we prompt to run in docker# We only do this for remote files,# but later we can make this a CLI flagdiff --git a/marimo/_tutorials/__init__.py b/marimo/_tutorials/__init__.pyindex d671853025..e477672c97 100644--- a/marimo/_tutorials/__init__.py+++ b/marimo/_tutorials/__init__.py@@ -6,7 +6,7 @@ import osimport tempfilefrom typing import get_args, Literal, Union-from marimo._utils.marimo_path import MarimoPath+from marimo._utils.marimo_path import MarimoPath, create_temp_notebook_filePythonTutorial = Literal[@@ -35,15 +35,18 @@ tutorial_order: list[Tutorial] = ["markdown-format","for-jupyter-users",]-assert set(tutorial_order) == set(get_args(PythonTutorial)) | set(get_args(MarkdownTutorial)), "Tutorial missing"+assert set(tutorial_order) == set(get_args(PythonTutorial)) | set(+ get_args(MarkdownTutorial)+), "Tutorial missing"def get_tutorial_source(name: Tutorial) -> str:if name in get_args(PythonTutorial):name = name.replace("-", "_")# from marimo._tutorials import <name>- tutorial = getattr(__import__("marimo._tutorials", fromlist=[name]),- name)+ tutorial = getattr(+ __import__("marimo._tutorials", fromlist=[name]), name+ )return inspect.getsource(tutorial)assert name in get_args(MarkdownTutorial)name = name.replace("-", "_")@@ -51,10 +54,10 @@ def get_tutorial_source(name: Tutorial) -> str:with open(file, "r", encoding="utf8") as f:return f.read()-def create_temp_tutorial_file(name: Tutorial, temp_dir: tempfile.TemporaryDirectory[str]) -> MarimoPath:++def create_temp_tutorial_file(+ name: Tutorial, temp_dir: tempfile.TemporaryDirectory[str]+) -> MarimoPath:source = get_tutorial_source(name)extension = "py" if name in get_args(PythonTutorial) else "md"- fname = os.path.join(temp_dir.name, f"{name}.{extension}")- path = MarimoPath(fname)- path.write_text(source)- return path+ return create_temp_notebook_file(name, extension, source, temp_dir)diff --git a/marimo/_utils/marimo_path.py b/marimo/_utils/marimo_path.pyindex 64c2de78f3..bd39c10802 100644--- a/marimo/_utils/marimo_path.py+++ b/marimo/_utils/marimo_path.py@@ -1,8 +1,24 @@# Copyright 2024 Marimo. All rights reserved.from __future__ import annotations+import osfrom pathlib import Path-from typing import Union+from typing import TYPE_CHECKING, Literal, Union++if TYPE_CHECKING:+ import tempfile+++def create_temp_notebook_file(+ name: str,+ extension: Literal["py", "md"],+ source: str,+ temp_dir: tempfile.TemporaryDirectory[str],+) -> MarimoPath:+ fname = os.path.join(temp_dir.name, f"{name}.{extension}")+ path = MarimoPath(fname)+ path.write_text(source)+ return pathclass MarimoPath:diff --git a/tests/_ast/test_app.py b/tests/_ast/test_app.pyindex cae06c3120..2a4c6537ea 100644--- a/tests/_ast/test_app.py+++ b/tests/_ast/test_app.py@@ -386,8 +386,7 @@ class TestApp:@app.celldef __() -> tuple[Any]:- def foo() -> None:- ...+ def foo() -> None: ...return (foo,)@@ -654,6 +653,9 @@ class TestAppComposition:assert result.output.text == vstack(["hello", "world"]).textassert not result.defs+ @pytest.mark.xfail(+ True, reason="Flaky in CI, can't repro locally", strict=False+ )async def test_app_comp_basic(self, k: Kernel, exec_req: ExecReqProvider) -> None:@@ -705,6 +707,9 @@ class TestAppComposition:assert "value is first" not in htmlassert "value is second" in html+ @pytest.mark.xfail(+ True, reason="Flaky in CI, can't repro locally", strict=False+ )async def test_app_comp_multiple_ui_elements(self, k: Kernel, exec_req: ExecReqProvider) -> None:diff --git a/tests/_cli/test_cli.py b/tests/_cli/test_cli.pyindex fedd83335b..9a61ae93e2 100644--- a/tests/_cli/test_cli.py+++ b/tests/_cli/test_cli.py@@ -211,7 +211,7 @@ def test_cli_edit_none() -> None:contents = _try_fetch(port)_check_contents(p, b"marimo-mode data-mode='home'", contents)_check_contents(- p, f"marimo-version data-version='{__version__}'".encode(), contents+ p, f"marimo-version data-version='{__version__}".encode(), contents)_check_contents(p, b"marimo-server-token", contents)@@ -235,7 +235,7 @@ def test_cli_edit_token() -> None:contents = _try_fetch(port, "localhost", "secret")_check_contents(p, b"marimo-mode data-mode='home'", contents)_check_contents(- p, f"marimo-version data-version='{__version__}'".encode(), contents+ p, f"marimo-version data-version='{__version__}".encode(), contents)_check_contents(p, b"marimo-server-token", contents)@@ -258,7 +258,7 @@ def test_cli_edit_directory() -> None:contents = _try_fetch(port)_check_contents(p, b"marimo-mode data-mode='home'", contents)_check_contents(- p, f"marimo-version data-version='{__version__}'".encode(), contents+ p, f"marimo-version data-version='{__version__}".encode(), contents)_check_contents(p, b"marimo-server-token", contents)@@ -282,7 +282,7 @@ def test_cli_edit_new_file() -> None:contents = _try_fetch(port)_check_contents(p, b"marimo-mode data-mode='edit'", contents)_check_contents(- p, f"marimo-version data-version='{__version__}'".encode(), contents+ p, f"marimo-version data-version='{__version__}".encode(), contents)_check_contents(p, b"marimo-server-token", contents)@@ -307,7 +307,7 @@ def test_cli_edit_with_additional_args(temp_marimo_file: str) -> None:contents = _try_fetch(port)_check_contents(p, b"marimo-mode data-mode='edit'", contents)_check_contents(- p, f"marimo-version data-version='{__version__}'".encode(), contents+ p, f"marimo-version data-version='{__version__}".encode(), contents)@@ -374,7 +374,7 @@ def test_cli_new() -> None:contents = _try_fetch(port)_check_contents(p, b"marimo-mode data-mode='edit'", contents)_check_contents(- p, f"marimo-version data-version='{__version__}'".encode(), contents+ p, f"marimo-version data-version='{__version__}".encode(), contents)_check_contents(p, b"marimo-server-token", contents)@@ -387,7 +387,7 @@ def test_cli_run(temp_marimo_file: str) -> None:contents = _try_fetch(port)_check_contents(p, b"marimo-mode data-mode='read'", contents)_check_contents(- p, f"marimo-version data-version='{__version__}'".encode(), contents+ p, f"marimo-version data-version='{__version__}".encode(), contents)@@ -407,7 +407,7 @@ def test_cli_run_with_show_code(temp_marimo_file: str) -> None:contents = _try_fetch(port)_check_contents(p, b"marimo-mode data-mode='read'", contents)_check_contents(- p, f"marimo-version data-version='{__version__}'".encode(), contents+ p, f"marimo-version data-version='{__version__}".encode(), contents)@@ -429,7 +429,7 @@ def test_cli_run_with_additional_args(temp_marimo_file: str) -> None:contents = _try_fetch(port)_check_contents(p, b"marimo-mode data-mode='read'", contents)_check_contents(- p, f"marimo-version data-version='{__version__}'".encode(), contents+ p, f"marimo-version data-version='{__version__}".encode(), contents)@@ -449,7 +449,7 @@ def test_cli_tutorial() -> None:contents = _try_fetch(port)_check_contents(p, b"marimo-mode data-mode='edit'", contents)_check_contents(- p, f"marimo-version data-version='{__version__}'".encode(), contents+ p, f"marimo-version data-version='{__version__}".encode(), contents)_check_contents(p, b"intro.py", contents)@@ -470,7 +470,7 @@ def test_cli_md_tutorial() -> None:contents = _try_fetch(port)_check_contents(p, b"marimo-mode data-mode='edit'", contents)_check_contents(- p, f"marimo-version data-version='{__version__}'".encode(), contents+ p, f"marimo-version data-version='{__version__}".encode(), contents)_check_contents(p, b"markdown-format.md", contents)@@ -483,7 +483,7 @@ def test_cli_md_run(temp_md_marimo_file: str) -> None:contents = _try_fetch(port)_check_contents(p, b"marimo-mode data-mode='read'", contents)_check_contents(- p, f"marimo-version data-version='{__version__}'".encode(), contents+ p, f"marimo-version data-version='{__version__}".encode(), contents)@@ -504,7 +504,7 @@ def test_cli_md_edit(temp_md_marimo_file: str) -> None:contents = _try_fetch(port)_check_contents(p, b"marimo-mode data-mode='edit'", contents)_check_contents(- p, f"marimo-version data-version='{__version__}'".encode(), contents+ p, f"marimo-version data-version='{__version__}".encode(), contents)