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: marimo export ipynb (#1367)
Akshay Agrawal4cb3069cec16
diff --git a/docs/guides/exporting.md b/docs/guides/exporting.mdindex 0a7fc8e6ea..364ac7ca3c 100644--- a/docs/guides/exporting.md+++ b/docs/guides/exporting.md@@ -1,5 +1,11 @@# Exporting+Export marimo notebooks to other file formats at the command line using++```+marimo export+```+## Export to static HTMLExport the current view your notebook to static HTML via the notebook@@ -26,8 +32,8 @@ marimo export html notebook.py -o notebook.html --watch## Export to a Python script-Export the notebook to a flat Python script at the command-line.-This exports the notebook in topological order, so the cells adhere to their dependency graph.+Export to a flat Python script in topological order, so the cells adhere to+their dependency graph.```bashmarimo export script notebook.py -o notebook.script.py@@ -36,14 +42,15 @@ marimo export script notebook.py -o notebook.script.py```{admonition} Top-level await not supported:class: warning-Exporting to a flat Python script does not support top-level await.-If you have top-level await in your notebook, you can still execute the notebook as a script with `python notebook.py`.+Exporting to a flat Python script does not support top-level await. If you have+top-level await in your notebook, you can still execute the notebook as a+script with `python notebook.py`.```## Export to markdown-Export the notebook to markdown at the command-line.-This exports the notebook in top to bottom order, so the cells are in the order as they appear in the notebook.+Export to markdown notebook in top to bottom order, so the cells are in the+order as they appear in the notebook.```bashmarimo export md notebook.py -o notebook.md@@ -56,3 +63,12 @@ You can also convert the markdown back to a marimo notebook:```bashmarimo convert notebook.md > notebook.py```++## Export to Jupyter notebook++Export to Jupyter notebook in topological order, so the cells adhere to+their dependency graph.++```bash+marimo export ipynb notebook.py -o notebook.ipynb+```diff --git a/marimo/_cli/export/commands.py b/marimo/_cli/export/commands.pyindex d53c5a85c2..e138dcd8b8 100644--- a/marimo/_cli/export/commands.py+++ b/marimo/_cli/export/commands.py@@ -2,6 +2,7 @@from __future__ import annotationsimport asyncio+import importlib.utilfrom typing import TYPE_CHECKING, Callableimport click@@ -9,6 +10,7 @@ import clickfrom marimo._cli.parse_args import parse_argsfrom marimo._cli.print import greenfrom marimo._server.export import (+ export_as_ipynb,export_as_md,export_as_script,run_app_then_export_as_html,@@ -252,6 +254,65 @@ def md(return watch_and_export(MarimoPath(name), output, watch, export_callback)+ help="""+Export a marimo notebook as a Jupyter notebook in topological order.++Example:++ \b+ * marimo export ipynb notebook.py -o notebook.ipynb++Watch for changes and regenerate the script on modification:++ \b+ * marimo export ipynb notebook.py -o notebook.ipynb --watch++Requires nbformat to be installed.+"""+)+ "--watch/--no-watch",+ default=False,+ show_default=True,+ type=bool,+ help="""+ Watch notebook for changes and regenerate the ipynb on modification.+ If watchdog is installed, it will be used to watch the file.+ Otherwise, file watcher will poll the file every 1s.+ """,+)+ "-o",+ "--output",+ type=str,+ default=None,+ help="""+ Output file to save the ipynb file to. If not provided, the ipynb contents+ will be printed to stdout.+ """,+)[email protected]("name", required=True)+def ipynb(+ name: str,+ output: str,+ watch: bool,+) -> None:+ """+ Export a marimo notebook as a Jupyter notebook in topological order.+ """++ def export_callback(file_path: MarimoPath) -> str:+ return export_as_ipynb(file_path)[0]++ if importlib.util.find_spec("nbformat") is None:+ raise ModuleNotFoundError(+ "Install `nbformat` from PyPI to use marimo export ipynb"+ )+ return watch_and_export(MarimoPath(name), output, watch, export_callback)++export.add_command(html)export.add_command(script)export.add_command(md)+export.add_command(ipynb)diff --git a/marimo/_dependencies/dependencies.py b/marimo/_dependencies/dependencies.pyindex fa851b6842..c9b472ef9f 100644--- a/marimo/_dependencies/dependencies.py+++ b/marimo/_dependencies/dependencies.py@@ -144,7 +144,7 @@ class DependencyManager:@staticmethoddef has_matplotlib() -> bool:- """Return True if plotly is installed."""+ """Return True if matplotlib is installed."""return importlib.util.find_spec("matplotlib") is not None@staticmethod@@ -161,3 +161,8 @@ class DependencyManager:def has_ipython() -> bool:"""Return True if IPython is installed."""return importlib.util.find_spec("IPython") is not None++ @staticmethod+ def has_nbformat() -> bool:+ """Return True if nbformat is installed."""+ return importlib.util.find_spec("nbformat") is not Nonediff --git a/marimo/_server/api/endpoints/login.py b/marimo/_server/api/endpoints/login.pyindex a2c4f4fab9..60ae38025f 100644--- a/marimo/_server/api/endpoints/login.py+++ b/marimo/_server/api/endpoints/login.py@@ -1,3 +1,4 @@+# Copyright 2024 Marimo. All rights reserved.from __future__ import annotationsfrom typing import TYPE_CHECKINGdiff --git a/marimo/_server/export/__init__.py b/marimo/_server/export/__init__.pyindex 9fc8ee972d..b603c93959 100644--- a/marimo/_server/export/__init__.py+++ b/marimo/_server/export/__init__.py@@ -40,6 +40,17 @@ def export_as_md(return Exporter().export_as_md(file_manager)+def export_as_ipynb(+ path: MarimoPath,+) -> tuple[str, str]:+ file_router = AppFileRouter.from_filename(path)+ file_key = file_router.get_unique_file_key()+ assert file_key is not None+ file_manager = file_router.get_file_manager(file_key)++ return Exporter().export_as_ipynb(file_manager)++async def run_app_then_export_as_html(path: MarimoPath,include_code: bool,diff --git a/marimo/_server/export/exporter.py b/marimo/_server/export/exporter.pyindex b100e26d4a..43b5dbd815 100644--- a/marimo/_server/export/exporter.py+++ b/marimo/_server/export/exporter.py@@ -1,11 +1,13 @@# Copyright 2024 Marimo. All rights reserved.from __future__ import annotations+import ioimport mimetypesimport osfrom typing import castfrom marimo import __version__+from marimo._ast.cell import Cell, CellImplfrom marimo._config.config import (DEFAULT_CONFIG,DisplayConfig,@@ -119,6 +121,36 @@ class Exporter:download_filename = get_download_filename(file_manager, ".script.py")return code, download_filename+ def export_as_ipynb(+ self,+ file_manager: AppFileManager,+ ) -> tuple[str, str]:+ import nbformat # type: ignore++ def create_notebook_cell(cell: CellImpl) -> nbformat.NotebookNode:+ markdown_string = get_markdown_from_cell(+ Cell(_name="__", _cell=cell), cell.code+ )+ if markdown_string is not None:+ return nbformat.v4.new_markdown_cell( # type: ignore+ markdown_string, id=cell.cell_id+ )+ else:+ return nbformat.v4.new_code_cell(cell.code, id=cell.cell_id) # type: ignore++ notebook = nbformat.v4.new_notebook() # type: ignore+ graph = file_manager.app.graph+ notebook["cells"] = [+ create_notebook_cell(graph.cells[cid]) # type: ignore+ for cid in dataflow.topological_sort(graph, graph.cells.keys())+ ]++ stream = io.StringIO()+ nbformat.write(notebook, stream) # type: ignore+ stream.seek(0)+ download_filename = get_download_filename(file_manager, ".ipynb")+ return stream.read(), download_filename+def export_as_md(self, file_manager: AppFileManager) -> tuple[str, str]:import yamldiff --git a/pyproject.toml b/pyproject.tomlindex 58b9df62e5..f65ab556da 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -132,6 +132,8 @@ testoptional = ["anywidget~=0.9.3","ipython~=8.12.3","openai~=1.12.0",+ # exporting as ipynb+ "nbformat >= 5.0.0",][project.urls]diff --git a/tests/_cli/snapshots/ipynb.txt b/tests/_cli/snapshots/ipynb.txtnew file mode 100644index 0000000000..b51916d9d1--- /dev/null+++ b/tests/_cli/snapshots/ipynb.txt@@ -0,0 +1,36 @@+{+ "cells": [+ {+ "cell_type": "code",+ "execution_count": null,+ "id": "Hbol",+ "metadata": {},+ "outputs": [],+ "source": [+ "import marimo as mo"+ ]+ },+ {+ "cell_type": "markdown",+ "id": "MJUe",+ "metadata": {},+ "source": [+ "markdown"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": null,+ "id": "vblA",+ "metadata": {},+ "outputs": [],+ "source": [+ "mo.md(f\"parametrized markdown {123}\")"+ ]+ }+ ],+ "metadata": {},+ "nbformat": 4,+ "nbformat_minor": 5+}+diff --git a/tests/_cli/test_cli_export.py b/tests/_cli/test_cli_export.pyindex 84ef3dd6da..5016a80449 100644--- a/tests/_cli/test_cli_export.py+++ b/tests/_cli/test_cli_export.py@@ -434,3 +434,19 @@ class TestExportMarkdown:in line)break+++class TestExportIpynb:+ @pytest.mark.skipif(+ not DependencyManager.has_nbformat(),+ reason="This test requires nbformat.",+ )+ def test_export_ipynb(self, temp_marimo_file_with_md: str) -> None:+ p = subprocess.run(+ ["marimo", "export", "ipynb", temp_marimo_file_with_md],+ capture_output=True,+ )+ assert p.returncode == 0, p.stderr.decode()+ output = p.stdout.decode()+ # ipynb has non-deterministic ids+ snapshot("ipynb.txt", output)diff --git a/tests/conftest.py b/tests/conftest.pyindex 152729d3b2..bf15b53830 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -262,6 +262,44 @@ def temp_unparsable_marimo_file() -> Generator[str, None, None]:tmp_dir.cleanup()+def temp_marimo_file_with_md() -> Generator[str, None, None]:+ tmp_dir = TemporaryDirectory()+ tmp_file = tmp_dir.name + "/notebook.py"+ content = inspect.cleandoc(+ """+ import marimo+ app = marimo.App()++ @app.cell+ def __():+ import marimo as mo+ return mo,++ @app.cell+ def __(mo):+ mo.md("markdown")+ return++ @app.cell+ def __(mo):+ mo.md(f"parametrized markdown {123}")+ return+++ if __name__ == "__main__":+ app.run()+ """+ )++ try:+ with open(tmp_file, "w") as f:+ f.write(content)+ yield tmp_file+ finally:+ tmp_dir.cleanup()++# Factory to create ExecutionRequests and abstract away cell IDclass ExecReqProvider:def __init__(self) -> None:diff --git a/tests/mocks.py b/tests/mocks.pyindex 110bc5e6e8..9c83e929db 100644--- a/tests/mocks.py+++ b/tests/mocks.py@@ -56,7 +56,6 @@ def snapshotter(current_file: str) -> Callable[[str, str], None]:)))-assert result == expected, f"Snapshot differs:\n{text_diff}"return snapshot