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: include marimo metadata on ipynb export (#7878)
Dylan Madisettia0f0b412fd03
diff --git a/marimo/_convert/ipynb/from_ir.py b/marimo/_convert/ipynb/from_ir.pyindex bf8715510a..fa9129ecf3 100644--- a/marimo/_convert/ipynb/from_ir.py+++ b/marimo/_convert/ipynb/from_ir.py@@ -29,6 +29,18 @@ if TYPE_CHECKING:from marimo._session.state.session_view import SessionView+# Note: We intentionally omit "version" as it would vary across environments+# and break reproducibility. The marimo_version in metadata is sufficient.+DEFAULT_LANGUAGE_INFO = {+ "codemirror_mode": {"name": "ipython", "version": 3},+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3",+}++def convert_from_ir_to_ipynb(app: InternalApp,*,@@ -46,12 +58,30 @@ def convert_from_ir_to_ipynb(Returns:JSON string of the .ipynb notebook"""+DependencyManager.nbformat.require("to convert marimo notebooks to ipynb")import nbformat # type: ignore[import-not-found]+ from marimo import __version__+notebook = nbformat.v4.new_notebook() # type: ignore[no-untyped-call]notebook["cells"] = []+ # Add marimo-specific notebook metadata+ marimo_metadata: dict[str, Any] = {+ "marimo_version": __version__,+ }+ app_config_diff = app.config.asdict_difference()+ if app_config_diff:+ marimo_metadata["app_config"] = app_config_diff+ # Include header if present (PEP 723 metadata, docstrings, etc.)+ if app._app._header:+ marimo_metadata["header"] = app._app._header+ notebook["metadata"]["marimo"] = marimo_metadata++ # Add standard Jupyter language_info (no kernelspec)+ notebook["metadata"]["language_info"] = DEFAULT_LANGUAGE_INFO+# Determine cell order based on sort_modeif sort_mode == "top-down":cell_data_list = list(app.cell_manager.cell_data())diff --git a/marimo/_convert/ipynb/to_ir.py b/marimo/_convert/ipynb/to_ir.pyindex 8f314ac12d..e87591df81 100644--- a/marimo/_convert/ipynb/to_ir.py+++ b/marimo/_convert/ipynb/to_ir.py@@ -849,6 +849,7 @@ def bind_cell_metadata(- If "hide-cell" is present in the tags, the cell is marked hidden (and removed)- Remaining tags (if any) are inserted as a comment at the top of the source.+ - If marimo-specific metadata is present, it is used to restore cell config."""cells: list[CodeCell] = []for source, meta, hide_code in zip(sources, metadata, hide_flags):@@ -858,8 +859,25 @@ def bind_cell_metadata(hide_code = Trueif tags:source = f"# Cell tags: {', '.join(sorted(tags))}\n{source}"++ # Extract marimo-specific cell config if present+ marimo_meta = meta.get("marimo", {})+ marimo_config = marimo_meta.get("config", {})++ # Merge marimo config with existing flags+ # marimo config takes precedence for hide_code if present+ if "hide_code" in marimo_config:+ hide_code = marimo_config["hide_code"]+cells.append(- CodeCell(source=source, config=CellConfig(hide_code=hide_code))+ CodeCell(+ source=source,+ config=CellConfig(+ hide_code=hide_code,+ column=marimo_config.get("column"),+ disabled=marimo_config.get("disabled", False),+ ),+ ))return cells@@ -1040,6 +1058,13 @@ def convert_from_ipynb_to_notebook_ir(Convert a raw notebook to a NotebookSerializationV1 object."""notebook = json.loads(raw_notebook)++ # Extract notebook-level marimo metadata if present+ notebook_metadata = notebook.get("metadata", {})+ marimo_nb_meta = notebook_metadata.get("marimo", {})+ app_config = marimo_nb_meta.get("app_config", {})+ stored_header = marimo_nb_meta.get("header")+sources: list[str] = []metadata: list[dict[str, Any]] = []hide_flags: list[bool] = []@@ -1067,13 +1092,20 @@ def convert_from_ipynb_to_notebook_ir(sources, metadata, hide_flags)+ # Use stored header if available, otherwise build from inline_meta+ # stored_header takes precedence when present (even if empty string)+ if stored_header is not None:+ header_value = stored_header+ else:+ header_value = build_metadata(inline_meta, extra_metadata)+return NotebookSerializationV1(- app=AppInstantiation(),- header=Header(value=build_metadata(inline_meta, extra_metadata)),+ app=AppInstantiation(options=app_config),+ header=Header(value=header_value),cells=[CellDef(code=cell.source,- options={"hide_code": cell.config.hide_code},+ options=cell.config.asdict(),)for cell in transformed_cells],diff --git a/tests/_cli/snapshots/ipynb.txt b/tests/_cli/snapshots/ipynb.txtindex f7fa45ac19..9f7dc7c801 100644--- a/tests/_cli/snapshots/ipynb.txt+++ b/tests/_cli/snapshots/ipynb.txt@@ -41,7 +41,22 @@]}],- "metadata": {},+ "metadata": {+ "language_info": {+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3"+ },+ "marimo": {+ "marimo_version": "0.0.0"+ }+ },"nbformat": 4,"nbformat_minor": 5}diff --git a/tests/_cli/snapshots/ipynb_topdown.txt b/tests/_cli/snapshots/ipynb_topdown.txtindex 0b3a8e7808..06c68b9981 100644--- a/tests/_cli/snapshots/ipynb_topdown.txt+++ b/tests/_cli/snapshots/ipynb_topdown.txt@@ -41,7 +41,22 @@]}],- "metadata": {},+ "metadata": {+ "language_info": {+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3"+ },+ "marimo": {+ "marimo_version": "0.0.0"+ }+ },"nbformat": 4,"nbformat_minor": 5}diff --git a/tests/_cli/snapshots/ipynb_with_errors.txt b/tests/_cli/snapshots/ipynb_with_errors.txtindex 3909c57aef..ecdce2eb69 100644--- a/tests/_cli/snapshots/ipynb_with_errors.txt+++ b/tests/_cli/snapshots/ipynb_with_errors.txt@@ -45,7 +45,22 @@]}],- "metadata": {},+ "metadata": {+ "language_info": {+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3"+ },+ "marimo": {+ "marimo_version": "0.0.0"+ }+ },"nbformat": 4,"nbformat_minor": 5}diff --git a/tests/_cli/snapshots/ipynb_with_outputs.txt b/tests/_cli/snapshots/ipynb_with_outputs.txtindex 72d1b8094f..6ea713a358 100644--- a/tests/_cli/snapshots/ipynb_with_outputs.txt+++ b/tests/_cli/snapshots/ipynb_with_outputs.txt@@ -61,7 +61,22 @@]}],- "metadata": {},+ "metadata": {+ "language_info": {+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3"+ },+ "marimo": {+ "marimo_version": "0.0.0"+ }+ },"nbformat": 4,"nbformat_minor": 5}diff --git a/tests/_convert/ipynb/snapshots/complex_file_format_top_down.ipynb.txt b/tests/_convert/ipynb/snapshots/complex_file_format_top_down.ipynb.txtindex 53ef791841..bd1863d2a3 100644--- a/tests/_convert/ipynb/snapshots/complex_file_format_top_down.ipynb.txt+++ b/tests/_convert/ipynb/snapshots/complex_file_format_top_down.ipynb.txt@@ -190,7 +190,30 @@]}],- "metadata": {},+ "metadata": {+ "language_info": {+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3"+ },+ "marimo": {+ "app_config": {+ "auto_download": [+ "html"+ ],+ "sql_output": "native",+ "width": "medium"+ },+ "header": "# /// script\n# description = \"Complex file format with setup cell\"\n# requires-python = \">=3.12\"\n# dependencies = [\n# \"marimo\",\n# \"pandas>=2.1.0\",\n# \"numpy>=2.1.0\",\n# ]\n# ///\n\n",+ "marimo_version": "0.0.0"+ }+ },"nbformat": 4,"nbformat_minor": 5}\ No newline at end of filediff --git a/tests/_convert/ipynb/snapshots/complex_outputs_top_down.ipynb.txt b/tests/_convert/ipynb/snapshots/complex_outputs_top_down.ipynb.txtindex 24624f7108..9f9fcbf7ca 100644--- a/tests/_convert/ipynb/snapshots/complex_outputs_top_down.ipynb.txt+++ b/tests/_convert/ipynb/snapshots/complex_outputs_top_down.ipynb.txt@@ -91,7 +91,22 @@]}],- "metadata": {},+ "metadata": {+ "language_info": {+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3"+ },+ "marimo": {+ "marimo_version": "0.0.0"+ }+ },"nbformat": 4,"nbformat_minor": 5}\ No newline at end of filediff --git a/tests/_convert/ipynb/snapshots/notebook_top_down.ipynb.txt b/tests/_convert/ipynb/snapshots/notebook_top_down.ipynb.txtindex 35f80093ef..8542c9ff67 100644--- a/tests/_convert/ipynb/snapshots/notebook_top_down.ipynb.txt+++ b/tests/_convert/ipynb/snapshots/notebook_top_down.ipynb.txt@@ -35,7 +35,22 @@]}],- "metadata": {},+ "metadata": {+ "language_info": {+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3"+ },+ "marimo": {+ "marimo_version": "0.0.0"+ }+ },"nbformat": 4,"nbformat_minor": 5}diff --git a/tests/_convert/ipynb/snapshots/notebook_topological.ipynb.txt b/tests/_convert/ipynb/snapshots/notebook_topological.ipynb.txtindex 4413bd7035..b9ed151432 100644--- a/tests/_convert/ipynb/snapshots/notebook_topological.ipynb.txt+++ b/tests/_convert/ipynb/snapshots/notebook_topological.ipynb.txt@@ -35,7 +35,22 @@]}],- "metadata": {},+ "metadata": {+ "language_info": {+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3"+ },+ "marimo": {+ "marimo_version": "0.0.0"+ }+ },"nbformat": 4,"nbformat_minor": 5}diff --git a/tests/_convert/ipynb/snapshots/simple_top_down.ipynb.txt b/tests/_convert/ipynb/snapshots/simple_top_down.ipynb.txtindex b1f5daf4cc..204df07fa8 100644--- a/tests/_convert/ipynb/snapshots/simple_top_down.ipynb.txt+++ b/tests/_convert/ipynb/snapshots/simple_top_down.ipynb.txt@@ -41,7 +41,22 @@]}],- "metadata": {},+ "metadata": {+ "language_info": {+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3"+ },+ "marimo": {+ "marimo_version": "0.0.0"+ }+ },"nbformat": 4,"nbformat_minor": 5}\ No newline at end of filediff --git a/tests/_server/export/snapshots/ipynb/basic.ipynb.txt b/tests/_server/export/snapshots/ipynb/basic.ipynb.txtindex 0f53ffd66c..75afe3a36d 100644--- a/tests/_server/export/snapshots/ipynb/basic.ipynb.txt+++ b/tests/_server/export/snapshots/ipynb/basic.ipynb.txt@@ -31,7 +31,22 @@]}],- "metadata": {},+ "metadata": {+ "language_info": {+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3"+ },+ "marimo": {+ "marimo_version": "0.0.0"+ }+ },"nbformat": 4,"nbformat_minor": 5}\ No newline at end of filediff --git a/tests/_server/export/snapshots/ipynb/empty_notebook.ipynb.txt b/tests/_server/export/snapshots/ipynb/empty_notebook.ipynb.txtindex 2ccb5c4d67..804a1f413f 100644--- a/tests/_server/export/snapshots/ipynb/empty_notebook.ipynb.txt+++ b/tests/_server/export/snapshots/ipynb/empty_notebook.ipynb.txt@@ -9,7 +9,22 @@"source": []}],- "metadata": {},+ "metadata": {+ "language_info": {+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3"+ },+ "marimo": {+ "marimo_version": "0.0.0"+ }+ },"nbformat": 4,"nbformat_minor": 5}\ No newline at end of filediff --git a/tests/_server/export/snapshots/ipynb/error_ancestor.ipynb.txt b/tests/_server/export/snapshots/ipynb/error_ancestor.ipynb.txtindex 4adc6e2e92..32f5877f16 100644--- a/tests/_server/export/snapshots/ipynb/error_ancestor.ipynb.txt+++ b/tests/_server/export/snapshots/ipynb/error_ancestor.ipynb.txt@@ -48,7 +48,22 @@]}],- "metadata": {},+ "metadata": {+ "language_info": {+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3"+ },+ "marimo": {+ "marimo_version": "0.0.0"+ }+ },"nbformat": 4,"nbformat_minor": 5}\ No newline at end of filediff --git a/tests/_server/export/snapshots/ipynb/error_complex.ipynb.txt b/tests/_server/export/snapshots/ipynb/error_complex.ipynb.txtindex bd43650ae7..6141bcbd38 100644--- a/tests/_server/export/snapshots/ipynb/error_complex.ipynb.txt+++ b/tests/_server/export/snapshots/ipynb/error_complex.ipynb.txt@@ -87,7 +87,22 @@]}],- "metadata": {},+ "metadata": {+ "language_info": {+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3"+ },+ "marimo": {+ "marimo_version": "0.0.0"+ }+ },"nbformat": 4,"nbformat_minor": 5}\ No newline at end of filediff --git a/tests/_server/export/snapshots/ipynb/error_cycle.ipynb.txt b/tests/_server/export/snapshots/ipynb/error_cycle.ipynb.txtindex f9f8c00a49..1cf3d53786 100644--- a/tests/_server/export/snapshots/ipynb/error_cycle.ipynb.txt+++ b/tests/_server/export/snapshots/ipynb/error_cycle.ipynb.txt@@ -35,7 +35,22 @@]}],- "metadata": {},+ "metadata": {+ "language_info": {+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3"+ },+ "marimo": {+ "marimo_version": "0.0.0"+ }+ },"nbformat": 4,"nbformat_minor": 5}\ No newline at end of filediff --git a/tests/_server/export/snapshots/ipynb/error_redefinition.ipynb.txt b/tests/_server/export/snapshots/ipynb/error_redefinition.ipynb.txtindex 8d8611bd28..edf899fb1c 100644--- a/tests/_server/export/snapshots/ipynb/error_redefinition.ipynb.txt+++ b/tests/_server/export/snapshots/ipynb/error_redefinition.ipynb.txt@@ -35,7 +35,22 @@]}],- "metadata": {},+ "metadata": {+ "language_info": {+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3"+ },+ "marimo": {+ "marimo_version": "0.0.0"+ }+ },"nbformat": 4,"nbformat_minor": 5}\ No newline at end of filediff --git a/tests/_server/export/snapshots/ipynb/error_undefined.ipynb.txt b/tests/_server/export/snapshots/ipynb/error_undefined.ipynb.txtindex 0bf81d71cf..84b43dde20 100644--- a/tests/_server/export/snapshots/ipynb/error_undefined.ipynb.txt+++ b/tests/_server/export/snapshots/ipynb/error_undefined.ipynb.txt@@ -25,7 +25,22 @@]}],- "metadata": {},+ "metadata": {+ "language_info": {+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3"+ },+ "marimo": {+ "marimo_version": "0.0.0"+ }+ },"nbformat": 4,"nbformat_minor": 5}\ No newline at end of filediff --git a/tests/_server/export/snapshots/ipynb/error_value_with_stdout.ipynb.txt b/tests/_server/export/snapshots/ipynb/error_value_with_stdout.ipynb.txtindex edd0996748..a596b1112b 100644--- a/tests/_server/export/snapshots/ipynb/error_value_with_stdout.ipynb.txt+++ b/tests/_server/export/snapshots/ipynb/error_value_with_stdout.ipynb.txt@@ -32,7 +32,22 @@]}],- "metadata": {},+ "metadata": {+ "language_info": {+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3"+ },+ "marimo": {+ "marimo_version": "0.0.0"+ }+ },"nbformat": 4,"nbformat_minor": 5}\ No newline at end of filediff --git a/tests/_server/export/snapshots/ipynb/with_console_output.ipynb.txt b/tests/_server/export/snapshots/ipynb/with_console_output.ipynb.txtindex 893f1bb418..03c3c54e47 100644--- a/tests/_server/export/snapshots/ipynb/with_console_output.ipynb.txt+++ b/tests/_server/export/snapshots/ipynb/with_console_output.ipynb.txt@@ -57,7 +57,22 @@]}],- "metadata": {},+ "metadata": {+ "language_info": {+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3"+ },+ "marimo": {+ "marimo_version": "0.0.0"+ }+ },"nbformat": 4,"nbformat_minor": 5}\ No newline at end of filediff --git a/tests/_server/export/snapshots/ipynb/with_dependencies.ipynb.txt b/tests/_server/export/snapshots/ipynb/with_dependencies.ipynb.txtindex 749efbc9d3..eeef5ece96 100644--- a/tests/_server/export/snapshots/ipynb/with_dependencies.ipynb.txt+++ b/tests/_server/export/snapshots/ipynb/with_dependencies.ipynb.txt@@ -31,7 +31,22 @@]}],- "metadata": {},+ "metadata": {+ "language_info": {+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3"+ },+ "marimo": {+ "marimo_version": "0.0.0"+ }+ },"nbformat": 4,"nbformat_minor": 5}\ No newline at end of filediff --git a/tests/_server/export/snapshots/ipynb/with_layout.ipynb.txt b/tests/_server/export/snapshots/ipynb/with_layout.ipynb.txtindex 5bc7bac4c2..5588c20b92 100644--- a/tests/_server/export/snapshots/ipynb/with_layout.ipynb.txt+++ b/tests/_server/export/snapshots/ipynb/with_layout.ipynb.txt@@ -11,7 +11,25 @@]}],- "metadata": {},+ "metadata": {+ "language_info": {+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3"+ },+ "marimo": {+ "app_config": {+ "layout_file": "layouts/layout.json"+ },+ "marimo_version": "0.0.0"+ }+ },"nbformat": 4,"nbformat_minor": 5}\ No newline at end of filediff --git a/tests/_server/export/snapshots/ipynb/with_stop.ipynb.txt b/tests/_server/export/snapshots/ipynb/with_stop.ipynb.txtindex 682c7d37cf..3a7b146d1e 100644--- a/tests/_server/export/snapshots/ipynb/with_stop.ipynb.txt+++ b/tests/_server/export/snapshots/ipynb/with_stop.ipynb.txt@@ -43,7 +43,22 @@]}],- "metadata": {},+ "metadata": {+ "language_info": {+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3"+ },+ "marimo": {+ "marimo_version": "0.0.0"+ }+ },"nbformat": 4,"nbformat_minor": 5}\ No newline at end of file