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.

feat: marimo new prompt.txt (#4426)

Akshay Agrawal373fe21ba145
diff --git a/marimo/_cli/cli.py b/marimo/_cli/cli.py
index fef6653076..8e07f3cb16 100644
--- a/marimo/_cli/cli.py
+++ b/marimo/_cli/cli.py
@@ -77,7 +77,7 @@ def _key_value_bullets(items: list[tuple[str, str]]) -> str:
lines: list[str] = []
def _sep(desc: str) -> str:
- return ":" if desc else ""
+ return " " if desc else ""
for key, desc in items:
# "\b" tells click not to reformat our text
@@ -210,6 +210,38 @@ def main(
GLOBAL_SETTINGS.LOG_LEVEL = _loggers.log_level_string_to_int(log_level)
+def _get_stdin_contents() -> str | None:
+ # Utiity to get data from stdin a nonblocking way.
+ #
+ # Not supported on Windows.
+ #
+ # 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 not is_windows():
+ import select
+
+ try:
+ if (
+ not sys.stdin.isatty()
+ and select.select([sys.stdin], [], [], 0)[0]
+ and (contents := sys.stdin.read().strip())
+ ):
+ return contents
+ except Exception:
+ ...
+
+ return None
+
+
edit_help_msg = "\n".join(
[
"\b",
@@ -334,32 +366,12 @@ def edit(
args: tuple[str, ...],
) -> None:
# 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 name is None and (stdin_contents := _get_stdin_contents()) is not None:
+ temp_dir = tempfile.TemporaryDirectory()
+ path = create_temp_notebook_file(
+ "notebook.py", "py", stdin_contents, temp_dir
+ )
+ name = path.absolute_name
# If file is a url, we prompt to run in docker
# We only do this for remote files,
@@ -443,7 +455,34 @@ def edit(
)
[email protected](help="Create a new notebook.")
+new_help_msg = "\n".join(
+ [
+ "\b",
+ "Create an empty notebook, or generate from a prompt with AI",
+ "",
+ _key_value_bullets(
+ [
+ (
+ "marimo new",
+ "Create an empty notebook",
+ ),
+ (
+ 'marimo new "Plot an interactive 3D surface with matplotlib."',
+ "Generate a notebook from a prompt.",
+ ),
+ (
+ "marimo new prompt.txt",
+ "Generate a notebook from a file containing a prompt.",
+ ),
+ ]
+ ),
+ "",
+ "Visit https://marimo.app/ai for more prompt examples.",
+ ]
+)
+
+
[email protected](help=new_help_msg)
@click.option(
"-p",
"--port",
@@ -523,11 +562,20 @@ def new(
return
file_router: Optional[AppFileRouter] = None
- if prompt:
+
+ if prompt is None:
+ # We support unix-style prompting, cat prompt.txt | marimo new
+ prompt = _get_stdin_contents()
+
+ if prompt is not None:
import tempfile
from marimo._ai.text_to_notebook import text_to_notebook
+ maybe_path = Path(prompt)
+ if maybe_path.is_file():
+ prompt = maybe_path.read_text()
+
try:
notebook_content = text_to_notebook(prompt)
temp_file = tempfile.NamedTemporaryFile(suffix=".py")
diff --git a/marimo/_tutorials/dataflow.py b/marimo/_tutorials/dataflow.py
index 8b5ed2d372..57c5ac15d9 100644
--- a/marimo/_tutorials/dataflow.py
+++ b/marimo/_tutorials/dataflow.py
@@ -1,3 +1,4 @@
+# Copyright 2025 Marimo. All rights reserved.
# /// script
# requires-python = ">=3.12"
# dependencies = [
diff --git a/marimo/_tutorials/markdown.py b/marimo/_tutorials/markdown.py
index fbd195fca9..9d7f1d14ef 100644
--- a/marimo/_tutorials/markdown.py
+++ b/marimo/_tutorials/markdown.py
@@ -1,3 +1,4 @@
+# Copyright 2025 Marimo. All rights reserved.
# /// script
# requires-python = ">=3.12"
# dependencies = [
diff --git a/marimo/_tutorials/plots.py b/marimo/_tutorials/plots.py
index ff2ee301ef..9beeedaf83 100644
--- a/marimo/_tutorials/plots.py
+++ b/marimo/_tutorials/plots.py
@@ -1,3 +1,4 @@
+# Copyright 2025 Marimo. All rights reserved.
# /// script
# requires-python = ">=3.12"
# dependencies = [