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
Create ipynb fixtures for `marimo convert` and snapshot outputs (#5179)
Trevor Manz748685cf14dc
diff --git a/pyproject.toml b/pyproject.tomlindex 9a6f2667e6..caf8642e72 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -330,6 +330,7 @@ exclude = ["tests/_cli/ipynb_data","tests/_runtime/runtime_data","tests/_ast/test_app.py",+ "tests/_convert/ipynb_data/*.ipynb","frontend","docs","build",diff --git a/scripts/generate_ipynb_fixtures.py b/scripts/generate_ipynb_fixtures.pynew file mode 100644index 0000000000..7ed5c09e8e--- /dev/null+++ b/scripts/generate_ipynb_fixtures.py@@ -0,0 +1,79 @@+# /// script+# requires-python = ">=3.13"+# dependencies = [+# "jupytext",+# "nbformat",+# ]+#+# [tool.uv]+# exclude-newer = "2025-06-03T16:30:20.082913-04:00"+# ///+"""+Generate `.ipynb` test fixtures for marimo's notebook conversion pipeline.++Each fixture corresponds to a named Jupyter notebook containing one or more code cells.+To add a new fixture, call `create_notebook_fixture(name, sources)`:++- `name`: the output filename (without `.ipynb`)+- `sources`: a list of code cell contents (as strings or `nbformat` cell dicts)++Run this script with `uv run scripts/generate_ipynb_fixtures.py` to regenerate all fixtures.+Output notebooks are written to `tests/_convert/ipynb_data/`.+"""++from __future__ import annotations+from io import FileIO+from pathlib import Path++import nbformat.v4.nbbase as nb+import jupytext+++SELF_DIR = Path(__file__).parent+FIXTURES_DIR = SELF_DIR / "../tests/_convert/ipynb_data"+++def create_notebook_fixture(name: str, sources: list[str | dict]) -> None:+ cells = []+ for source in sources:+ if isinstance(source, str):+ cell = nb.new_code_cell(source)+ else:+ cell = source+ cells.append(cell)++ notebook = nb.new_notebook(cells=cells)++ for i, cell in enumerate(notebook.cells):+ cell.id = str(i) # ensure we always have 1,2,3,4++ (FIXTURES_DIR / f"{name}.ipynb").write_text(+ jupytext.writes(notebook, fmt="ipynb")+ )+++def main() -> None:+ FIXTURES_DIR.mkdir(exist_ok=True)+ create_notebook_fixture(+ "multiple_definitions",+ [+ "x = 1\nprint(x) # print",+ "x = 2\nprint(x) # print",+ ],+ )+ create_notebook_fixture(+ "multiple_definitions_multiline",+ [+ "K = 2\nnearest_partition = np.argpartition(dist_sq_1, K + 1, axis=1)",+ """plt.scatter(X_1[:, 0], X_1[:, 1], s=100)+K = 2+for i_1 in range(X_1.shape[0]):+ for j in nearest_partition[i_1, :K + 1]:+ plt.plot(*zip(X_1[j], X_1[i_1]), color='black')\+""",+ ],+ )+++if __name__ == "__main__":+ main()diff --git a/tests/_convert/ipynb_data/multiple_definitions.ipynb b/tests/_convert/ipynb_data/multiple_definitions.ipynbnew file mode 100644index 0000000000..13f3cee8ed--- /dev/null+++ b/tests/_convert/ipynb_data/multiple_definitions.ipynb@@ -0,0 +1,29 @@+{+ "cells": [+ {+ "cell_type": "code",+ "execution_count": null,+ "id": "0",+ "metadata": {},+ "outputs": [],+ "source": [+ "x = 1\n",+ "print(x) # print"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": null,+ "id": "1",+ "metadata": {},+ "outputs": [],+ "source": [+ "x = 2\n",+ "print(x) # print"+ ]+ }+ ],+ "metadata": {},+ "nbformat": 4,+ "nbformat_minor": 5+}\ No newline at end of filediff --git a/tests/_convert/ipynb_data/multiple_definitions_multiline.ipynb b/tests/_convert/ipynb_data/multiple_definitions_multiline.ipynbnew file mode 100644index 0000000000..1e1117b416--- /dev/null+++ b/tests/_convert/ipynb_data/multiple_definitions_multiline.ipynb@@ -0,0 +1,32 @@+{+ "cells": [+ {+ "cell_type": "code",+ "execution_count": null,+ "id": "0",+ "metadata": {},+ "outputs": [],+ "source": [+ "K = 2\n",+ "nearest_partition = np.argpartition(dist_sq_1, K + 1, axis=1)"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": null,+ "id": "1",+ "metadata": {},+ "outputs": [],+ "source": [+ "plt.scatter(X_1[:, 0], X_1[:, 1], s=100)\n",+ "K = 2\n",+ "for i_1 in range(X_1.shape[0]):\n",+ " for j in nearest_partition[i_1, :K + 1]:\n",+ " plt.plot(*zip(X_1[j], X_1[i_1]), color='black')"+ ]+ }+ ],+ "metadata": {},+ "nbformat": 4,+ "nbformat_minor": 5+}\ No newline at end of filediff --git a/tests/_convert/snapshots/convert_multiple_definitions.py.txt b/tests/_convert/snapshots/convert_multiple_definitions.py.txtnew file mode 100644index 0000000000..e668aef49a--- /dev/null+++ b/tests/_convert/snapshots/convert_multiple_definitions.py.txt@@ -0,0 +1,21 @@+import marimo++app = marimo.App()+++def _():+ _x = 1+ print(_x)+ return+++def _():+ _x = 2+ print(_x)+ return+++if __name__ == "__main__":+ app.run()diff --git a/tests/_convert/snapshots/convert_multiple_definitions_multiline.py.txt b/tests/_convert/snapshots/convert_multiple_definitions_multiline.py.txtnew file mode 100644index 0000000000..e0218ec623--- /dev/null+++ b/tests/_convert/snapshots/convert_multiple_definitions_multiline.py.txt@@ -0,0 +1,24 @@+import marimo++app = marimo.App()+++def _(dist_sq_1, np):+ _K = 2+ nearest_partition = np.argpartition(dist_sq_1, _K + 1, axis=1)+ return (nearest_partition,)+++def _(X_1, nearest_partition, plt):+ plt.scatter(X_1[:, 0], X_1[:, 1], s=100)+ _K = 2+ for i_1 in range(X_1.shape[0]):+ for j in nearest_partition[i_1, :_K + 1]:+ plt.plot(*zip(X_1[j], X_1[i_1]), color='black')+ return+++if __name__ == "__main__":+ app.run()diff --git a/tests/_convert/test_convert_ipynb.py b/tests/_convert/test_convert_ipynb.pynew file mode 100644index 0000000000..9dbc908161--- /dev/null+++ b/tests/_convert/test_convert_ipynb.py@@ -0,0 +1,27 @@+import pathlib+import re++import pytest++from marimo._convert.converters import MarimoConvert+from marimo._convert.ipynb import convert_from_ipynb_to_notebook_ir+from tests.mocks import snapshotter++SELF_DIR = pathlib.Path(__file__).parent+snapshot_test = snapshotter(__file__)+++ "ipynb_path", (SELF_DIR / "ipynb_data").glob("*.ipynb")+)+def test_marimo_convert_snapshots(ipynb_path: pathlib.Path) -> None:+ """Test marimo convert against all notebook fixtures using snapshots."""+ contents = ipynb_path.read_text()+ ir = convert_from_ipynb_to_notebook_ir(contents)+ converted = MarimoConvert.from_ir(ir).to_py()++ converted = re.sub(r"__generated_with = .*\n", "", converted)+ converted = re.sub(r"# requires-python = .*\n", "", converted)++ snapshot_name = f"convert_{ipynb_path.name.replace('.ipynb', '.py.txt')}"+ snapshot_test(snapshot_name, converted)