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.

improvement: add -o to marimo convert CLI (#2461)

Myles Scolnick8540131833c2
diff --git a/marimo/_cli/convert/commands.py b/marimo/_cli/convert/commands.py
index 9b412d4f00..f7c782a1b3 100644
--- a/marimo/_cli/convert/commands.py
+++ b/marimo/_cli/convert/commands.py
@@ -9,11 +9,23 @@ from marimo._cli.convert.markdown import convert_from_md
from marimo._cli.convert.utils import load_external_file
from marimo._cli.print import echo
from marimo._convert.ipynb import convert_from_ipynb
+from marimo._utils.paths import maybe_make_dirs
@click.argument("filename", required=True)
+ "-o",
+ "--output",
+ type=str,
+ default=None,
+ help="""
+ Output file to save the converted notebook to.
+ If not provided, the converted notebook will be printed to stdout.
+ """,
+)
def convert(
filename: str,
+ output: str,
) -> None:
r"""Convert a Jupyter notebook or Markdown file to a marimo notebook.
@@ -48,4 +60,12 @@ def convert(
else:
assert ext in (".md", ".qmd")
notebook = convert_from_md(text)
- echo(notebook)
+
+ if output:
+ # Make dirs if needed
+ maybe_make_dirs(output)
+ with open(output, "w") as f:
+ f.write(notebook)
+ echo(f"Converted notebook saved to {output}")
+ else:
+ echo(notebook)
diff --git a/tests/_cli/snapshots/ipynb_to_marimo.txt b/tests/_cli/snapshots/ipynb_to_marimo.txt
new file mode 100644
index 0000000000..3e1e3c2757
--- /dev/null
+++ b/tests/_cli/snapshots/ipynb_to_marimo.txt
@@ -0,0 +1,15 @@
+import marimo
+
+__generated_with = "0.8.22"
+app = marimo.App()
+
+
+def __():
+ print('Hello, World!')
+ return
+
+
+if __name__ == "__main__":
+ app.run()
+
diff --git a/tests/_cli/snapshots/ipynb_to_marimo_with_output.txt b/tests/_cli/snapshots/ipynb_to_marimo_with_output.txt
new file mode 100644
index 0000000000..724669e8b8
--- /dev/null
+++ b/tests/_cli/snapshots/ipynb_to_marimo_with_output.txt
@@ -0,0 +1,14 @@
+import marimo
+
+__generated_with = "0.8.22"
+app = marimo.App()
+
+
+def __():
+ print('Hello, Output!')
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_cli/snapshots/markdown_to_marimo.txt b/tests/_cli/snapshots/markdown_to_marimo.txt
new file mode 100644
index 0000000000..d61e276677
--- /dev/null
+++ b/tests/_cli/snapshots/markdown_to_marimo.txt
@@ -0,0 +1,20 @@
+import marimo
+
+__generated_with = "0.8.22"
+app = marimo.App()
+
+
+def __(mo):
+ mo.md(
+ r"""
+ # Test Markdown
+
+ print('Hello from Markdown!')
+ """
+ )
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_cli/test_cli_convert.py b/tests/_cli/test_cli_convert.py
new file mode 100644
index 0000000000..78ed28f622
--- /dev/null
+++ b/tests/_cli/test_cli_convert.py
@@ -0,0 +1,112 @@
+# Copyright 2024 Marimo. All rights reserved.
+from __future__ import annotations
+
+import subprocess
+from typing import TYPE_CHECKING
+
+from tests.mocks import snapshotter
+
+snapshot = snapshotter(__file__)
+
+if TYPE_CHECKING:
+ from pathlib import Path
+
+
+class TestConvert:
+ @staticmethod
+ def test_convert_ipynb(tmp_path: Path) -> None:
+ notebook_path = tmp_path / "test_notebook.ipynb"
+ notebook_content = """
+{
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "print('Hello, World!')"
+ ]
+ }
+ ],
+ "metadata": {},
+ "nbformat": 4,
+ "nbformat_minor": 4
+}
+"""
+ notebook_path.write_text(notebook_content)
+
+ p = subprocess.run(
+ ["marimo", "convert", str(notebook_path)],
+ capture_output=True,
+ text=True,
+ )
+ assert p.returncode == 0, p.stderr
+ output = p.stdout
+ snapshot("ipynb_to_marimo.txt", output)
+
+ @staticmethod
+ def test_convert_markdown(tmp_path: Path) -> None:
+ md_path = tmp_path / "test_markdown.md"
+ md_content = """
+# Test Markdown
+
+print('Hello from Markdown!')
+"""
+ md_path.write_text(md_content)
+
+ p = subprocess.run(
+ ["marimo", "convert", str(md_path)],
+ capture_output=True,
+ text=True,
+ )
+ assert p.returncode == 0, p.stderr
+ output = p.stdout
+ snapshot("markdown_to_marimo.txt", output)
+
+ @staticmethod
+ def test_convert_with_output(tmp_path: Path) -> None:
+ notebook_path = tmp_path / "test_notebook.ipynb"
+ notebook_content = """
+{
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "print('Hello, Output!')"
+ ]
+ }
+ ],
+ "metadata": {},
+ "nbformat": 4,
+ "nbformat_minor": 4
+}
+"""
+ notebook_path.write_text(notebook_content)
+ output_path = tmp_path / "output.py"
+
+ p = subprocess.run(
+ ["marimo", "convert", str(notebook_path), "-o", str(output_path)],
+ capture_output=True,
+ text=True,
+ )
+ assert p.returncode == 0, p.stderr
+ assert output_path.exists()
+ output_content = output_path.read_text()
+ snapshot("ipynb_to_marimo_with_output.txt", output_content)
+
+ @staticmethod
+ def test_convert_invalid_file(tmp_path: Path) -> None:
+ invalid_file = tmp_path / "invalid.txt"
+ invalid_file.touch()
+
+ p = subprocess.run(
+ ["marimo", "convert", str(invalid_file)],
+ capture_output=True,
+ text=True,
+ )
+ assert p.returncode != 0
+ assert "File must be an .ipynb or .md file" in p.stderr