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.

refactor: move files and tests around to cleanup `marimo._convert` (#7858)

Myles Scolnick5edf295b5991
diff --git a/development_docs/testing.md b/development_docs/testing.md
new file mode 100644
index 0000000000..639506a609
--- /dev/null
+++ b/development_docs/testing.md
@@ -0,0 +1,42 @@
+# Testing
+
+## Directory Structure
+
+- **Snapshots**: Store in `snapshots/` directory next to your test file
+- **Fixtures**: Store test input files in `fixtures/` directory next to your test file
+
+```
+tests/_convert/ipynb/
+├── test_ipynb_converter.py
+├── snapshots/ # Expected outputs
+└── fixtures/ # Test inputs
+```
+
+## Snapshot Testing
+
+```python
+from tests.mocks import snapshotter
+
+snapshot = snapshotter(__file__)
+snapshot("output.py.txt", result) # Auto-creates/compares snapshot
+```
+
+## Kernel Fixtures
+
+Available in `tests/conftest.py`:
+
+- `k` - Default kernel (autorun, relaxed)
+- `strict_kernel` - Strict execution mode
+- `lazy_kernel` - Lazy execution mode
+- `run_mode_kernel` - RUN mode (not EDIT)
+- `mocked_kernel` - Full MockedKernel wrapper
+- `executing_kernel` - Execution context installed
+- `any_kernel` - Parametrized: runs test 3x (k, strict, lazy)
+- `execution_kernel` - Parametrized: runs test 2x (k, strict)
+
+## Running Tests
+
+```bash
+uvx hatch run +py=3.12 test:test tests/path/to/test.py
+uvx hatch run +py=3.12 test-optional:test tests/path/to/test.py # with optional deps
+```
diff --git a/marimo/_ast/load.py b/marimo/_ast/load.py
index 4f2e6c915e..8d78143537 100644
--- a/marimo/_ast/load.py
+++ b/marimo/_ast/load.py
@@ -158,7 +158,7 @@ def get_notebook_status(filename: str) -> LoadResult:
notebook: Optional[NotebookSerialization] = None
if path.suffix in (".md", ".qmd"):
- from marimo._convert.markdown.markdown import (
+ from marimo._convert.markdown.to_ir import (
convert_from_md_to_marimo_ir,
)
@@ -219,7 +219,7 @@ def load_app(filename: Optional[str | Path]) -> Optional[App]:
contents = _maybe_contents(filename)
if not contents:
return None
- from marimo._convert.markdown.markdown import convert_from_md_to_app
+ from marimo._convert.markdown.to_ir import convert_from_md_to_app
return convert_from_md_to_app(contents) if contents else None
elif not path.suffix == ".py":
diff --git a/marimo/_convert/__init__.py b/marimo/_convert/__init__.py
new file mode 100644
index 0000000000..eb04c8df90
--- /dev/null
+++ b/marimo/_convert/__init__.py
@@ -0,0 +1,24 @@
+# Copyright 2026 Marimo. All rights reserved.
+"""
+marimo._convert - Format conversion utilities for marimo notebooks.
+
+Provides bidirectional conversion between marimo's internal representation
+and various notebook/script formats.
+"""
+
+from __future__ import annotations
+
+# Only import the main converters at module level to avoid circular imports.
+# Other conversion functions should be imported directly from their submodules:
+# from marimo._convert.markdown import convert_from_ir_to_markdown
+# from marimo._convert.ipynb import convert_from_ipynb_to_notebook_ir
+# etc.
+from marimo._convert.converters import (
+ MarimoConvert,
+ MarimoConverterIntermediate,
+)
+
+__all__ = [
+ "MarimoConvert",
+ "MarimoConverterIntermediate",
+]
diff --git a/marimo/_convert/common/__init__.py b/marimo/_convert/common/__init__.py
new file mode 100644
index 0000000000..d50666223b
--- /dev/null
+++ b/marimo/_convert/common/__init__.py
@@ -0,0 +1,37 @@
+# Copyright 2026 Marimo. All rights reserved.
+from __future__ import annotations
+
+from marimo._convert.common.comment_preserver import (
+ CommentPreserver,
+ CommentToken,
+)
+from marimo._convert.common.dom_traversal import (
+ replace_html_attributes,
+ replace_virtual_files_with_data_uris,
+)
+from marimo._convert.common.filename import (
+ get_download_filename,
+ get_filename,
+ make_download_headers,
+)
+from marimo._convert.common.format import (
+ get_markdown_from_cell,
+ markdown_to_marimo,
+ sql_to_marimo,
+)
+
+__all__ = [
+ # utils
+ "get_download_filename",
+ "get_filename",
+ "get_markdown_from_cell",
+ "make_download_headers",
+ "markdown_to_marimo",
+ "sql_to_marimo",
+ # comment_preserver
+ "CommentPreserver",
+ "CommentToken",
+ # dom_traversal
+ "replace_html_attributes",
+ "replace_virtual_files_with_data_uris",
+]
diff --git a/marimo/_convert/comment_preserver.py b/marimo/_convert/common/comment_preserver.py
similarity index 100%
rename from marimo/_convert/comment_preserver.py
rename to marimo/_convert/common/comment_preserver.py
diff --git a/marimo/_server/export/dom_traversal.py b/marimo/_convert/common/dom_traversal.py
similarity index 100%
rename from marimo/_server/export/dom_traversal.py
rename to marimo/_convert/common/dom_traversal.py
diff --git a/marimo/_server/export/utils.py b/marimo/_convert/common/filename.py
similarity index 100%
rename from marimo/_server/export/utils.py
rename to marimo/_convert/common/filename.py
diff --git a/marimo/_convert/utils.py b/marimo/_convert/common/format.py
similarity index 100%
rename from marimo/_convert/utils.py
rename to marimo/_convert/common/format.py
diff --git a/marimo/_convert/converters.py b/marimo/_convert/converters.py
index 40e1fb573b..fae12fe94b 100644
--- a/marimo/_convert/converters.py
+++ b/marimo/_convert/converters.py
@@ -106,7 +106,7 @@ class MarimoConvert:
Args:
source: Markdown source code string
"""
- from marimo._convert.markdown.markdown import (
+ from marimo._convert.markdown.to_ir import (
convert_from_md_to_marimo_ir,
)
@@ -119,7 +119,9 @@ class MarimoConvert:
Args:
source: Jupyter notebook JSON string
"""
- from marimo._convert.ipynb import convert_from_ipynb_to_notebook_ir
+ from marimo._convert.ipynb.to_ir import (
+ convert_from_ipynb_to_notebook_ir,
+ )
return MarimoConvert.from_ir(convert_from_ipynb_to_notebook_ir(source))
diff --git a/marimo/_convert/ipynb/__init__.py b/marimo/_convert/ipynb/__init__.py
new file mode 100644
index 0000000000..4cc7b6fd0f
--- /dev/null
+++ b/marimo/_convert/ipynb/__init__.py
@@ -0,0 +1,26 @@
+# Copyright 2026 Marimo. All rights reserved.
+"""Jupyter notebook (.ipynb) conversion utilities for marimo notebooks.
+
+This module provides bidirectional conversion between ipynb and marimo IR:
+- `to_ir`: Parse Jupyter notebooks into marimo notebook IR
+- `from_ir`: Export marimo notebook IR to Jupyter notebook format
+"""
+
+from __future__ import annotations
+
+from marimo._convert.ipynb.from_ir import convert_from_ir_to_ipynb
+from marimo._convert.ipynb.to_ir import (
+ CodeCell,
+ ExclamationMarkResult,
+ convert_from_ipynb_to_notebook_ir,
+)
+
+__all__ = [
+ # Export (IR → ipynb)
+ "convert_from_ir_to_ipynb",
+ # Import (ipynb → IR)
+ "convert_from_ipynb_to_notebook_ir",
+ # Types
+ "CodeCell",
+ "ExclamationMarkResult",
+]
diff --git a/marimo/_convert/ipynb/from_ir.py b/marimo/_convert/ipynb/from_ir.py
new file mode 100644
index 0000000000..bf8715510a
--- /dev/null
+++ b/marimo/_convert/ipynb/from_ir.py
@@ -0,0 +1,338 @@
+# Copyright 2026 Marimo. All rights reserved.
+"""Export marimo notebooks to Jupyter ipynb format."""
+
+from __future__ import annotations
+
+import io
+import json
+import re
+from html.parser import HTMLParser
+from typing import TYPE_CHECKING, Any, Literal, Optional, Union, cast
+
+from marimo._ast.cell import Cell, CellConfig
+from marimo._ast.errors import CycleError, MultipleDefinitionError
+from marimo._ast.names import is_internal_cell_name
+from marimo._convert.common.format import get_markdown_from_cell
+from marimo._dependencies.dependencies import DependencyManager
+from marimo._messaging.cell_output import CellChannel, CellOutput
+from marimo._messaging.errors import (
+ Error as MarimoError,
+ MarimoExceptionRaisedError,
+)
+from marimo._messaging.mimetypes import METADATA_KEY
+from marimo._runtime import dataflow
+
+if TYPE_CHECKING:
+ from nbformat.notebooknode import NotebookNode # type: ignore
+
+ from marimo._ast.app import InternalApp
+ from marimo._session.state.session_view import SessionView
+
+
+def convert_from_ir_to_ipynb(
+ app: InternalApp,
+ *,
+ sort_mode: Literal["top-down", "topological"],
+ session_view: Optional[SessionView] = None,
+) -> str:
+ """Export notebook as .ipynb, optionally including outputs.
+
+ Args:
+ app: The internal app to export
+ sort_mode: How to order cells - "top-down" preserves notebook order,
+ "topological" orders by dependencies
+ session_view: Optional session view to include cell outputs
+
+ Returns:
+ JSON string of the .ipynb notebook
+ """
+ DependencyManager.nbformat.require("to convert marimo notebooks to ipynb")
+ import nbformat # type: ignore[import-not-found]
+
+ notebook = nbformat.v4.new_notebook() # type: ignore[no-untyped-call]
+ notebook["cells"] = []
+
+ # Determine cell order based on sort_mode
+ if sort_mode == "top-down":
+ cell_data_list = list(app.cell_manager.cell_data())
+ else:
+ # Topological sort - try to sort, fall back to top-down on cycle
+ try:
+ graph = app.graph
+ sorted_ids = dataflow.topological_sort(graph, graph.cells.keys())
+ # Build cell_data list in topological order
+ cell_data_list = [
+ app.cell_manager.cell_data_at(cid)
+ for cid in sorted_ids
+ if cid in graph.cells
+ ]
+ except (CycleError, MultipleDefinitionError):
+ # Fall back to top-down order if graph is invalid
+ cell_data_list = list(app.cell_manager.cell_data())
+
+ for cell_data in cell_data_list:
+ cid = cell_data.cell_id
+
+ # Get outputs if session_view is provided
+ outputs: list[NotebookNode] = []
+ if session_view is not None:
+ cell_output = session_view.get_cell_outputs([cid]).get(cid, None)
+ cell_console_outputs = session_view.get_cell_console_outputs(
+ [cid]
+ ).get(cid, [])
+ outputs = _convert_marimo_output_to_ipynb(
+ cell_output, cell_console_outputs
+ )
+
+ notebook_cell = _create_ipynb_cell(
+ cell_id=cid,
+ code=cell_data.code,
+ name=cell_data.name,
+ config=cell_data.config,
+ cell=cell_data.cell,
+ outputs=outputs,
+ )
+ notebook["cells"].append(notebook_cell)
+
+ stream = io.StringIO()
+ nbformat.write(notebook, stream) # type: ignore[no-untyped-call]
+ stream.seek(0)
+ return stream.read()
+
+
+def _create_ipynb_cell(
+ cell_id: str,
+ code: str,
+ name: str,
+ config: CellConfig,
+ cell: Optional[Cell],
+ outputs: list[NotebookNode],
+) -> NotebookNode:
+ """Create an ipynb cell with metadata.
+
+ Args:
+ cell_id: The cell's unique identifier
+ code: The cell's source code
+ name: The cell's name
+ config: The cell's configuration
+ cell: Optional Cell object for markdown detection
+ outputs: List of cell outputs (ignored for markdown cells)
+ """
+ import nbformat
+
+ # Try to extract markdown if we have a valid Cell
+ if cell is not None:
+ markdown_string = get_markdown_from_cell(cell, code)
+ if markdown_string is not None:
+ node = cast(
+ nbformat.NotebookNode,
+ nbformat.v4.new_markdown_cell(markdown_string, id=cell_id), # type: ignore[no-untyped-call]
+ )
+ _add_marimo_metadata(node, name, config)
+ return node
+
+ node = cast(
+ nbformat.NotebookNode,
+ nbformat.v4.new_code_cell(code, id=cell_id), # type: ignore[no-untyped-call]
+ )
+ if outputs:
+ node.outputs = outputs
+ _add_marimo_metadata(node, name, config)
+ return node
+
+
+def _add_marimo_metadata(
+ node: NotebookNode, name: str, config: CellConfig
+) -> None:
+ """Add marimo-specific metadata to a notebook cell."""
+ marimo_metadata: dict[str, Any] = {}
+ if config.is_different_from_default():
+ marimo_metadata["config"] = config.asdict_without_defaults()
+ if not is_internal_cell_name(name):
+ marimo_metadata["name"] = name
+ if marimo_metadata:
+ node["metadata"]["marimo"] = marimo_metadata
+
+
+# Output conversion helpers
+
+
+def _maybe_extract_dataurl(data: Any) -> Any:
+ if (
+ isinstance(data, str)
+ and data.startswith("data:")
+ and ";base64," in data
+ ):
+ return data.split(";base64,")[1]
+ else:
+ return data
+
+
+class _HTMLTextExtractor(HTMLParser):
+ """Extract plain text from HTML."""
+
+ def __init__(self) -> None:
+ super().__init__()
+ self.text_parts: list[str] = []
+
+ def handle_data(self, data: str) -> None:
+ self.text_parts.append(data)
+
+ def get_text(self) -> str:
+ return "".join(self.text_parts)
+
+
+def _strip_html_from_traceback(html_traceback: str) -> list[str]:
+ """Convert HTML-formatted traceback to plain text lines.
+
+ Also strips temporary file paths from tracebacks. Exports run cells in
+ the kernel which compiles them with temp file paths. It's not easy to
+ set anonymous source since exports are done in the kernel, so we strip
+ the paths here instead.
+ """
+ parser = _HTMLTextExtractor()
+ parser.feed(html_traceback)
+ text = parser.get_text()
+
+ # Strip temp file paths like /tmp/marimo_12345/__marimo__cell_Hbol_.py
+ # Replace with empty string to get cleaner tracebacks
+ text = re.sub(r'[^"]*__marimo__cell_[^"]*\.py', "", text)
+
+ return text.splitlines()
+
+
+def _extract_traceback_from_console(
+ console_outputs: list[CellOutput],
+) -> list[str]:
+ """Extract traceback lines from console outputs."""
+ for output in console_outputs:
+ if (
+ output.channel == CellChannel.STDERR
+ and output.mimetype == "application/vnd.marimo+traceback"
+ ):
+ return _strip_html_from_traceback(str(output.data))
+ return []
+
+
+def _get_error_info(
+ error: Union[MarimoError, dict[str, Any]],
+) -> tuple[str, str]:
+ """Extract ename and evalue from a marimo error."""
+ from marimo._messaging.msgspec_encoder import asdict
+
+ if isinstance(error, dict):
+ return error.get("type", "UnknownError"), error.get("msg", "")
+ elif isinstance(error, MarimoExceptionRaisedError):
+ return error.exception_type, error.msg.rstrip().strip(":")
+ else:
+ # For other error types, use the tag as ename and describe() as evalue
+ error_dict = asdict(error)
+ return error_dict.get("type", "Error"), error.describe()
+
+
+def _convert_marimo_output_to_ipynb(
+ cell_output: Optional[CellOutput], console_outputs: list[CellOutput]
+) -> list[NotebookNode]:
+ """Convert marimo output format to IPython notebook format."""
+ import nbformat
+
+ ipynb_outputs: list[NotebookNode] = []
+
+ # Handle stdout/stderr
+ for console_out in console_outputs:
+ if console_out.channel == CellChannel.STDOUT:
+ ipynb_outputs.append(
+ cast(
+ nbformat.NotebookNode,
+ nbformat.v4.new_output( # type: ignore[no-untyped-call]
+ "stream",
+ name="stdout",
+ text=console_out.data,
+ ),
+ )
+ )
+ elif console_out.channel == CellChannel.STDERR:
+ # Skip tracebacks - they're included in error outputs
+ if console_out.mimetype == "application/vnd.marimo+traceback":
+ continue
+ ipynb_outputs.append(
+ cast(
+ nbformat.NotebookNode,
+ nbformat.v4.new_output( # type: ignore[no-untyped-call]
+ "stream",
+ name="stderr",
+ text=console_out.data,
+ ),
+ )
+ )
+
+ if not cell_output:
+ return ipynb_outputs
+
+ if cell_output.data is None:
+ return ipynb_outputs
+
+ if cell_output.channel == CellChannel.MARIMO_ERROR:
+ traceback_lines = _extract_traceback_from_console(console_outputs)
+ errors = cast(
+ list[Union[MarimoError, dict[str, Any]]], cell_output.data
+ )
+ for error in errors:
+ ename, evalue = _get_error_info(error)
+ ipynb_outputs.append(
+ cast(
+ nbformat.NotebookNode,
+ nbformat.v4.new_output( # type: ignore[no-untyped-call]
+ "error",
+ ename=ename,
+ evalue=evalue,
+ traceback=traceback_lines,
+ ),
+ )
+ )
+ return ipynb_outputs
+
+ if cell_output.channel not in (CellChannel.OUTPUT, CellChannel.MEDIA):
+ return ipynb_outputs
+
+ if cell_output.mimetype == "text/plain" and (
+ cell_output.data == [] or cell_output.data == ""
+ ):
+ return ipynb_outputs
+
+ # Handle rich output
+ data: dict[str, Any] = {}
+ metadata: dict[str, Any] = {}
+
+ if cell_output.mimetype == "application/vnd.marimo+error":
+ # Already handled above via MARIMO_ERROR channel
+ return ipynb_outputs
+ elif cell_output.mimetype == "application/vnd.marimo+mimebundle":
+ if isinstance(cell_output.data, dict):
+ mimebundle = cell_output.data
+ elif isinstance(cell_output.data, str):
+ mimebundle = json.loads(cell_output.data)
+ else:
+ raise ValueError(f"Invalid data type: {type(cell_output.data)}")
+
+ for mime, content in mimebundle.items():
+ if mime == METADATA_KEY and isinstance(content, dict):
+ metadata = content
+ else:
+ data[mime] = _maybe_extract_dataurl(content)
+ else:
+ data[cell_output.mimetype] = _maybe_extract_dataurl(cell_output.data)
+
+ if data:
+ ipynb_outputs.append(
+ cast(
+ nbformat.NotebookNode,
+ nbformat.v4.new_output( # type: ignore[no-untyped-call]
+ "display_data",
+ data=data,
+ metadata=metadata,
+ ),
+ )
+ )
+
+ return ipynb_outputs
diff --git a/marimo/_convert/ipynb.py b/marimo/_convert/ipynb/to_ir.py
similarity index 99%
rename from marimo/_convert/ipynb.py
rename to marimo/_convert/ipynb/to_ir.py
index db59ed0656..8f314ac12d 100644
--- a/marimo/_convert/ipynb.py
+++ b/marimo/_convert/ipynb/to_ir.py
@@ -14,7 +14,7 @@ from marimo._ast.compiler import compile_cell
from marimo._ast.transformers import NameTransformer, RemoveImportTransformer
from marimo._ast.variables import is_local
from marimo._ast.visitor import Block, NamedNode, ScopedVisitor
-from marimo._convert.utils import markdown_to_marimo
+from marimo._convert.common.format import markdown_to_marimo
from marimo._runtime.dataflow import DirectedGraph
from marimo._schemas.serialization import (
AppInstantiation,
@@ -983,7 +983,7 @@ def _transform_sources(
A tuple of (cells, exclamation_metadata) where exclamation_metadata
contains pip packages and subprocess import information.
"""
- from marimo._convert.comment_preserver import CommentPreserver
+ from marimo._convert.common.comment_preserver import CommentPreserver
# Define transforms that don't need comment preservation
simple_transforms = [
diff --git a/marimo/_convert/markdown/__init__.py b/marimo/_convert/markdown/__init__.py
index 236d950d01..d45cad0294 100644
--- a/marimo/_convert/markdown/__init__.py
+++ b/marimo/_convert/markdown/__init__.py
@@ -1,204 +1,32 @@
# Copyright 2026 Marimo. All rights reserved.
-from __future__ import annotations
-
-import ast
-import os
-import re
-import textwrap
-from typing import TYPE_CHECKING
-
-from marimo import _loggers
-from marimo._ast import codegen
-from marimo._ast.compiler import const_or_id
-from marimo._ast.names import is_internal_cell_name
-from marimo._convert.utils import get_markdown_from_cell
-from marimo._schemas.serialization import NotebookSerializationV1
-from marimo._types.ids import CellId_t
-from marimo._version import __version__
-
-if TYPE_CHECKING:
- from marimo._ast.cell import CellImpl
- from marimo._ast.visitor import Language
-
-LOGGER = _loggers.marimo_logger()
-
-
-def convert_from_ir_to_markdown(
- notebook: NotebookSerializationV1,
- filename: str | None = None,
-) -> str:
- from marimo._ast.app_config import _AppConfig
- from marimo._ast.compiler import compile_cell
- from marimo._convert.markdown.markdown import (
- formatted_code_block,
- is_sanitized_markdown,
- )
- from marimo._utils import yaml
-
- filename = filename or notebook.filename or "notebook.md"
- app_title = notebook.app.options.get("app_title", None)
- if not app_title:
- app_title = _format_filename_title(filename)
-
- metadata: dict[str, str | list[str]] = {}
- metadata.update(
- {
- "title": app_title,
- "marimo-version": __version__,
- }
- )
-
- # Put data from AppFileManager into the yaml header.
- ignored_keys = {"app_title"}
- default_config = _AppConfig().asdict()
-
- # Get values defined in _AppConfig without explicitly extracting keys,
- # as long as it isn't the default.
- metadata.update(
- {
- k: v
- for k, v in notebook.app.options.items()
- if k not in ignored_keys and v != default_config.get(k)
- }
- )
-
- # Add header from notebook if present
- if notebook.header and notebook.header.value:
- metadata["header"] = notebook.header.value.strip()
-
- # Add the expected qmd filter to the metadata.
- is_qmd = filename.endswith(".qmd")
- if is_qmd:
- if "filters" not in metadata:
- metadata["filters"] = []
- if "marimo" not in str(metadata["filters"]):
- if isinstance(metadata["filters"], str):
- metadata["filters"] = metadata["filters"].split(",")
- if isinstance(metadata["filters"], list):
- metadata["filters"].append("marimo-team/marimo")
- else:
- LOGGER.warning(
- "Unexpected type for filters: %s",
- type(metadata["filters"]),
- )
-
- header = yaml.marimo_compat_dump(
- {
- k: v
- for k, v in metadata.items()
- if v is not None and v != "" and v != []
- },
- sort_keys=False,
- )
- document = ["---", header.strip(), "---", ""]
- previous_was_markdown = False
+"""Markdown conversion utilities for marimo notebooks.
- for cell in notebook.cells:
- code = cell.code
- # Config values are opt in, so only include if they are set.
- attributes = cell.options.copy()
+This module provides bidirectional conversion between markdown and marimo IR:
+- `to_ir`: Parse markdown (.md, .qmd) files into marimo notebook IR
+- `from_ir`: Export marimo notebook IR to markdown format
+"""
- # Extract name from options if present (for unparsable cells)
- # and use it instead of cell.name
- cell_name = attributes.pop("name", None) or cell.name
-
- # Allow for attributes like column index.
- attributes = {k: repr(v).lower() for k, v in attributes.items() if v}
- if not is_internal_cell_name(cell_name):
- attributes["name"] = cell_name
-
- # No "cell" typically means not parseable. However newly added
- # cells require compilation before cell is set.
- # TODO: Refactor so it doesn't occur in export (codegen
- # does this too)
- # NB. Also need to recompile in the sql case since sql parsing is
- # cached.
- language: Language = "python"
- cell_impl: CellImpl | None = None
- try:
- cell_impl = compile_cell(code, cell_id=CellId_t("dummy"))
- language = cell_impl.language
- except SyntaxError:
- pass
-
- if cell_impl:
- # Markdown that starts a column is forced to code.
- column = attributes.get("column", None)
- if not column or column == "0":
- markdown = get_markdown_from_cell(cell_impl, code)
- # Unsanitized markdown is forced to code.
- if markdown and is_sanitized_markdown(markdown):
- # Use blank HTML comment to separate markdown codeblocks
- if previous_was_markdown:
- document.append("<!---->")
- previous_was_markdown = True
- document.append(markdown)
- continue
- # In which case we need to format it like our python blocks.
- elif cell_impl.markdown:
- code = codegen.format_markdown(cell_impl)
-
- attributes["language"] = language
- # Definitely a code cell, but need to determine if it can be
- # formatted as non-python.
- if attributes["language"] == "sql":
- sql_options: dict[str, str] | None = (
- _get_sql_options_from_cell(code)
- )
- if not sql_options:
- # means not sql.
- attributes.pop("language")
- else:
- # Ignore default query value.
- if sql_options.get("query") == "_df":
- sql_options.pop("query")
- attributes.update(sql_options)
- code = "\n".join(cell_impl.raw_sqls).strip()
-
- # Definitely no "cell"; as such, treat as code, as everything in
- # marimo is code.
- else:
- attributes["unparsable"] = "true"
-
- # Dedent and strip code to prevent whitespace accumulation on roundtrips
- code = textwrap.dedent(code).strip()
-
- # Add a blank line between markdown and code
- if previous_was_markdown:
- document.append("")
- previous_was_markdown = False
- document.append(formatted_code_block(code, attributes, is_qmd=is_qmd))
-
- return "\n".join(document).strip()
-
-
-def _format_filename_title(filename: str) -> str:
- basename = os.path.basename(filename)
- name, _ext = os.path.splitext(basename)
- title = re.sub("[-_]", " ", name)
- return title.title()
-
-
-def _get_sql_options_from_cell(code: str) -> dict[str, str] | None:
- # Note frontend/src/core/codemirror/language/sql.ts
- # also extracts options via ast. Ideally, these should be synced.
- options = {}
- code = code.strip()
- try:
- (body,) = ast.parse(code).body
- (target,) = body.targets # type: ignore[attr-defined]
- options["query"] = target.id
- if body.value.func.attr == "sql": # type: ignore[attr-defined]
- value = body.value # type: ignore[attr-defined]
- else:
- return None
- if value.keywords:
- for keyword in value.keywords: # type: ignore[attr-defined]
- options[keyword.arg] = const_or_id(keyword.value) # type: ignore[attr-defined]
- output = options.pop("output", "True").lower()
- if output == "false":
- options["hide_output"] = "True"
+from __future__ import annotations
- return options
- except (AssertionError, AttributeError, ValueError):
- return None
+from marimo._convert.markdown.from_ir import convert_from_ir_to_markdown
+from marimo._convert.markdown.to_ir import (
+ convert_from_md_to_app,
+ convert_from_md_to_marimo_ir,
+ extract_frontmatter,
+ formatted_code_block,
+ is_sanitized_markdown,
+ sanitize_markdown,
+)
+
+__all__ = [
+ # Export (IR → Markdown)
+ "convert_from_ir_to_markdown",
+ # Import (Markdown → IR/App)
+ "convert_from_md_to_app",
+ "convert_from_md_to_marimo_ir",
+ # Utilities
+ "extract_frontmatter",
+ "formatted_code_block",
+ "is_sanitized_markdown",
+ "sanitize_markdown",
+]
diff --git a/marimo/_convert/markdown/from_ir.py b/marimo/_convert/markdown/from_ir.py
new file mode 100644
index 0000000000..81547dfdfd
--- /dev/null
+++ b/marimo/_convert/markdown/from_ir.py
@@ -0,0 +1,204 @@
+# Copyright 2026 Marimo. All rights reserved.
+from __future__ import annotations
+
+import ast
+import os
+import re
+import textwrap
+from typing import TYPE_CHECKING
+
+from marimo import _loggers
+from marimo._ast import codegen
+from marimo._ast.compiler import const_or_id
+from marimo._ast.names import is_internal_cell_name
+from marimo._convert.common.format import get_markdown_from_cell
+from marimo._schemas.serialization import NotebookSerializationV1
+from marimo._types.ids import CellId_t
+from marimo._version import __version__
+
+if TYPE_CHECKING:
+ from marimo._ast.cell import CellImpl
+ from marimo._ast.visitor import Language
+
+LOGGER = _loggers.marimo_logger()
+
+
+def convert_from_ir_to_markdown(
+ notebook: NotebookSerializationV1,
+ filename: str | None = None,
+) -> str:
+ from marimo._ast.app_config import _AppConfig
+ from marimo._ast.compiler import compile_cell
+ from marimo._convert.markdown.to_ir import (
+ formatted_code_block,
+ is_sanitized_markdown,
+ )
+ from marimo._utils import yaml
+
+ filename = filename or notebook.filename or "notebook.md"
+ app_title = notebook.app.options.get("app_title", None)
+ if not app_title:
+ app_title = _format_filename_title(filename)
+
+ metadata: dict[str, str | list[str]] = {}
+ metadata.update(
+ {
+ "title": app_title,
+ "marimo-version": __version__,
+ }
+ )
+
+ # Put data from AppFileManager into the yaml header.
+ ignored_keys = {"app_title"}
+ default_config = _AppConfig().asdict()
+
+ # Get values defined in _AppConfig without explicitly extracting keys,
+ # as long as it isn't the default.
+ metadata.update(
+ {
+ k: v
+ for k, v in notebook.app.options.items()
+ if k not in ignored_keys and v != default_config.get(k)
+ }
+ )
+
+ # Add header from notebook if present
+ if notebook.header and notebook.header.value:
+ metadata["header"] = notebook.header.value.strip()
+
+ # Add the expected qmd filter to the metadata.
+ is_qmd = filename.endswith(".qmd")
+ if is_qmd:
+ if "filters" not in metadata:
+ metadata["filters"] = []
+ if "marimo" not in str(metadata["filters"]):
+ if isinstance(metadata["filters"], str):
+ metadata["filters"] = metadata["filters"].split(",")
+ if isinstance(metadata["filters"], list):
+ metadata["filters"].append("marimo-team/marimo")
+ else:
+ LOGGER.warning(
+ "Unexpected type for filters: %s",
+ type(metadata["filters"]),
+ )
+
+ header = yaml.marimo_compat_dump(
+ {
+ k: v
+ for k, v in metadata.items()
+ if v is not None and v != "" and v != []
+ },
+ sort_keys=False,
+ )
+ document = ["---", header.strip(), "---", ""]
+ previous_was_markdown = False
+
+ for cell in notebook.cells:
+ code = cell.code
+ # Config values are opt in, so only include if they are set.
+ attributes = cell.options.copy()
+
+ # Extract name from options if present (for unparsable cells)
+ # and use it instead of cell.name
+ cell_name = attributes.pop("name", None) or cell.name
+
+ # Allow for attributes like column index.
+ attributes = {k: repr(v).lower() for k, v in attributes.items() if v}
+ if not is_internal_cell_name(cell_name):
+ attributes["name"] = cell_name
+
+ # No "cell" typically means not parseable. However newly added
+ # cells require compilation before cell is set.
+ # TODO: Refactor so it doesn't occur in export (codegen
+ # does this too)
+ # NB. Also need to recompile in the sql case since sql parsing is
+ # cached.
+ language: Language = "python"
+ cell_impl: CellImpl | None = None
+ try:
+ cell_impl = compile_cell(code, cell_id=CellId_t("dummy"))
+ language = cell_impl.language
+ except SyntaxError:
+ pass
+
+ if cell_impl:
+ # Markdown that starts a column is forced to code.
+ column = attributes.get("column", None)
+ if not column or column == "0":
+ markdown = get_markdown_from_cell(cell_impl, code)
+ # Unsanitized markdown is forced to code.
+ if markdown and is_sanitized_markdown(markdown):
+ # Use blank HTML comment to separate markdown codeblocks
+ if previous_was_markdown:
+ document.append("<!---->")
+ previous_was_markdown = True
+ document.append(markdown)
+ continue
+ # In which case we need to format it like our python blocks.
+ elif cell_impl.markdown:
+ code = codegen.format_markdown(cell_impl)
+
+ attributes["language"] = language
+ # Definitely a code cell, but need to determine if it can be
+ # formatted as non-python.
+ if attributes["language"] == "sql":
+ sql_options: dict[str, str] | None = (
+ _get_sql_options_from_cell(code)
+ )
+ if not sql_options:
+ # means not sql.
+ attributes.pop("language")
+ else:
+ # Ignore default query value.
+ if sql_options.get("query") == "_df":
+ sql_options.pop("query")
+ attributes.update(sql_options)
+ code = "\n".join(cell_impl.raw_sqls).strip()
+
+ # Definitely no "cell"; as such, treat as code, as everything in
+ # marimo is code.
+ else:
+ attributes["unparsable"] = "true"
+
+ # Dedent and strip code to prevent whitespace accumulation on roundtrips
+ code = textwrap.dedent(code).strip()
+
+ # Add a blank line between markdown and code
+ if previous_was_markdown:
+ document.append("")
+ previous_was_markdown = False
+ document.append(formatted_code_block(code, attributes, is_qmd=is_qmd))
+
+ return "\n".join(document).strip()
+
+
+def _format_filename_title(filename: str) -> str:
+ basename = os.path.basename(filename)
+ name, _ext = os.path.splitext(basename)
+ title = re.sub("[-_]", " ", name)
+ return title.title()
+
+
+def _get_sql_options_from_cell(code: str) -> dict[str, str] | None:
+ # Note frontend/src/core/codemirror/language/sql.ts
+ # also extracts options via ast. Ideally, these should be synced.
+ options = {}
+ code = code.strip()
+ try:
+ (body,) = ast.parse(code).body
+ (target,) = body.targets # type: ignore[attr-defined]
+ options["query"] = target.id
+ if body.value.func.attr == "sql": # type: ignore[attr-defined]
+ value = body.value # type: ignore[attr-defined]
+ else:
+ return None
+ if value.keywords:
+ for keyword in value.keywords: # type: ignore[attr-defined]
+ options[keyword.arg] = const_or_id(keyword.value) # type: ignore[attr-defined]
+ output = options.pop("output", "True").lower()
+ if output == "false":
+ options["hide_output"] = "True"
+
+ return options
+ except (AssertionError, AttributeError, ValueError):
+ return None
diff --git a/marimo/_convert/markdown/markdown.py b/marimo/_convert/markdown/to_ir.py
similarity index 99%
rename from marimo/_convert/markdown/markdown.py
rename to marimo/_convert/markdown/to_ir.py
index d25d21b1dc..aa6b8bbd00 100644
--- a/marimo/_convert/markdown/markdown.py
+++ b/marimo/_convert/markdown/to_ir.py
@@ -17,6 +17,14 @@ from typing import (
# Native to python
from xml.etree.ElementTree import Element, SubElement
+# Markdown is a dependency of marimo, as such we utilize it as much as possible
+# to parse markdown.
+from markdown import Markdown
+from markdown.blockparser import BlockParser
+from markdown.blockprocessors import BlockProcessor
+from markdown.preprocessors import Preprocessor
+from markdown.util import HTML_PLACEHOLDER_RE, Registry
+
# As are extensions
from pymdownx.superfences import ( # type: ignore
RE_NESTED_FENCE_START,
@@ -28,7 +36,7 @@ from marimo._ast.app import App, InternalApp
from marimo._ast.cell import Cell, CellConfig
from marimo._ast.compiler import compile_cell
from marimo._ast.names import DEFAULT_CELL_NAME
-from marimo._convert.utils import markdown_to_marimo, sql_to_marimo
+from marimo._convert.common.format import markdown_to_marimo, sql_to_marimo
from marimo._dependencies.dependencies import DependencyManager
from marimo._schemas.serialization import (
AppInstantiation,
@@ -37,14 +45,6 @@ from marimo._schemas.serialization import (
NotebookSerializationV1,
)
-# Markdown is a dependency of marimo, as such we utilize it as much as possible
-# to parse markdown.
-from markdown import Markdown
-from markdown.blockparser import BlockParser
-from markdown.blockprocessors import BlockProcessor
-from markdown.preprocessors import Preprocessor
-from markdown.util import HTML_PLACEHOLDER_RE, Registry
-
LOGGER = _loggers.marimo_logger()
MARIMO_MD = "marimo-md"
diff --git a/marimo/_convert/non_marimo_python_script.py b/marimo/_convert/non_marimo_python_script.py
index 5274981afe..eface7b4a9 100644
--- a/marimo/_convert/non_marimo_python_script.py
+++ b/marimo/_convert/non_marimo_python_script.py
@@ -5,7 +5,7 @@ from __future__ import annotations
import json
-from marimo._convert.ipynb import convert_from_ipynb_to_notebook_ir
+from marimo._convert.ipynb.to_ir import convert_from_ipynb_to_notebook_ir
from marimo._schemas.serialization import (
AppInstantiation,
Header,
diff --git a/marimo/_convert/script.py b/marimo/_convert/script.py
index 841aac0e6a..727b6f1250 100644
--- a/marimo/_convert/script.py
+++ b/marimo/_convert/script.py
@@ -10,7 +10,6 @@ from marimo._version import __version__
def convert_from_ir_to_script(ir: NotebookSerialization) -> str:
- filename = ir.filename or "notebook.py"
app = InternalApp(load_notebook_ir(ir))
# Check if any code is async, if so, raise an error
@@ -25,7 +24,10 @@ def convert_from_ir_to_script(ir: NotebookSerialization) -> str:
)
graph = app.graph
- header = codegen.get_header_comments(filename) or ""
+ if ir.filename:
+ header = codegen.get_header_comments(ir.filename) or ""
+ else:
+ header = ""
codes: list[str] = [
"# %%\n" + graph.cells[cid].code
for cid in topological_sort(graph, graph.cells.keys())
diff --git a/marimo/_runtime/packages/pypi_package_manager.py b/marimo/_runtime/packages/pypi_package_manager.py
index c55a07e2cc..a2a4a64022 100644
--- a/marimo/_runtime/packages/pypi_package_manager.py
+++ b/marimo/_runtime/packages/pypi_package_manager.py
@@ -445,7 +445,7 @@ class UvPackageManager(PypiPackageManager):
packages_to_remove: list[str],
upgrade: bool,
) -> bool:
- from marimo._convert.markdown.markdown import extract_frontmatter
+ from marimo._convert.markdown.to_ir import extract_frontmatter
from marimo._utils import yaml
from marimo._utils.inline_script_metadata import (
get_headers_from_frontmatter,
diff --git a/marimo/_server/api/endpoints/export.py b/marimo/_server/api/endpoints/export.py
index b59bcac8f7..f93347fea0 100644
--- a/marimo/_server/api/endpoints/export.py
+++ b/marimo/_server/api/endpoints/export.py
@@ -9,6 +9,10 @@ from starlette.exceptions import HTTPException
from starlette.responses import HTMLResponse, JSONResponse, PlainTextResponse
from marimo import _loggers
+from marimo._convert.common.filename import (
+ get_download_filename,
+ make_download_headers,
+)
from marimo._convert.markdown import convert_from_ir_to_markdown
from marimo._convert.script import convert_from_ir_to_script
from marimo._dependencies.dependencies import DependencyManager
@@ -16,10 +20,6 @@ from marimo._messaging.msgspec_encoder import asdict
from marimo._server.api.deps import AppState
from marimo._server.api.utils import parse_request
from marimo._server.export.exporter import AutoExporter, Exporter
-from marimo._server.export.utils import (
- get_download_filename,
- make_download_headers,
-)
from marimo._server.models.export import (
ExportAsHTMLRequest,
ExportAsMarkdownRequest,
diff --git a/marimo/_server/export/__init__.py b/marimo/_server/export/__init__.py
index ce23e2ed3b..cb89c0a7b3 100644
--- a/marimo/_server/export/__init__.py
+++ b/marimo/_server/export/__init__.py
@@ -16,6 +16,7 @@ from marimo._config.config import RuntimeConfig
from marimo._config.manager import (
get_default_config_manager,
)
+from marimo._convert.common.filename import get_download_filename
from marimo._convert.converters import MarimoConvert
from marimo._messaging.cell_output import CellChannel, CellOutput
from marimo._messaging.errors import Error, is_unexpected_error
@@ -29,7 +30,6 @@ from marimo._output.hypertext import patch_html_for_non_interactive_output
from marimo._runtime.commands import AppMetadata, SerializedCLIArgs
from marimo._schemas.serialization import NotebookSerialization
from marimo._server.export.exporter import Exporter
-from marimo._server.export.utils import get_download_filename
from marimo._server.file_router import AppFileRouter
from marimo._server.models.export import ExportAsHTMLRequest
from marimo._server.models.models import InstantiateNotebookRequest
diff --git a/marimo/_server/export/exporter.py b/marimo/_server/export/exporter.py
index 065654b139..a78cf0c0e9 100644
--- a/marimo/_server/export/exporter.py
+++ b/marimo/_server/export/exporter.py
@@ -3,20 +3,14 @@ from __future__ import annotations
import asyncio
import base64
-import io
-import json
import mimetypes
-import re
from concurrent.futures import ThreadPoolExecutor
-from html.parser import HTMLParser
from pathlib import Path
-from typing import TYPE_CHECKING, Any, Literal, Optional, Union, cast
+from typing import Literal, Optional, cast
from marimo import _loggers
from marimo._ast.app import InternalApp
-from marimo._ast.cell import Cell, CellConfig
-from marimo._ast.errors import CycleError, MultipleDefinitionError
-from marimo._ast.names import DEFAULT_CELL_NAME, is_internal_cell_name
+from marimo._ast.names import DEFAULT_CELL_NAME
from marimo._config.config import (
DEFAULT_CONFIG,
DisplayConfig,
@@ -24,25 +18,18 @@ from marimo._config.config import (
)
from marimo._config.settings import GLOBAL_SETTINGS
from marimo._config.utils import deep_copy
-from marimo._convert.utils import get_markdown_from_cell
-from marimo._dependencies.dependencies import DependencyManager
-from marimo._messaging.cell_output import CellChannel, CellOutput
-from marimo._messaging.errors import (
- Error as MarimoError,
- MarimoExceptionRaisedError,
-)
-from marimo._messaging.mimetypes import METADATA_KEY, KnownMimeType
-from marimo._runtime import dataflow
-from marimo._runtime.virtual_file import read_virtual_file
-from marimo._schemas.notebook import NotebookV1
-from marimo._schemas.session import NotebookSessionV1
-from marimo._server.export.dom_traversal import (
+from marimo._convert.common.dom_traversal import (
replace_virtual_files_with_data_uris,
)
-from marimo._server.export.utils import (
+from marimo._convert.common.filename import (
get_download_filename,
get_filename,
)
+from marimo._convert.ipynb.from_ir import convert_from_ir_to_ipynb
+from marimo._messaging.mimetypes import KnownMimeType
+from marimo._runtime.virtual_file import read_virtual_file
+from marimo._schemas.notebook import NotebookV1
+from marimo._schemas.session import NotebookSessionV1
from marimo._server.models.export import ExportAsHTMLRequest
from marimo._server.templates.templates import (
static_notebook_template,
@@ -66,9 +53,6 @@ LOGGER = _loggers.marimo_logger()
# Root directory for static assets
ROOT = (marimo_package_path() / "_static").resolve()
-if TYPE_CHECKING:
- from nbformat.notebooknode import NotebookNode # type: ignore
-
VIRTUAL_FILE_ALLOWED_ATTRIBUTES = {"src"}
# We don't include video/audio as it can potentially be too much data
# and the current use-cases are for images.
@@ -279,64 +263,9 @@ class Exporter:
session_view: Optional[SessionView] = None,
) -> str:
"""Export notebook as .ipynb, optionally including outputs if session_view provided."""
- DependencyManager.nbformat.require(
- "to convert marimo notebooks to ipynb"
+ return convert_from_ir_to_ipynb(
+ app, sort_mode=sort_mode, session_view=session_view
)
- import nbformat # type: ignore[import-not-found]
-
- notebook = nbformat.v4.new_notebook() # type: ignore[no-untyped-call]
- notebook["cells"] = []
-
- # Determine cell order based on sort_mode
- if sort_mode == "top-down":
- cell_data_list = list(app.cell_manager.cell_data())
- else:
- # Topological sort - try to sort, fall back to top-down on cycle
- try:
- graph = app.graph
- sorted_ids = dataflow.topological_sort(
- graph, graph.cells.keys()
- )
- # Build cell_data list in topological order
- cell_data_list = [
- app.cell_manager.cell_data_at(cid)
- for cid in sorted_ids
- if cid in graph.cells
- ]
- except (CycleError, MultipleDefinitionError):
- # Fall back to top-down order if graph is invalid
- cell_data_list = list(app.cell_manager.cell_data())
-
- for cell_data in cell_data_list:
- cid = cell_data.cell_id
-
- # Get outputs if session_view is provided
- outputs: list[NotebookNode] = []
- if session_view is not None:
- cell_output = session_view.get_cell_outputs([cid]).get(
- cid, None
- )
- cell_console_outputs = session_view.get_cell_console_outputs(
- [cid]
- ).get(cid, [])
- outputs = _convert_marimo_output_to_ipynb(
- cell_output, cell_console_outputs
- )
-
- notebook_cell = _create_ipynb_cell(
- cell_id=cid,
- code=cell_data.code,
- name=cell_data.name,
- config=cell_data.config,
- cell=cell_data.cell,
- outputs=outputs,
- )
- notebook["cells"].append(notebook_cell)
-
- stream = io.StringIO()
- nbformat.write(notebook, stream) # type: ignore[no-untyped-call]
- stream.seek(0)
- return stream.read()
def export_as_wasm(
self,
@@ -490,60 +419,6 @@ class AutoExporter:
self._executor.shutdown(wait=False)
-def _create_ipynb_cell(
- cell_id: str,
- code: str,
- name: str,
- config: CellConfig,
- cell: Optional[Cell],
- outputs: list[NotebookNode],
-) -> NotebookNode:
- """Create an ipynb cell with metadata.
-
- Args:
- cell_id: The cell's unique identifier
- code: The cell's source code
- name: The cell's name
- config: The cell's configuration
- cell: Optional Cell object for markdown detection
- outputs: List of cell outputs (ignored for markdown cells)
- """
- import nbformat
-
- # Try to extract markdown if we have a valid Cell
- if cell is not None:
- markdown_string = get_markdown_from_cell(cell, code)
- if markdown_string is not None:
- node = cast(
- nbformat.NotebookNode,
- nbformat.v4.new_markdown_cell(markdown_string, id=cell_id), # type: ignore[no-untyped-call]
- )
- _add_marimo_metadata(node, name, config)
- return node
-
- node = cast(
- nbformat.NotebookNode,
- nbformat.v4.new_code_cell(code, id=cell_id), # type: ignore[no-untyped-call]
- )
- if outputs:
- node.outputs = outputs
- _add_marimo_metadata(node, name, config)
- return node
-
-
-def _add_marimo_metadata(
- node: NotebookNode, name: str, config: CellConfig
-) -> None:
- """Add marimo-specific metadata to a notebook cell."""
- marimo_metadata: dict[str, Any] = {}
- if config.is_different_from_default():
- marimo_metadata["config"] = config.asdict_without_defaults()
- if not is_internal_cell_name(name):
- marimo_metadata["name"] = name
- if marimo_metadata:
- node["metadata"]["marimo"] = marimo_metadata
-
-
def get_html_contents() -> str:
if GLOBAL_SETTINGS.DEVELOPMENT_MODE:
import marimo._utils.requests as requests
@@ -557,184 +432,3 @@ def get_html_contents() -> str:
index_html = Path(ROOT) / "index.html"
return index_html.read_text(encoding="utf-8")
-
-
-def _maybe_extract_dataurl(data: Any) -> Any:
- if (
- isinstance(data, str)
- and data.startswith("data:")
- and ";base64," in data
- ):
- return data.split(";base64,")[1]
- else:
- return data
-
-
-class _HTMLTextExtractor(HTMLParser):
- """Extract plain text from HTML."""
-
- def __init__(self) -> None:
- super().__init__()
- self.text_parts: list[str] = []
-
- def handle_data(self, data: str) -> None:
- self.text_parts.append(data)
-
- def get_text(self) -> str:
- return "".join(self.text_parts)
-
-
-def _strip_html_from_traceback(html_traceback: str) -> list[str]:
- """Convert HTML-formatted traceback to plain text lines.
-
- Also strips temporary file paths from tracebacks. Exports run cells in
- the kernel which compiles them with temp file paths. It's not easy to
- set anonymous source since exports are done in the kernel, so we strip
- the paths here instead.
- """
- parser = _HTMLTextExtractor()
- parser.feed(html_traceback)
- text = parser.get_text()
-
- # Strip temp file paths like /tmp/marimo_12345/__marimo__cell_Hbol_.py
- # Replace with empty string to get cleaner tracebacks
- text = re.sub(r'[^"]*__marimo__cell_[^"]*\.py', "", text)
-
- return text.splitlines()
-
-
-def _extract_traceback_from_console(
- console_outputs: list[CellOutput],
-) -> list[str]:
- """Extract traceback lines from console outputs."""
- for output in console_outputs:
- if (
- output.channel == CellChannel.STDERR
- and output.mimetype == "application/vnd.marimo+traceback"
- ):
- return _strip_html_from_traceback(str(output.data))
- return []
-
-
-def _get_error_info(
- error: Union[MarimoError, dict[str, Any]],
-) -> tuple[str, str]:
- """Extract ename and evalue from a marimo error."""
- from marimo._messaging.msgspec_encoder import asdict
-
- if isinstance(error, dict):
- return error.get("type", "UnknownError"), error.get("msg", "")
- elif isinstance(error, MarimoExceptionRaisedError):
- return error.exception_type, error.msg.rstrip().strip(":")
- else:
- # For other error types, use the tag as ename and describe() as evalue
- error_dict = asdict(error)
- return error_dict.get("type", "Error"), error.describe()
-
-
-def _convert_marimo_output_to_ipynb(
- cell_output: Optional[CellOutput], console_outputs: list[CellOutput]
-) -> list[NotebookNode]:
- """Convert marimo output format to IPython notebook format."""
- import nbformat
-
- ipynb_outputs: list[NotebookNode] = []
-
- # Handle stdout/stderr
- for console_out in console_outputs:
- if console_out.channel == CellChannel.STDOUT:
- ipynb_outputs.append(
- cast(
- nbformat.NotebookNode,
- nbformat.v4.new_output( # type: ignore[no-untyped-call]
- "stream",
- name="stdout",
- text=console_out.data,
- ),
- )
- )
- elif console_out.channel == CellChannel.STDERR:
- # Skip tracebacks - they're included in error outputs
- if console_out.mimetype == "application/vnd.marimo+traceback":
- continue
- ipynb_outputs.append(
- cast(
- nbformat.NotebookNode,
- nbformat.v4.new_output( # type: ignore[no-untyped-call]
- "stream",
- name="stderr",
- text=console_out.data,
- ),
- )
- )
-
- if not cell_output:
- return ipynb_outputs
-
- if cell_output.data is None:
- return ipynb_outputs
-
- if cell_output.channel == CellChannel.MARIMO_ERROR:
- traceback_lines = _extract_traceback_from_console(console_outputs)
- errors = cast(
- list[Union[MarimoError, dict[str, Any]]], cell_output.data
- )
- for error in errors:
- ename, evalue = _get_error_info(error)
- ipynb_outputs.append(
- cast(
- nbformat.NotebookNode,
- nbformat.v4.new_output( # type: ignore[no-untyped-call]
- "error",
- ename=ename,
- evalue=evalue,
- traceback=traceback_lines,
- ),
- )
- )
- return ipynb_outputs
-
- if cell_output.channel not in (CellChannel.OUTPUT, CellChannel.MEDIA):
- return ipynb_outputs
-
- if cell_output.mimetype == "text/plain" and (
- cell_output.data == [] or cell_output.data == ""
- ):
- return ipynb_outputs
-
- # Handle rich output
- data: dict[str, Any] = {}
- metadata: dict[str, Any] = {}
-
- if cell_output.mimetype == "application/vnd.marimo+error":
- # Already handled above via MARIMO_ERROR channel
- return ipynb_outputs
- elif cell_output.mimetype == "application/vnd.marimo+mimebundle":
- if isinstance(cell_output.data, dict):
- mimebundle = cell_output.data
- elif isinstance(cell_output.data, str):
- mimebundle = json.loads(cell_output.data)
- else:
- raise ValueError(f"Invalid data type: {type(cell_output.data)}")
-
- for mime, content in mimebundle.items():
- if mime == METADATA_KEY and isinstance(content, dict):
- metadata = content
- else:
- data[mime] = _maybe_extract_dataurl(content)
- else:
- data[cell_output.mimetype] = _maybe_extract_dataurl(cell_output.data)
-
- if data:
- ipynb_outputs.append(
- cast(
- nbformat.NotebookNode,
- nbformat.v4.new_output( # type: ignore[no-untyped-call]
- "display_data",
- data=data,
- metadata=metadata,
- ),
- )
- )
-
- return ipynb_outputs
diff --git a/marimo/_utils/inline_script_metadata.py b/marimo/_utils/inline_script_metadata.py
index 82fc9a9e51..b4fa513cd1 100644
--- a/marimo/_utils/inline_script_metadata.py
+++ b/marimo/_utils/inline_script_metadata.py
@@ -209,7 +209,7 @@ def is_marimo_dependency(dependency: str) -> bool:
def get_headers_from_markdown(contents: str) -> dict[str, str]:
- from marimo._convert.markdown.markdown import extract_frontmatter
+ from marimo._convert.markdown.to_ir import extract_frontmatter
frontmatter, _ = extract_frontmatter(contents)
return get_headers_from_frontmatter(frontmatter)
diff --git a/pixi.lock b/pixi.lock
index 91190acafe..cd4174d905 100644
--- a/pixi.lock
+++ b/pixi.lock
@@ -1133,7 +1133,7 @@ packages:
- pypi: ./
name: marimo
version: 0.19.2
- sha256: a9b9623f9875a6ed5a6dc5adfce07b6ae1f89695dcc9e0a38ae8adf71e75efa9
+ sha256: aa4ab418a21eebcd80bd092626b71ec7e8409e5542beb1607ee6adaf0e5699dc
requires_dist:
- click>=8.0,<9
- jedi>=0.18.0
diff --git a/pyproject.toml b/pyproject.toml
index 615f26a334..2a3220eb15 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -351,8 +351,8 @@ exclude = [
"tests/_ast/codegen_data",
"tests/_ast/test_app.py",
"tests/_cli/cli_data",
- "tests/_cli/ipynb_data",
- "tests/_convert/ipynb_data/*.ipynb",
+ "tests/_cli/fixtures",
+ "tests/_convert/ipynb/fixtures",
"tests/_lint/test_files",
"tests/_runtime/runtime_data",
]
@@ -501,7 +501,8 @@ exclude = [
'tests/_ast/codegen_data',
'tests/_ast/cell_data',
'tests/_cli/cli_data',
- 'tests/_cli/ipynb_data',
+ 'tests/_cli/fixtures',
+ 'tests/_convert/ipynb/fixtures',
'tests/_runtime/runtime_data',
'marimo/_snippets/data/',
'marimo/_smoke_tests/',
@@ -510,7 +511,7 @@ warn_unused_ignores = false
[tool.pytest.ini_options]
minversion = "6.0"
-addopts = "-ra -q -v --ignore tests/_cli/ipynb_data --ignore tests/_ast/codegen_data --ignore tests/_ast/app_data"
+addopts = "-ra -q -v --ignore tests/_cli/fixtures --ignore tests/_ast/codegen_data --ignore tests/_ast/app_data"
testpaths = ["tests"]
asyncio_mode = "auto"
asyncio_default_fixture_loop_scope = "function"
diff --git a/scripts/generate_ipynb_fixtures.py b/scripts/generate_ipynb_fixtures.py
index 20f0797924..b8e5352c07 100644
--- a/scripts/generate_ipynb_fixtures.py
+++ b/scripts/generate_ipynb_fixtures.py
@@ -18,7 +18,7 @@ To add a new fixture, call `create_notebook_fixture(name, sources)`:
- `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/`.
+Output notebooks are written to `tests/_convert/fixtures/`.
"""
from __future__ import annotations
@@ -29,7 +29,7 @@ import jupytext
SELF_DIR = Path(__file__).parent
-FIXTURES_DIR = SELF_DIR / "../tests/_convert/ipynb_data"
+FIXTURES_DIR = SELF_DIR / "../tests/_convert/fixtures"
def create_notebook_fixture(name: str, sources: list[str | dict]) -> None:
diff --git a/tests/_cli/ipynb_data/arithmetic.ipynb.txt b/tests/_cli/fixtures/arithmetic.ipynb.txt
similarity index 100%
rename from tests/_cli/ipynb_data/arithmetic.ipynb.txt
rename to tests/_cli/fixtures/arithmetic.ipynb.txt
diff --git a/tests/_cli/ipynb_data/blank.ipynb.txt b/tests/_cli/fixtures/blank.ipynb.txt
similarity index 100%
rename from tests/_cli/ipynb_data/blank.ipynb.txt
rename to tests/_cli/fixtures/blank.ipynb.txt
diff --git a/tests/_cli/ipynb_data/juv.ipynb.txt b/tests/_cli/fixtures/juv.ipynb.txt
similarity index 100%
rename from tests/_cli/ipynb_data/juv.ipynb.txt
rename to tests/_cli/fixtures/juv.ipynb.txt
diff --git a/tests/_cli/ipynb_data/markdown.ipynb.txt b/tests/_cli/fixtures/markdown.ipynb.txt
similarity index 100%
rename from tests/_cli/ipynb_data/markdown.ipynb.txt
rename to tests/_cli/fixtures/markdown.ipynb.txt
diff --git a/tests/_cli/ipynb_data/multiple_defs.ipynb.txt b/tests/_cli/fixtures/multiple_defs.ipynb.txt
similarity index 100%
rename from tests/_cli/ipynb_data/multiple_defs.ipynb.txt
rename to tests/_cli/fixtures/multiple_defs.ipynb.txt
diff --git a/tests/_cli/ipynb_data/unparsable.ipynb.txt b/tests/_cli/fixtures/unparsable.ipynb.txt
similarity index 100%
rename from tests/_cli/ipynb_data/unparsable.ipynb.txt
rename to tests/_cli/fixtures/unparsable.ipynb.txt
diff --git a/tests/_convert/common/__init__.py b/tests/_convert/common/__init__.py
new file mode 100644
index 0000000000..fe2d2e388e
--- /dev/null
+++ b/tests/_convert/common/__init__.py
@@ -0,0 +1 @@
+# Copyright 2026 Marimo. All rights reserved.
diff --git a/tests/_convert/test_convert_utils.py b/tests/_convert/common/test_convert_format.py
similarity index 89%
rename from tests/_convert/test_convert_utils.py
rename to tests/_convert/common/test_convert_format.py
index ff2bce57c1..c669a4b6fa 100644
--- a/tests/_convert/test_convert_utils.py
+++ b/tests/_convert/common/test_convert_format.py
@@ -6,13 +6,13 @@ from textwrap import dedent
from marimo._ast import codegen
from marimo._ast.cell import CellConfig
from marimo._ast.compiler import compile_cell
-from marimo._convert import utils
+from marimo._convert.common import format
def test_markdown_to_marimo():
markdown = "# Hello, World!\nThis is a test."
expected = 'mo.md(r"""\n# Hello, World!\nThis is a test.\n""")' # noqa: E501
- assert utils.markdown_to_marimo(markdown) == expected
+ assert format.markdown_to_marimo(markdown) == expected
markdown = 'Here are some quotes: """'
expected = r'''
@@ -20,14 +20,14 @@ mo.md(r"""
Here are some quotes: \"\"\"
""")'''.strip()
- assert utils.markdown_to_marimo(markdown) == expected
+ assert format.markdown_to_marimo(markdown) == expected
markdown = r"This has a backslash: \\"
expected = '''
mo.md(r"""
This has a backslash: \\\\
""")'''.strip() # noqa: E501
- assert utils.markdown_to_marimo(markdown) == expected
+ assert format.markdown_to_marimo(markdown) == expected
def test_markdown_to_marimo_with_quotes():
@@ -38,7 +38,7 @@ mo.md(r"""
"this is markdown"
""")'''
).strip()
- assert utils.markdown_to_marimo(markdown) == expected
+ assert format.markdown_to_marimo(markdown) == expected
def test_generate_from_sources():
@@ -141,7 +141,7 @@ def test_markdown_with_quotes_and_cell_configs():
def test_get_markdown_from_cell_base():
empty_markdown_str = "mo.md('hello')"
- markdown = utils.get_markdown_from_cell(
+ markdown = format.get_markdown_from_cell(
compile_cell(empty_markdown_str, "id"), empty_markdown_str
)
assert markdown == "hello"
@@ -149,7 +149,7 @@ def test_get_markdown_from_cell_base():
def test_get_markdown_from_cell_empty():
empty_markdown_str = "mo.md()"
- markdown = utils.get_markdown_from_cell(
+ markdown = format.get_markdown_from_cell(
compile_cell(empty_markdown_str, "id"), empty_markdown_str
)
assert markdown is None
@@ -158,7 +158,7 @@ def test_get_markdown_from_cell_empty():
def test_get_markdown_from_cell_broken():
empty_markdown_str = "mo.md()"
# This can occur because the cell isn't recompiled at this point.
- markdown = utils.get_markdown_from_cell(
+ markdown = format.get_markdown_from_cell(
compile_cell(empty_markdown_str, "id"), "mo.md(f'{broken(}')"
)
assert markdown is None
diff --git a/tests/_server/export/test_dom_traversal.py b/tests/_convert/common/test_dom_traversal.py
similarity index 99%
rename from tests/_server/export/test_dom_traversal.py
rename to tests/_convert/common/test_dom_traversal.py
index fb6d02dd90..c44a1a5c5f 100644
--- a/tests/_server/export/test_dom_traversal.py
+++ b/tests/_convert/common/test_dom_traversal.py
@@ -1,7 +1,7 @@
# Copyright 2026 Marimo. All rights reserved.
from __future__ import annotations
-from marimo._server.export.dom_traversal import (
+from marimo._convert.common.dom_traversal import (
_is_virtual_file_url,
_parse_virtual_file_url,
replace_html_attributes,
diff --git a/tests/_convert/ipynb_data/cell_metadata.ipynb b/tests/_convert/ipynb/fixtures/ipynb/cell_metadata.ipynb
similarity index 87%
rename from tests/_convert/ipynb_data/cell_metadata.ipynb
rename to tests/_convert/ipynb/fixtures/ipynb/cell_metadata.ipynb
index 87ea83346a..835e3b40d6 100644
--- a/tests/_convert/ipynb_data/cell_metadata.ipynb
+++ b/tests/_convert/ipynb/fixtures/ipynb/cell_metadata.ipynb
@@ -12,7 +12,7 @@
},
"outputs": [],
"source": [
- "print('Hello')"
+ "print(\"Hello\")"
]
},
{
@@ -22,7 +22,7 @@
"metadata": {},
"outputs": [],
"source": [
- "print('World')"
+ "print(\"World\")"
]
},
{
@@ -37,7 +37,7 @@
},
"outputs": [],
"source": [
- "print('Cell 1')"
+ "print(\"Cell 1\")"
]
},
{
@@ -49,7 +49,7 @@
},
"outputs": [],
"source": [
- "print('Cell 2')"
+ "print(\"Cell 2\")"
]
},
{
@@ -64,7 +64,7 @@
},
"outputs": [],
"source": [
- "print('Cell 3')"
+ "print(\"Cell 3\")"
]
},
{
@@ -84,7 +84,7 @@
},
"outputs": [],
"source": [
- "print('Complex metadata')"
+ "print(\"Complex metadata\")"
]
},
{
@@ -98,7 +98,7 @@
},
"outputs": [],
"source": [
- "print('hidden cell')"
+ "print(\"hidden cell\")"
]
},
{
@@ -113,7 +113,7 @@
},
"outputs": [],
"source": [
- "print('hidden cell, with other tags')"
+ "print(\"hidden cell, with other tags\")"
]
}
],
diff --git a/tests/_convert/ipynb_data/comments_preservation.ipynb b/tests/_convert/ipynb/fixtures/ipynb/comments_preservation.ipynb
similarity index 97%
rename from tests/_convert/ipynb_data/comments_preservation.ipynb
rename to tests/_convert/ipynb/fixtures/ipynb/comments_preservation.ipynb
index 81a3de0bb0..878d9e1b43 100644
--- a/tests/_convert/ipynb_data/comments_preservation.ipynb
+++ b/tests/_convert/ipynb/fixtures/ipynb/comments_preservation.ipynb
@@ -153,7 +153,6 @@
],
"source": [
"# Cell 7: Duplicate import with different comment\n",
- "import numpy as np # Import numpy again with different comment\n",
"# This should be deduplicated but preserve one of the comments\n",
"arr = np.array([1, 2, 3]) # Create array"
]
@@ -166,8 +165,10 @@
"source": [
"# Cell 8: Complex expressions with comments\n",
"# Calculate something complex\n",
- "z = (x * 2 # Multiply x by 2\n",
- " + y) # Add y to the result\n",
+ "z = (\n",
+ " x * 2 # Multiply x by 2\n",
+ " + y\n",
+ ") # Add y to the result\n",
"# Final comment in cell"
]
},
@@ -194,6 +195,8 @@
" \"\"\"This is a docstring, not a comment.\"\"\"\n",
" # This is a comment inside the function\n",
" return 42 # Return a value\n",
+ "\n",
+ "\n",
"# Comment after function"
]
},
diff --git a/tests/_convert/ipynb_data/duplicate_definitions_and_aug_assign.ipynb b/tests/_convert/ipynb/fixtures/ipynb/duplicate_definitions_and_aug_assign.ipynb
similarity index 100%
rename from tests/_convert/ipynb_data/duplicate_definitions_and_aug_assign.ipynb
rename to tests/_convert/ipynb/fixtures/ipynb/duplicate_definitions_and_aug_assign.ipynb
diff --git a/tests/_convert/ipynb_data/duplicate_definitions_read_before_write.ipynb b/tests/_convert/ipynb/fixtures/ipynb/duplicate_definitions_read_before_write.ipynb
similarity index 94%
rename from tests/_convert/ipynb_data/duplicate_definitions_read_before_write.ipynb
rename to tests/_convert/ipynb/fixtures/ipynb/duplicate_definitions_read_before_write.ipynb
index d5f593fbcd..80115008bb 100644
--- a/tests/_convert/ipynb_data/duplicate_definitions_read_before_write.ipynb
+++ b/tests/_convert/ipynb/fixtures/ipynb/duplicate_definitions_read_before_write.ipynb
@@ -27,7 +27,9 @@
"metadata": {},
"outputs": [],
"source": [
- "x; x = 2; x"
+ "x\n",
+ "x = 2\n",
+ "x"
]
},
{
diff --git a/tests/_convert/ipynb_data/duplicate_definitions_syntax_error.ipynb b/tests/_convert/ipynb/fixtures/ipynb/duplicate_definitions_syntax_error.ipynb
similarity index 100%
rename from tests/_convert/ipynb_data/duplicate_definitions_syntax_error.ipynb
rename to tests/_convert/ipynb/fixtures/ipynb/duplicate_definitions_syntax_error.ipynb
diff --git a/tests/_convert/ipynb_data/hides_markdown_cells.ipynb b/tests/_convert/ipynb/fixtures/ipynb/hides_markdown_cells.ipynb
similarity index 100%
rename from tests/_convert/ipynb_data/hides_markdown_cells.ipynb
rename to tests/_convert/ipynb/fixtures/ipynb/hides_markdown_cells.ipynb
diff --git a/tests/_convert/ipynb_data/multiple_definitions.ipynb b/tests/_convert/ipynb/fixtures/ipynb/multiple_definitions.ipynb
similarity index 88%
rename from tests/_convert/ipynb_data/multiple_definitions.ipynb
rename to tests/_convert/ipynb/fixtures/ipynb/multiple_definitions.ipynb
index 13f3cee8ed..7cd070cd44 100644
--- a/tests/_convert/ipynb_data/multiple_definitions.ipynb
+++ b/tests/_convert/ipynb/fixtures/ipynb/multiple_definitions.ipynb
@@ -8,7 +8,7 @@
"outputs": [],
"source": [
"x = 1\n",
- "print(x) # print"
+ "print(x) # print"
]
},
{
@@ -19,7 +19,7 @@
"outputs": [],
"source": [
"x = 2\n",
- "print(x) # print"
+ "print(x) # print"
]
}
],
diff --git a/tests/_convert/ipynb_data/multiple_definitions_multiline.ipynb b/tests/_convert/ipynb/fixtures/ipynb/multiple_definitions_multiline.ipynb
similarity index 81%
rename from tests/_convert/ipynb_data/multiple_definitions_multiline.ipynb
rename to tests/_convert/ipynb/fixtures/ipynb/multiple_definitions_multiline.ipynb
index 1e1117b416..3c29d1b269 100644
--- a/tests/_convert/ipynb_data/multiple_definitions_multiline.ipynb
+++ b/tests/_convert/ipynb/fixtures/ipynb/multiple_definitions_multiline.ipynb
@@ -21,8 +21,8 @@
"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')"
+ " for j in nearest_partition[i_1, : K + 1]:\n",
+ " plt.plot(*zip(X_1[j], X_1[i_1]), color=\"black\")"
]
}
],
diff --git a/tests/_convert/ipynb_data/pip_commands.ipynb b/tests/_convert/ipynb/fixtures/ipynb/pip_commands.ipynb
similarity index 99%
rename from tests/_convert/ipynb_data/pip_commands.ipynb
rename to tests/_convert/ipynb/fixtures/ipynb/pip_commands.ipynb
index 63f02c0897..2b4bd38845 100644
--- a/tests/_convert/ipynb_data/pip_commands.ipynb
+++ b/tests/_convert/ipynb/fixtures/ipynb/pip_commands.ipynb
@@ -30,6 +30,7 @@
"# Mixed cell with pip and other commands\n",
"!pip install scikit-learn\n",
"import numpy as np\n",
+ "\n",
"!pip install seaborn"
]
},
diff --git a/tests/_convert/ipynb/fixtures/py/complex_file_format.py b/tests/_convert/ipynb/fixtures/py/complex_file_format.py
new file mode 100644
index 0000000000..06bec5335f
--- /dev/null
+++ b/tests/_convert/ipynb/fixtures/py/complex_file_format.py
@@ -0,0 +1,105 @@
+# /// script
+# description = "Complex file format with setup cell"
+# requires-python = ">=3.12"
+# dependencies = [
+# "marimo",
+# "pandas>=2.1.0",
+# "numpy>=2.1.0",
+# ]
+# ///
+
+import marimo
+
+__generated_with = "0.19.2"
+app = marimo.App(width="medium", auto_download=["html"], sql_output="native")
+
+with app.setup:
+ # Complex file format with setup cell
+ import marimo as mo
+
+
[email protected](hide_code=True)
+def _():
+ mo.md("""
+ # Documentation
+
+ This cell has **hidden code** and uses markdown.
+
+ - Feature 1
+ - Feature 2
+ """)
+ return
+
+
+def imports():
+ """Named cell with imports."""
+ import pandas as pd
+ import numpy as np
+ return np, pd
+
+
[email protected](disabled=True)
+def disabled_cell():
+ """This cell is disabled."""
+ x = 42
+ should_not_run = True
+ return
+
+
+def data_loading(np, pd):
+ """Named cell for data loading."""
+ df = pd.DataFrame({"a": np.array([1, 2, 3]), "b": np.array([4, 5, 6])})
+ return (df,)
+
+
+def analysis(df):
+ """Named cell with analysis and markdown output."""
+ result = df["a"].sum()
+ mo.md(f"The sum is **{result}**")
+ return
+
+
+def _():
+ """Unnamed cell."""
+ internal_var = 100
+ return
+
+
+def add(x, y):
+ """Pure function."""
+ return x + y
+
+
[email protected](hide_code=True)
+def remove(x, y):
+ """Hidden function."""
+ return x - y
+
+
+class MyClass:
+ """Pure class."""
+ def __init__(self, x, y):
+ self.x = x
+ self.y = y
+ def add(self):
+ return self.x + self.y
+
+
[email protected]_definition(hide_code=True)
+class MyHiddenClass:
+ """Hidden class."""
+ def __init__(self, x, y):
+ self.x = x
+ self.y = y
+ def add(self):
+ return self.x + self.y
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_convert/ipynb/fixtures/py/complex_outputs.py b/tests/_convert/ipynb/fixtures/py/complex_outputs.py
new file mode 100644
index 0000000000..8819ff26de
--- /dev/null
+++ b/tests/_convert/ipynb/fixtures/py/complex_outputs.py
@@ -0,0 +1,70 @@
+import marimo
+
+__generated_with = "0.19.2"
+app = marimo.App()
+
+
+def _():
+ import marimo as mo
+ return (mo,)
+
+
+def _(mo):
+ mo.md("""
+ # Testing Various Output Types
+
+ This notebook tests various output scenarios.
+ """)
+ return
+
+
+def _():
+ """Cell with print statements."""
+ print("Standard output")
+ print("Multiple lines")
+ return
+
+
+def _(mo):
+ """Cell with rich output."""
+ data = {"x": [1, 2, 3], "y": [4, 5, 6]}
+ mo.ui.table(data)
+ return (data,)
+
+
+def _(data):
+ """Cell with calculations and implicit output."""
+ result = sum(data["x"]) + sum(data["y"])
+ result
+ return
+
+
+def _(mo):
+ """Cell with multiple outputs."""
+ mo.md("## Section 1")
+ print("Debug info")
+ value = 42
+ value
+ return
+
+
+def error_cell():
+ """This would cause an error if run."""
+ # Note: This is valid Python, just demonstrates error handling
+ import sys
+
+ if hasattr(sys, "never_exists"):
+ raise ValueError("This should not happen")
+ success = True
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_convert/ipynb/fixtures/py/simple.py b/tests/_convert/ipynb/fixtures/py/simple.py
new file mode 100644
index 0000000000..92f69c97a2
--- /dev/null
+++ b/tests/_convert/ipynb/fixtures/py/simple.py
@@ -0,0 +1,36 @@
+import marimo
+
+__generated_with = "0.19.2"
+app = marimo.App()
+
+
+def _():
+ import marimo as mo
+ return (mo,)
+
+
+def _(mo):
+ mo.md("""
+ # Hello, World!
+ """)
+ return
+
+
+def _():
+ x = 1
+ y = 2
+ z = x + y
+ return (z,)
+
+
+def _(z):
+ print(z)
+ return
+
+
+if __name__ == "__main__":
+ app.run()
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.txt
new file mode 100644
index 0000000000..53ef791841
--- /dev/null
+++ b/tests/_convert/ipynb/snapshots/complex_file_format_top_down.ipynb.txt
@@ -0,0 +1,196 @@
+{
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "setup",
+ "metadata": {
+ "marimo": {
+ "name": "setup"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "# Complex file format with setup cell\n",
+ "import marimo as mo"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "Hbol",
+ "metadata": {
+ "marimo": {
+ "config": {
+ "hide_code": true
+ }
+ }
+ },
+ "source": [
+ "# Documentation\n",
+ "\n",
+ "This cell has **hidden code** and uses markdown.\n",
+ "\n",
+ "- Feature 1\n",
+ "- Feature 2"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "MJUe",
+ "metadata": {
+ "marimo": {
+ "name": "imports"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "\"\"\"Named cell with imports.\"\"\"\n",
+ "import pandas as pd\n",
+ "import numpy as np"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "vblA",
+ "metadata": {
+ "marimo": {
+ "config": {
+ "disabled": true
+ },
+ "name": "disabled_cell"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "\"\"\"This cell is disabled.\"\"\"\n",
+ "x = 42\n",
+ "should_not_run = True"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "bkHC",
+ "metadata": {
+ "marimo": {
+ "name": "data_loading"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "\"\"\"Named cell for data loading.\"\"\"\n",
+ "df = pd.DataFrame({\"a\": np.array([1, 2, 3]), \"b\": np.array([4, 5, 6])})"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "lEQa",
+ "metadata": {
+ "marimo": {
+ "name": "analysis"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "\"\"\"Named cell with analysis and markdown output.\"\"\"\n",
+ "result = df[\"a\"].sum()\n",
+ "mo.md(f\"The sum is **{result}**\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "PKri",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "\"\"\"Unnamed cell.\"\"\"\n",
+ "internal_var = 100"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "Xref",
+ "metadata": {
+ "marimo": {
+ "name": "*add"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "def add(x, y):\n",
+ " \"\"\"Pure function.\"\"\"\n",
+ " return x + y"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "SFPL",
+ "metadata": {
+ "marimo": {
+ "config": {
+ "hide_code": true
+ },
+ "name": "*remove"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "def remove(x, y):\n",
+ " \"\"\"Hidden function.\"\"\"\n",
+ " return x - y"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "BYtC",
+ "metadata": {
+ "marimo": {
+ "name": "*MyClass"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "class MyClass:\n",
+ " \"\"\"Pure class.\"\"\"\n",
+ " def __init__(self, x, y):\n",
+ " self.x = x\n",
+ " self.y = y\n",
+ " def add(self):\n",
+ " return self.x + self.y"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "RGSE",
+ "metadata": {
+ "marimo": {
+ "config": {
+ "hide_code": true
+ },
+ "name": "*MyHiddenClass"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "class MyHiddenClass:\n",
+ " \"\"\"Hidden class.\"\"\"\n",
+ " def __init__(self, x, y):\n",
+ " self.x = x\n",
+ " self.y = y\n",
+ " def add(self):\n",
+ " return self.x + self.y"
+ ]
+ }
+ ],
+ "metadata": {},
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
\ No newline at end of file
diff --git a/tests/_convert/ipynb/snapshots/complex_outputs_top_down.ipynb.txt b/tests/_convert/ipynb/snapshots/complex_outputs_top_down.ipynb.txt
new file mode 100644
index 0000000000..24624f7108
--- /dev/null
+++ b/tests/_convert/ipynb/snapshots/complex_outputs_top_down.ipynb.txt
@@ -0,0 +1,97 @@
+{
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "Hbol",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import marimo as mo"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "MJUe",
+ "metadata": {},
+ "source": [
+ "# Testing Various Output Types\n",
+ "\n",
+ "This notebook tests various output scenarios."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "vblA",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "\"\"\"Cell with print statements.\"\"\"\n",
+ "print(\"Standard output\")\n",
+ "print(\"Multiple lines\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "bkHC",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "\"\"\"Cell with rich output.\"\"\"\n",
+ "data = {\"x\": [1, 2, 3], \"y\": [4, 5, 6]}\n",
+ "mo.ui.table(data)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "lEQa",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "\"\"\"Cell with calculations and implicit output.\"\"\"\n",
+ "result = sum(data[\"x\"]) + sum(data[\"y\"])\n",
+ "result"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "PKri",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "\"\"\"Cell with multiple outputs.\"\"\"\n",
+ "mo.md(\"## Section 1\")\n",
+ "print(\"Debug info\")\n",
+ "value = 42\n",
+ "value"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "Xref",
+ "metadata": {
+ "marimo": {
+ "name": "error_cell"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "\"\"\"This would cause an error if run.\"\"\"\n",
+ "# Note: This is valid Python, just demonstrates error handling\n",
+ "import sys\n",
+ "\n",
+ "if hasattr(sys, \"never_exists\"):\n",
+ " raise ValueError(\"This should not happen\")\n",
+ "success = True"
+ ]
+ }
+ ],
+ "metadata": {},
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
\ No newline at end of file
diff --git a/tests/_convert/snapshots/convert_cell_metadata.py.txt b/tests/_convert/ipynb/snapshots/converted_cell_metadata.py.txt
similarity index 72%
rename from tests/_convert/snapshots/convert_cell_metadata.py.txt
rename to tests/_convert/ipynb/snapshots/converted_cell_metadata.py.txt
index 35ff989012..292aea871b 100644
--- a/tests/_convert/snapshots/convert_cell_metadata.py.txt
+++ b/tests/_convert/ipynb/snapshots/converted_cell_metadata.py.txt
@@ -6,53 +6,53 @@ app = marimo.App()
@app.cell
def _():
# Cell tags: tag1, tag2
- print('Hello')
+ print("Hello")
return
@app.cell
def _():
- print('World')
+ print("World")
return
@app.cell
def _():
# Cell tags: data-processing, important
- print('Cell 1')
+ print("Cell 1")
return
@app.cell
def _():
- print('Cell 2')
+ print("Cell 2")
return
@app.cell
def _():
# Cell tags: visualization
- print('Cell 3')
+ print("Cell 3")
return
@app.cell
def _():
# Cell tags: tag1, tag2
- print('Complex metadata')
+ print("Complex metadata")
return
@app.cell(hide_code=True)
def _():
- print('hidden cell')
+ print("hidden cell")
return
@app.cell(hide_code=True)
def _():
# Cell tags: remove-print
- print('hidden cell, with other tags')
+ print("hidden cell, with other tags")
return
diff --git a/tests/_convert/snapshots/convert_comments_preservation.py.txt b/tests/_convert/ipynb/snapshots/converted_comments_preservation.py.txt
similarity index 96%
rename from tests/_convert/snapshots/convert_comments_preservation.py.txt
rename to tests/_convert/ipynb/snapshots/converted_comments_preservation.py.txt
index aec962af94..780d7940ad 100644
--- a/tests/_convert/snapshots/convert_comments_preservation.py.txt
+++ b/tests/_convert/ipynb/snapshots/converted_comments_preservation.py.txt
@@ -64,7 +64,7 @@ def _():
def _(np):
# Cell 7: Duplicate import with different comment
# This should be deduplicated but preserve one of the comments
- arr = np.array([1, 2, 3]) # Import numpy again with different comment # Create array
+ arr = np.array([1, 2, 3]) # Create array
return
@@ -93,6 +93,8 @@ def _():
"""This is a docstring, not a comment."""
# This is a comment inside the function
return 42 # Return a value
+
+
# Comment after function
return
diff --git a/tests/_convert/snapshots/convert_duplicate_definitions_and_aug_assign.py.txt b/tests/_convert/ipynb/snapshots/converted_duplicate_definitions_and_aug_assign.py.txt
similarity index 100%
rename from tests/_convert/snapshots/convert_duplicate_definitions_and_aug_assign.py.txt
rename to tests/_convert/ipynb/snapshots/converted_duplicate_definitions_and_aug_assign.py.txt
diff --git a/tests/_convert/snapshots/convert_duplicate_definitions_read_before_write.py.txt b/tests/_convert/ipynb/snapshots/converted_duplicate_definitions_read_before_write.py.txt
similarity index 100%
rename from tests/_convert/snapshots/convert_duplicate_definitions_read_before_write.py.txt
rename to tests/_convert/ipynb/snapshots/converted_duplicate_definitions_read_before_write.py.txt
diff --git a/tests/_convert/snapshots/convert_duplicate_definitions_syntax_error.py.txt b/tests/_convert/ipynb/snapshots/converted_duplicate_definitions_syntax_error.py.txt
similarity index 100%
rename from tests/_convert/snapshots/convert_duplicate_definitions_syntax_error.py.txt
rename to tests/_convert/ipynb/snapshots/converted_duplicate_definitions_syntax_error.py.txt
diff --git a/tests/_convert/snapshots/convert_hides_markdown_cells.py.txt b/tests/_convert/ipynb/snapshots/converted_hides_markdown_cells.py.txt
similarity index 100%
rename from tests/_convert/snapshots/convert_hides_markdown_cells.py.txt
rename to tests/_convert/ipynb/snapshots/converted_hides_markdown_cells.py.txt
diff --git a/tests/_convert/snapshots/convert_multiple_definitions.py.txt b/tests/_convert/ipynb/snapshots/converted_multiple_definitions.py.txt
similarity index 100%
rename from tests/_convert/snapshots/convert_multiple_definitions.py.txt
rename to tests/_convert/ipynb/snapshots/converted_multiple_definitions.py.txt
diff --git a/tests/_convert/snapshots/convert_multiple_definitions_multiline.py.txt b/tests/_convert/ipynb/snapshots/converted_multiple_definitions_multiline.py.txt
similarity index 100%
rename from tests/_convert/snapshots/convert_multiple_definitions_multiline.py.txt
rename to tests/_convert/ipynb/snapshots/converted_multiple_definitions_multiline.py.txt
diff --git a/tests/_convert/snapshots/convert_pip_commands.py.txt b/tests/_convert/ipynb/snapshots/converted_pip_commands.py.txt
similarity index 99%
rename from tests/_convert/snapshots/convert_pip_commands.py.txt
rename to tests/_convert/ipynb/snapshots/converted_pip_commands.py.txt
index 0a966294d3..2927b80dce 100644
--- a/tests/_convert/snapshots/convert_pip_commands.py.txt
+++ b/tests/_convert/ipynb/snapshots/converted_pip_commands.py.txt
@@ -30,6 +30,7 @@ def _():
# Mixed cell with pip and other commands
# packages added via marimo's package management: scikit-learn !pip install scikit-learn
import numpy as np
+
# packages added via marimo's package management: seaborn !pip install seaborn
return
diff --git a/tests/_convert/ipynb/snapshots/simple_top_down.ipynb.txt b/tests/_convert/ipynb/snapshots/simple_top_down.ipynb.txt
new file mode 100644
index 0000000000..b1f5daf4cc
--- /dev/null
+++ b/tests/_convert/ipynb/snapshots/simple_top_down.ipynb.txt
@@ -0,0 +1,47 @@
+{
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "Hbol",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import marimo as mo"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "MJUe",
+ "metadata": {},
+ "source": [
+ "# Hello, World!"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "vblA",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "x = 1\n",
+ "y = 2\n",
+ "z = x + y"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "bkHC",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "print(z)"
+ ]
+ }
+ ],
+ "metadata": {},
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
\ No newline at end of file
diff --git a/tests/_convert/test_ipynb.py b/tests/_convert/ipynb/test_ipynb_to_ir.py
similarity index 99%
rename from tests/_convert/test_ipynb.py
rename to tests/_convert/ipynb/test_ipynb_to_ir.py
index 77241e0e62..d6c3f61bf5 100644
--- a/tests/_convert/test_ipynb.py
+++ b/tests/_convert/ipynb/test_ipynb_to_ir.py
@@ -5,7 +5,7 @@ from textwrap import dedent
import pytest
-from marimo._convert.ipynb import (
+from marimo._convert.ipynb.to_ir import (
CellsTransform,
CodeCell,
Transform,
diff --git a/tests/_convert/test_ipynb_to_marimo.py b/tests/_convert/ipynb/test_ipynb_to_marimo.py
similarity index 70%
rename from tests/_convert/test_ipynb_to_marimo.py
rename to tests/_convert/ipynb/test_ipynb_to_marimo.py
index ba426636cb..a8e57b37ee 100644
--- a/tests/_convert/test_ipynb_to_marimo.py
+++ b/tests/_convert/ipynb/test_ipynb_to_marimo.py
@@ -7,15 +7,15 @@ from pathlib import Path
import pytest
from marimo._convert.converters import MarimoConvert
-from marimo._convert.ipynb import convert_from_ipynb_to_notebook_ir
+from marimo._convert.ipynb.to_ir import convert_from_ipynb_to_notebook_ir
from tests.mocks import snapshotter
snapshot = snapshotter(__file__)
-DIR_PATH = Path(__file__).parent / "ipynb_data"
+DIR_PATH = Path(__file__).parent / "fixtures" / "ipynb"
[email protected]("ipynb_path", DIR_PATH.glob("*.ipynb.txt"))
[email protected]("ipynb_path", DIR_PATH.glob("*.ipynb"))
def test_ipynb_to_marimo_snapshots(ipynb_path: Path) -> None:
contents = ipynb_path.read_text()
ir = convert_from_ipynb_to_notebook_ir(contents)
@@ -24,6 +24,6 @@ def test_ipynb_to_marimo_snapshots(ipynb_path: Path) -> None:
converted = re.sub(r"__generated_with = .*\n", "", converted)
converted = re.sub(r"# requires-python = .*\n", "", converted)
snapshot(
- f"converted_{ipynb_path.name.replace('.ipynb.txt', '.py.txt')}",
+ f"converted_{ipynb_path.name.replace('.ipynb', '.py.txt')}",
converted,
)
diff --git a/tests/_convert/ipynb/test_iypnb_from_marimo.py b/tests/_convert/ipynb/test_iypnb_from_marimo.py
new file mode 100644
index 0000000000..2a8a4bc10b
--- /dev/null
+++ b/tests/_convert/ipynb/test_iypnb_from_marimo.py
@@ -0,0 +1,40 @@
+from __future__ import annotations
+
+import json
+import pathlib
+
+import pytest
+
+from marimo._ast.app import InternalApp
+from marimo._ast.load import load_app
+from marimo._convert.ipynb import convert_from_ir_to_ipynb
+from tests.mocks import snapshotter
+
+SELF_DIR = pathlib.Path(__file__).parent
+snapshot_test = snapshotter(__file__)
+
+pytest.importorskip("nbformat")
+
+
+ "py_path", (SELF_DIR / "fixtures" / "py").glob("*.py")
+)
+def test_convert_from_ir_to_ipynb_snapshots(py_path: pathlib.Path) -> None:
+ """Test convert_from_ir_to_ipynb against all Python fixtures using snapshots."""
+ # Load the marimo app from file
+ app = load_app(py_path)
+ assert app
+ internal_app = InternalApp(app)
+
+ # Convert
+ sort_mode = "top-down"
+ ipynb_str = convert_from_ir_to_ipynb(internal_app, sort_mode=sort_mode)
+
+ # Parse as JSON to validate and format consistently
+ ipynb_json = json.loads(ipynb_str)
+ formatted_ipynb = json.dumps(ipynb_json, indent=2, sort_keys=True)
+
+ base_name = py_path.name.replace(".py", "")
+ snapshot_name = f"{base_name}_{sort_mode.replace('-', '_')}.ipynb.txt"
+
+ snapshot_test(snapshot_name, formatted_ipynb)
diff --git a/tests/_convert/snapshots/dataflow.md.txt b/tests/_convert/markdown/snapshots/dataflow.md.txt
similarity index 100%
rename from tests/_convert/snapshots/dataflow.md.txt
rename to tests/_convert/markdown/snapshots/dataflow.md.txt
diff --git a/tests/_convert/snapshots/frontmatter-only.py.txt b/tests/_convert/markdown/snapshots/frontmatter-only.py.txt
similarity index 100%
rename from tests/_convert/snapshots/frontmatter-only.py.txt
rename to tests/_convert/markdown/snapshots/frontmatter-only.py.txt
diff --git a/tests/_convert/snapshots/frontmatter-test.py.txt b/tests/_convert/markdown/snapshots/frontmatter-test.py.txt
similarity index 100%
rename from tests/_convert/snapshots/frontmatter-test.py.txt
rename to tests/_convert/markdown/snapshots/frontmatter-test.py.txt
diff --git a/tests/_convert/snapshots/has-header.md.txt b/tests/_convert/markdown/snapshots/has-header.md.txt
similarity index 100%
rename from tests/_convert/snapshots/has-header.md.txt
rename to tests/_convert/markdown/snapshots/has-header.md.txt
diff --git a/tests/_convert/snapshots/marimo_for_jupyter_users.md.txt b/tests/_convert/markdown/snapshots/marimo_for_jupyter_users.md.txt
similarity index 100%
rename from tests/_convert/snapshots/marimo_for_jupyter_users.md.txt
rename to tests/_convert/markdown/snapshots/marimo_for_jupyter_users.md.txt
diff --git a/tests/_convert/snapshots/no-frontmatter.py.txt b/tests/_convert/markdown/snapshots/no-frontmatter.py.txt
similarity index 100%
rename from tests/_convert/snapshots/no-frontmatter.py.txt
rename to tests/_convert/markdown/snapshots/no-frontmatter.py.txt
diff --git a/tests/_convert/snapshots/sql-notebook.py.txt b/tests/_convert/markdown/snapshots/sql-notebook.py.txt
similarity index 100%
rename from tests/_convert/snapshots/sql-notebook.py.txt
rename to tests/_convert/markdown/snapshots/sql-notebook.py.txt
diff --git a/tests/_convert/snapshots/sql.md.txt b/tests/_convert/markdown/snapshots/sql.md.txt
similarity index 100%
rename from tests/_convert/snapshots/sql.md.txt
rename to tests/_convert/markdown/snapshots/sql.md.txt
diff --git a/tests/_convert/snapshots/unsafe-app.md.txt b/tests/_convert/markdown/snapshots/unsafe-app.md.txt
similarity index 100%
rename from tests/_convert/snapshots/unsafe-app.md.txt
rename to tests/_convert/markdown/snapshots/unsafe-app.md.txt
diff --git a/tests/_convert/snapshots/unsafe-app.py.txt b/tests/_convert/markdown/snapshots/unsafe-app.py.txt
similarity index 100%
rename from tests/_convert/snapshots/unsafe-app.py.txt
rename to tests/_convert/markdown/snapshots/unsafe-app.py.txt
diff --git a/tests/_convert/snapshots/unsafe-doc.md.txt b/tests/_convert/markdown/snapshots/unsafe-doc.md.txt
similarity index 100%
rename from tests/_convert/snapshots/unsafe-doc.md.txt
rename to tests/_convert/markdown/snapshots/unsafe-doc.md.txt
diff --git a/tests/_convert/snapshots/unsafe-doc.py.txt b/tests/_convert/markdown/snapshots/unsafe-doc.py.txt
similarity index 100%
rename from tests/_convert/snapshots/unsafe-doc.py.txt
rename to tests/_convert/markdown/snapshots/unsafe-doc.py.txt
diff --git a/tests/_convert/test_markdown_conversion.py b/tests/_convert/markdown/test_markdown_conversion.py
similarity index 97%
rename from tests/_convert/test_markdown_conversion.py
rename to tests/_convert/markdown/test_markdown_conversion.py
index 571eb5af37..275cdb5f9e 100644
--- a/tests/_convert/test_markdown_conversion.py
+++ b/tests/_convert/markdown/test_markdown_conversion.py
@@ -3,6 +3,7 @@ from __future__ import annotations
import os
import sys
+from pathlib import Path
from textwrap import dedent
import pytest
@@ -10,12 +11,10 @@ import pytest
from marimo import __version__
from marimo._ast.app import InternalApp
from marimo._convert.converters import MarimoConvert
-from marimo._convert.markdown.markdown import convert_from_md_to_app
-from marimo._server.export import export_as_md
+from marimo._convert.markdown.to_ir import convert_from_md_to_app
# Just a handful of scripts to test
from marimo._tutorials import dataflow, for_jupyter_users, sql
-from marimo._utils.marimo_path import MarimoPath
from tests.mocks import snapshotter
modules = {
@@ -52,7 +51,9 @@ def convert_from_py_to_md(py: str) -> str:
)
def test_markdown_snapshots() -> None:
for name, mod in modules.items():
- output = export_as_md(MarimoPath(mod.__file__)).contents
+ py_contents = Path(str(mod.__file__)).read_text(encoding="utf-8")
+ converter = MarimoConvert.from_py(py_contents)
+ output = converter.to_markdown(filename=f"{name}.py")
snapshot(f"{name}.md.txt", output)
diff --git a/tests/_convert/test_markdown_module.py b/tests/_convert/markdown/test_markdown_from_ir.py
similarity index 99%
rename from tests/_convert/test_markdown_module.py
rename to tests/_convert/markdown/test_markdown_from_ir.py
index 78247ff687..d14a75574c 100644
--- a/tests/_convert/test_markdown_module.py
+++ b/tests/_convert/markdown/test_markdown_from_ir.py
@@ -2,7 +2,7 @@
from __future__ import annotations
from marimo._ast.app import App, InternalApp
-from marimo._convert.markdown import (
+from marimo._convert.markdown.from_ir import (
_format_filename_title,
_get_sql_options_from_cell,
convert_from_ir_to_markdown,
diff --git a/tests/_convert/test_convert_ipynb.py b/tests/_convert/test_convert_ipynb.py
deleted file mode 100644
index 9dbc908161..0000000000
--- a/tests/_convert/test_convert_ipynb.py
+++ /dev/null
@@ -1,27 +0,0 @@
-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)
diff --git a/tests/_server/export/test_exporter.py b/tests/_server/export/test_exporter.py
index d2c2094a04..c63a250261 100644
--- a/tests/_server/export/test_exporter.py
+++ b/tests/_server/export/test_exporter.py
@@ -10,6 +10,10 @@ import pytest
from marimo._ast.app import App, InternalApp
from marimo._config.config import DEFAULT_CONFIG
+from marimo._convert.ipynb.from_ir import (
+ _convert_marimo_output_to_ipynb,
+ _maybe_extract_dataurl,
+)
from marimo._dependencies.dependencies import DependencyManager
from marimo._messaging.cell_output import CellChannel, CellOutput
from marimo._messaging.msgspec_encoder import encode_json_str
@@ -19,11 +23,7 @@ from marimo._server.export import (
run_app_then_export_as_ipynb,
run_app_until_completion,
)
-from marimo._server.export.exporter import (
- Exporter,
- _convert_marimo_output_to_ipynb,
- _maybe_extract_dataurl,
-)
+from marimo._server.export.exporter import Exporter
from marimo._server.models.export import ExportAsHTMLRequest
from marimo._session.notebook import AppFileManager
from marimo._session.state.session_view import SessionView
@@ -986,7 +986,7 @@ def test_export_html_replaces_virtual_files_in_outputs(
# Mock read_virtual_file to return test image data
with patch(
- "marimo._server.export.dom_traversal.read_virtual_file"
+ "marimo._convert.common.dom_traversal.read_virtual_file"
) as mock_read:
mock_read.return_value = b"fake_image_data"
@@ -1116,7 +1116,7 @@ def test_export_html_replaces_multiple_virtual_files_complex(
with (
patch(
- "marimo._server.export.dom_traversal.read_virtual_file"
+ "marimo._convert.common.dom_traversal.read_virtual_file"
) as mock_read_dom,
patch(
"marimo._server.export.exporter.read_virtual_file"