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: cleanup markdown serializers (#7569)

Myles Scolnick62d6a3f1dfa2
diff --git a/marimo/_ast/app.py b/marimo/_ast/app.py
index d63ab3be2c..dbcc4c6ca5 100644
--- a/marimo/_ast/app.py
+++ b/marimo/_ast/app.py
@@ -61,6 +61,7 @@ from marimo._runtime.context.types import (
from marimo._schemas.serialization import (
AppInstantiation,
CellDef,
+ Header,
NotebookSerializationV1,
)
from marimo._types.ids import CellId_t
@@ -243,6 +244,7 @@ class App:
self._graph = dataflow.DirectedGraph()
self._execution_context: ExecutionContext | None = None
self._runner = dataflow.Runner(self._graph)
+ self._header: str | None = None
self._unparsable_code: list[str] = []
self._unparsable = False
@@ -1015,6 +1017,9 @@ class InternalApp:
def to_ir(self) -> NotebookSerializationV1:
return NotebookSerializationV1(
+ header=Header(value=self._app._header)
+ if self._app._header
+ else None,
cells=[
CellDef(
code=cell_data.code,
@@ -1026,6 +1031,7 @@ class InternalApp:
app=AppInstantiation(
options=self._app._config.asdict(),
),
+ filename=self._app._filename,
)
def to_py(self) -> str:
diff --git a/marimo/_ast/load.py b/marimo/_ast/load.py
index d3cbed9f30..4f2e6c915e 100644
--- a/marimo/_ast/load.py
+++ b/marimo/_ast/load.py
@@ -123,6 +123,7 @@ def find_cell(filename: str, lineno: int) -> CellDef | None:
def load_notebook_ir(
notebook: NotebookSerialization, filepath: Optional[str] = None
) -> App:
+ """Load a notebook IR into an App."""
# Use filepath from notebook if not explicitly provided
if filepath is None:
filepath = notebook.filename
@@ -132,44 +133,11 @@ def load_notebook_ir(
app._unparsable_cell(cell.code, **cell.options)
continue
app._cell_manager.register_ir_cell(cell, InternalApp(app))
+ if notebook.header and notebook.header.value:
+ app._header = notebook.header.value
return app
-def load_notebook(filename: str) -> Optional[NotebookSerialization]:
- """Load and return notebook serialization from a marimo notebook file.
-
- Args:
- filename: Path to a marimo notebook file (.py or .md)
-
- Returns:
- NotebookSerialization if the file exists and contains valid code,
- None if the file is empty or contains only comments.
-
- Raises:
- MarimoFileError: If the file exists but doesn't define a valid marimo app
- RuntimeError: If there are issues loading the module
- SyntaxError: If the file contains a syntax error
- FileNotFoundError: If the file doesn't exist
- """
- path = Path(filename)
-
- contents = _maybe_contents(filename)
- if not contents:
- return None
-
- if path.suffix in (".md", ".qmd"):
- from marimo._convert.markdown.markdown import (
- convert_from_md_to_marimo_ir,
- )
-
- return convert_from_md_to_marimo_ir(contents)
-
- if path.suffix == ".py":
- return parse_notebook(contents)
-
- raise MarimoFileError("File must end with .py, .md, or .qmd.")
-
-
def get_notebook_status(filename: str) -> LoadResult:
"""Attempts to parse an app- should raise SyntaxError on failure.
diff --git a/marimo/_cli/export/commands.py b/marimo/_cli/export/commands.py
index f0c0020af2..072d21135e 100644
--- a/marimo/_cli/export/commands.py
+++ b/marimo/_cli/export/commands.py
@@ -353,7 +353,7 @@ def md(
return
def export_callback(file_path: MarimoPath) -> ExportResult:
- return export_as_md(file_path, new_filename=output)
+ return export_as_md(file_path)
return watch_and_export(
MarimoPath(name), output, watch, export_callback, force
diff --git a/marimo/_convert/converters.py b/marimo/_convert/converters.py
index 9954f7f3bd..40e1fb573b 100644
--- a/marimo/_convert/converters.py
+++ b/marimo/_convert/converters.py
@@ -22,11 +22,9 @@ class MarimoConverterIntermediate:
def to_markdown(self, filename: str | None = None) -> str:
"""Convert to markdown format."""
- from marimo._server.export.exporter import Exporter
+ from marimo._convert.markdown import convert_from_ir_to_markdown
- exporter = Exporter()
- generated_contents, _ = exporter.export_as_md(self.ir, filename)
- return generated_contents
+ return convert_from_ir_to_markdown(self.ir, filename)
def to_py(self) -> str:
"""Convert to python format."""
diff --git a/marimo/_convert/markdown/__init__.py b/marimo/_convert/markdown/__init__.py
new file mode 100644
index 0000000000..8342a2e187
--- /dev/null
+++ b/marimo/_convert/markdown/__init__.py
@@ -0,0 +1,202 @@
+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.
+ if filename.endswith(".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))
+
+ 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/markdown.py
index 7df156bb0c..02d1051bea 100644
--- a/marimo/_convert/markdown/markdown.py
+++ b/marimo/_convert/markdown/markdown.py
@@ -17,14 +17,6 @@ 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,
@@ -45,6 +37,14 @@ 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/script.py b/marimo/_convert/script.py
new file mode 100644
index 0000000000..31c79d1b9f
--- /dev/null
+++ b/marimo/_convert/script.py
@@ -0,0 +1,34 @@
+from __future__ import annotations
+
+from marimo._ast import codegen
+from marimo._ast.app import InternalApp
+from marimo._ast.load import load_notebook_ir
+from marimo._runtime.dataflow import topological_sort
+from marimo._schemas.serialization import NotebookSerialization
+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
+ for cell in app.cell_manager.cells():
+ if not cell:
+ continue
+ if cell._is_coroutine:
+ from click import UsageError
+
+ raise UsageError(
+ "Cannot export a notebook with async code to a flat script"
+ )
+
+ graph = app.graph
+ header = codegen.get_header_comments(filename) or ""
+ codes: list[str] = [
+ "# %%\n" + graph.cells[cid].code
+ for cid in topological_sort(graph, graph.cells.keys())
+ ]
+ return f'{header}\n__generated_with = "{__version__}"\n\n' + "\n\n".join(
+ codes
+ )
diff --git a/marimo/_convert/utils.py b/marimo/_convert/utils.py
index 9a091cf318..caa2e6b773 100644
--- a/marimo/_convert/utils.py
+++ b/marimo/_convert/utils.py
@@ -1,7 +1,13 @@
# Copyright 2026 Marimo. All rights reserved.
from __future__ import annotations
+from typing import TYPE_CHECKING, Optional, Union
+
from marimo._ast import codegen
+from marimo._ast.compiler import extract_markdown
+
+if TYPE_CHECKING:
+ from marimo._ast.cell import Cell, CellImpl
def markdown_to_marimo(source: str) -> str:
@@ -38,3 +44,13 @@ def sql_to_marimo(
")",
]
)
+
+
+def get_markdown_from_cell(
+ cell: Union[CellImpl, Cell], code: str
+) -> Optional[str]:
+ """Attempt to extract markdown from a cell, or return None"""
+
+ if not (cell.refs == {"mo"} and not cell.defs):
+ return None
+ return extract_markdown(code)
diff --git a/marimo/_lint/linter.py b/marimo/_lint/linter.py
index 4c6ea94896..d0f0206a08 100644
--- a/marimo/_lint/linter.py
+++ b/marimo/_lint/linter.py
@@ -262,7 +262,7 @@ class Linter:
with capture_output():
if filename.endswith((".md", ".qmd")):
- return converter.to_markdown(filename)
+ return converter.to_markdown()
else:
return converter.to_py()
diff --git a/marimo/_pyodide/pyodide_session.py b/marimo/_pyodide/pyodide_session.py
index c1b0fbc2eb..690a6878b7 100644
--- a/marimo/_pyodide/pyodide_session.py
+++ b/marimo/_pyodide/pyodide_session.py
@@ -15,6 +15,7 @@ from marimo._config.config import (
PartialMarimoConfig,
merge_default_config,
)
+from marimo._convert.markdown import convert_from_ir_to_markdown
from marimo._messaging.msgspec_encoder import encode_json_str
from marimo._pyodide.restartable_task import RestartableTask
from marimo._pyodide.streams import (
@@ -387,10 +388,7 @@ class PyodideBridge:
def export_markdown(self, request: str) -> str:
del request
- md, _filename = Exporter().export_as_md(
- notebook=self.session.app_manager.app.to_ir(),
- filename=self.session.app_manager.filename,
- )
+ md = convert_from_ir_to_markdown(self.session.app_manager.app.to_ir())
return json.dumps(md)
def _parse(self, request: str, cls: type[T]) -> T:
diff --git a/marimo/_server/api/endpoints/export.py b/marimo/_server/api/endpoints/export.py
index d3220c7fe6..a2dea62029 100644
--- a/marimo/_server/api/endpoints/export.py
+++ b/marimo/_server/api/endpoints/export.py
@@ -9,12 +9,15 @@ from starlette.exceptions import HTTPException
from starlette.responses import HTMLResponse, JSONResponse, PlainTextResponse
from marimo import _loggers
+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
from marimo._messaging.msgspec_encoder import asdict
from marimo._server.api.deps import AppState
from marimo._server.api.status import HTTPStatus
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
from marimo._server.model import SessionMode
from marimo._server.models.export import (
ExportAsHTMLRequest,
@@ -186,9 +189,9 @@ async def export_as_script(
body = await parse_request(request, cls=ExportAsScriptRequest)
session = app_state.require_current_session()
- python, filename = Exporter().export_as_script(
- app=session.app_file_manager.app,
- filename=session.app_file_manager.filename,
+ python = convert_from_ir_to_script(session.app_file_manager.app.to_ir())
+ filename = get_download_filename(
+ session.app_file_manager.filename, "script.py"
)
if body.download:
@@ -237,13 +240,15 @@ async def export_as_markdown(
detail="File must be saved before downloading",
)
- markdown, filename = Exporter().export_as_md(
- notebook=app_file_manager.app.to_ir(),
- filename=app_file_manager.filename,
- )
+ markdown = convert_from_ir_to_markdown(app_file_manager.app.to_ir())
if body.download:
- headers = {"Content-Disposition": f"attachment; filename={filename}"}
+ download_filename = get_download_filename(
+ app_file_manager.filename, "md"
+ )
+ headers = {
+ "Content-Disposition": f"attachment; filename={download_filename}"
+ }
else:
headers = {}
@@ -295,9 +300,8 @@ async def auto_export_as_markdown(
# Reload the file manager to get the latest state
session.app_file_manager.reload()
- markdown, _filename = Exporter().export_as_md(
- notebook=session.app_file_manager.app.to_ir(),
- filename=session.app_file_manager.filename,
+ markdown = convert_from_ir_to_markdown(
+ session.app_file_manager.app.to_ir()
)
# Save the Markdown file to disk, at `.marimo/<filename>.md`
@@ -359,9 +363,8 @@ async def auto_export_as_ipynb(
# Reload the file manager to get the latest state
session.app_file_manager.reload()
- ipynb, _filename = Exporter().export_as_ipynb(
+ ipynb = Exporter().export_as_ipynb(
app=session.app_file_manager.app,
- filename=session.app_file_manager.filename,
sort_mode="top-down",
session_view=session_view,
)
diff --git a/marimo/_server/export/__init__.py b/marimo/_server/export/__init__.py
index 1e01e0a9d4..b40f73f57b 100644
--- a/marimo/_server/export/__init__.py
+++ b/marimo/_server/export/__init__.py
@@ -4,15 +4,18 @@ from __future__ import annotations
import asyncio
import os
import sys
-from dataclasses import dataclass
+from dataclasses import dataclass, replace
from typing import TYPE_CHECKING, Literal, Optional, cast
from marimo import _loggers
+from marimo._ast.app import InternalApp
+from marimo._ast.load import load_app
from marimo._cli.print import echo
from marimo._config.config import RuntimeConfig
from marimo._config.manager import (
get_default_config_manager,
)
+from marimo._convert.converters import MarimoConvert
from marimo._messaging.cell_output import CellChannel, CellOutput
from marimo._messaging.errors import Error, is_unexpected_error
from marimo._messaging.notification import (
@@ -23,7 +26,9 @@ from marimo._messaging.serde import deserialize_kernel_message
from marimo._messaging.types import KernelMessage
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.model import ConnectionState, SessionMode
from marimo._server.models.export import ExportAsHTMLRequest
@@ -35,8 +40,6 @@ from marimo._utils.marimo_path import MarimoPath
LOGGER = _loggers.marimo_logger()
if TYPE_CHECKING:
- from pathlib import Path
-
from marimo._server.session.session_view import SessionView
from marimo._server.sessions.types import Session
@@ -48,66 +51,56 @@ class ExportResult:
did_error: bool
-def export_as_script(
- path: MarimoPath,
-) -> ExportResult:
- file_router = AppFileRouter.from_filename(path)
- file_key = file_router.get_unique_file_key()
- assert file_key is not None
- file_manager = file_router.get_file_manager(file_key)
+def _as_ir(path: MarimoPath) -> NotebookSerialization:
+ if path.is_python():
+ py_contents = path.read_text(encoding="utf-8")
+ converter = MarimoConvert.from_py(py_contents)
+ return _with_filename(converter.ir, path.short_name)
+ elif path.is_markdown():
+ md_contents = path.read_text(encoding="utf-8")
+ converter = MarimoConvert.from_md(md_contents)
+ return _with_filename(converter.ir, path.short_name)
+ raise ValueError(f"Unsupported file type: {path.path.suffix}")
- result = Exporter().export_as_script(
- filename=file_manager.filename,
- app=file_manager.app,
- )
+
+def export_as_script(path: MarimoPath) -> ExportResult:
+ from marimo._convert.script import convert_from_ir_to_script
+
+ ir = _as_ir(path)
return ExportResult(
- contents=result[0],
- download_filename=result[1],
+ contents=convert_from_ir_to_script(ir),
+ download_filename=get_download_filename(path.short_name, "script.py"),
did_error=False,
)
-def export_as_md(
- path: MarimoPath,
- new_filename: Optional[Path] = None,
-) -> ExportResult:
- file_router = AppFileRouter.from_filename(path)
- file_key = file_router.get_unique_file_key()
- assert file_key is not None
- file_manager = file_router.get_file_manager(file_key)
-
- # py -> md
- if new_filename:
- file_manager.filename = str(new_filename)
- result = Exporter().export_as_md(
- notebook=file_manager.app.to_ir(),
- filename=file_manager.filename,
- previous=path.path,
- )
+def export_as_md(path: MarimoPath) -> ExportResult:
+ ir = _as_ir(path)
return ExportResult(
- contents=result[0],
- download_filename=result[1],
+ contents=MarimoConvert.from_ir(ir).to_markdown(),
+ download_filename=get_download_filename(path.short_name, "md"),
did_error=False,
)
def export_as_ipynb(
- path: MarimoPath,
- sort_mode: Literal["top-down", "topological"],
+ path: MarimoPath, sort_mode: Literal["top-down", "topological"]
) -> ExportResult:
- file_router = AppFileRouter.from_filename(path)
- file_key = file_router.get_unique_file_key()
- assert file_key is not None
- file_manager = file_router.get_file_manager(file_key)
+ app = load_app(path.absolute_name)
+ if app is None:
+ return ExportResult(
+ contents="",
+ download_filename=get_download_filename(path.short_name, "ipynb"),
+ did_error=True,
+ )
result = Exporter().export_as_ipynb(
- filename=file_manager.filename,
- app=file_manager.app,
+ app=InternalApp(app),
sort_mode=sort_mode,
)
return ExportResult(
- contents=result[0],
- download_filename=result[1],
+ contents=result,
+ download_filename=get_download_filename(path.short_name, "ipynb"),
did_error=False,
)
@@ -118,20 +111,26 @@ def export_as_wasm(
show_code: bool,
asset_url: Optional[str] = None,
) -> ExportResult:
- file_router = AppFileRouter.from_filename(path)
- file_key = file_router.get_unique_file_key()
- assert file_key is not None
- file_manager = file_router.get_file_manager(file_key)
+ _app = load_app(path.absolute_name)
+ if _app is None:
+ return ExportResult(
+ contents="",
+ download_filename=get_download_filename(
+ path.short_name, "wasm.html"
+ ),
+ did_error=True,
+ )
+ app = InternalApp(_app)
# Inline the layout file, if it exists
- file_manager.app.inline_layout_file()
- config = get_default_config_manager(current_path=file_manager.path)
+ app.inline_layout_file()
+ config = get_default_config_manager(current_path=path.absolute_name)
result = Exporter().export_as_wasm(
- filename=file_manager.filename,
- app=file_manager.app,
+ filename=path.short_name,
+ app=app,
display_config=config.get_config()["display"],
mode=mode,
- code=file_manager.to_code(),
+ code=app.to_py(),
asset_url=asset_url,
show_code=show_code,
)
@@ -161,14 +160,13 @@ async def run_app_then_export_as_ipynb(
)
result = Exporter().export_as_ipynb(
- filename=file_manager.filename,
app=file_manager.app,
sort_mode=sort_mode,
session_view=session_view,
)
return ExportResult(
- contents=result[0],
- download_filename=result[1],
+ contents=result,
+ download_filename=get_download_filename(filepath.short_name, "ipynb"),
did_error=did_error,
)
@@ -362,3 +360,9 @@ async def run_app_until_completion(
session.close()
return session.session_view, session_consumer.did_error
+
+
+def _with_filename(
+ ir: NotebookSerialization, filename: str
+) -> NotebookSerialization:
+ return replace(ir, filename=filename)
diff --git a/marimo/_server/export/exporter.py b/marimo/_server/export/exporter.py
index a5bbefb09f..875e733c92 100644
--- a/marimo/_server/export/exporter.py
+++ b/marimo/_server/export/exporter.py
@@ -16,11 +16,9 @@ from typing import (
)
from marimo import _loggers
-from marimo._ast import codegen
from marimo._ast.app import InternalApp
from marimo._ast.cell import Cell, CellImpl
from marimo._ast.names import DEFAULT_CELL_NAME, is_internal_cell_name
-from marimo._ast.visitor import Language
from marimo._config.config import (
DEFAULT_CONFIG,
DisplayConfig,
@@ -28,23 +26,20 @@ 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.mimetypes import KnownMimeType
from marimo._runtime import dataflow
from marimo._runtime.virtual_file import read_virtual_file
from marimo._schemas.notebook import NotebookV1
-from marimo._schemas.serialization import NotebookSerializationV1
from marimo._schemas.session import NotebookSessionV1
from marimo._server.export.dom_traversal import (
replace_virtual_files_with_data_uris,
)
from marimo._server.export.utils import (
- format_filename_title,
get_download_filename,
get_filename,
- get_markdown_from_cell,
- get_sql_options_from_cell,
)
from marimo._server.models.export import ExportAsHTMLRequest
from marimo._server.session.serialize import (
@@ -57,7 +52,6 @@ from marimo._server.templates.templates import (
wasm_notebook_template,
)
from marimo._server.tokens import SkewProtectionToken
-from marimo._types.ids import CellId_t
from marimo._utils import async_path
from marimo._utils.code import hash_code
from marimo._utils.data_uri import build_data_url
@@ -275,43 +269,13 @@ class Exporter:
base64.b64encode(buffer_contents),
)
- def export_as_script(
- self,
- filename: Optional[str],
- app: InternalApp,
- ) -> tuple[str, str]:
- # Check if any code is async, if so, raise an error
- for cell in app.cell_manager.cells():
- if not cell:
- continue
- if cell._is_coroutine:
- from click import UsageError
-
- raise UsageError(
- "Cannot export a notebook with async code to a flat script"
- )
-
- graph = app.graph
- header = codegen.get_header_comments(filename) if filename else ""
- codes: list[str] = [
- "# %%\n" + graph.cells[cid].code
- for cid in dataflow.topological_sort(graph, graph.cells.keys())
- ]
- code = (
- f'{header}\n__generated_with = "{__version__}"\n\n'
- + "\n\n".join(codes)
- )
-
- download_filename = get_download_filename(filename, "script.py")
- return code, download_filename
-
def export_as_ipynb(
self,
app: InternalApp,
- filename: Optional[str],
+ *,
sort_mode: Literal["top-down", "topological"],
session_view: Optional[SessionView] = None,
- ) -> tuple[str, str]:
+ ) -> str:
"""Export notebook as .ipynb, optionally including outputs if session_view provided."""
DependencyManager.nbformat.require(
"to convert marimo notebooks to ipynb"
@@ -366,160 +330,7 @@ class Exporter:
stream = io.StringIO()
nbformat.write(notebook, stream) # type: ignore[no-untyped-call]
stream.seek(0)
- download_filename = get_download_filename(filename, "ipynb")
- return stream.read(), download_filename
-
- def export_as_md(
- self,
- notebook: NotebookSerializationV1,
- filename: Optional[str],
- previous: Path | None = None,
- ) -> tuple[str, str]:
- from marimo._ast.app_config import _AppConfig
- from marimo._ast.compiler import compile_cell
- from marimo._convert.markdown.markdown import (
- extract_frontmatter,
- formatted_code_block,
- is_sanitized_markdown,
- )
- from marimo._utils import yaml
-
- filename = get_filename(filename)
- 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)
- }
- )
- # If previously a markdown file, extract frontmatter.
- # otherwise if it was a python file, extract header.
- if previous and previous.suffix == ".py":
- header = codegen.get_header_comments(previous)
- if header:
- metadata["header"] = header.strip()
- else:
- header_file = previous if previous else filename
- if header_file and Path(header_file).exists():
- with open(header_file, encoding="utf-8") as f:
- _metadata, _ = extract_frontmatter(f.read())
- metadata.update(_metadata)
-
- # Add the expected qmd filter to the metadata.
- if filename.endswith(".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
- # 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"
- # 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))
-
- download_filename = get_download_filename(filename, "md")
- return "\n".join(document).strip(), download_filename
+ return stream.read()
def export_as_wasm(
self,
diff --git a/marimo/_server/export/utils.py b/marimo/_server/export/utils.py
index b73e01b45b..1966aa7fa2 100644
--- a/marimo/_server/export/utils.py
+++ b/marimo/_server/export/utils.py
@@ -1,20 +1,8 @@
# Copyright 2026 Marimo. All rights reserved.
from __future__ import annotations
-import ast
import os
-import re
-from typing import Optional, Union
-
-from marimo._ast.cell import Cell, CellImpl
-from marimo._ast.compiler import const_or_id, extract_markdown
-
-
-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()
+from typing import Optional
def get_filename(filename: Optional[str], default: str = "notebook.py") -> str:
@@ -29,38 +17,3 @@ def get_download_filename(filename: Optional[str], extension: str) -> str:
if basename.endswith(f".{extension}"):
return basename
return f"{os.path.splitext(basename)[0]}.{extension}"
-
-
-def get_markdown_from_cell(
- cell: Union[CellImpl, Cell], code: str
-) -> Optional[str]:
- """Attempt to extract markdown from a cell, or return None"""
-
- if not (cell.refs == {"mo"} and not cell.defs):
- return None
- return extract_markdown(code)
-
-
-def get_sql_options_from_cell(code: str) -> Optional[dict[str, str]]:
- # 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/_server/notebook/file_manager.py b/marimo/_server/notebook/file_manager.py
index 7261df2e4e..53874cf0ca 100644
--- a/marimo/_server/notebook/file_manager.py
+++ b/marimo/_server/notebook/file_manager.py
@@ -16,7 +16,7 @@ from marimo._runtime.layout.layout import (
read_layout_config,
save_layout_config,
)
-from marimo._schemas.serialization import NotebookSerializationV1
+from marimo._schemas.serialization import Header, NotebookSerializationV1
from marimo._server.api.status import HTTPException, HTTPStatus
from marimo._server.models.models import (
CopyNotebookRequest,
@@ -172,9 +172,24 @@ class AppFileManager:
"""
LOGGER.debug("Saving app to %s", path)
- # Get the appropriate format handler and serialize
+ # Get the header in case it was modified by the user (e.g. package installation)
handler = get_format_handler(path)
- contents = handler.serialize(notebook, path, previous_path)
+ header: Optional[str] = None
+ if previous_path and previous_path.exists():
+ header = handler.extract_header(previous_path)
+ elif path.exists():
+ header = handler.extract_header(path)
+
+ if header:
+ notebook = NotebookSerializationV1(
+ app=notebook.app,
+ header=Header(value=header) if header else notebook.header,
+ cells=notebook.cells,
+ violations=notebook.violations,
+ valid=notebook.valid,
+ filename=str(path),
+ )
+ contents = handler.serialize(notebook)
if persist:
self.storage.write(path, contents)
@@ -247,11 +262,7 @@ class AppFileManager:
self._assert_path_does_not_exist(new_path)
- needs_save = False
-
- if self.is_notebook_named and self._filename is not None:
- # Force a save after rename if filetype changed
- needs_save = self._filename.suffix != new_path.suffix
+ if self._filename is not None:
self.storage.rename(self._filename, new_path)
else:
# Create new file for unnamed notebooks
@@ -259,14 +270,14 @@ class AppFileManager:
previous_filename = self._filename
self._filename = new_path
+ self.app._app._filename = str(new_path)
- if needs_save:
- self._save_file(
- new_path,
- notebook=self.app.to_ir(),
- persist=True,
- previous_path=previous_filename,
- )
+ self._save_file(
+ new_path,
+ notebook=self.app.to_ir(),
+ persist=True,
+ previous_path=previous_filename,
+ )
return new_path.name
diff --git a/marimo/_server/notebook/serializer.py b/marimo/_server/notebook/serializer.py
index e7ab9e71d1..8a4a2035fd 100644
--- a/marimo/_server/notebook/serializer.py
+++ b/marimo/_server/notebook/serializer.py
@@ -3,25 +3,19 @@
from __future__ import annotations
-from abc import ABC, abstractmethod
from pathlib import Path
-from typing import Optional
+from typing import Optional, Protocol
from marimo._ast import codegen
from marimo._convert.converters import MarimoConvert
-from marimo._schemas.serialization import Header, NotebookSerializationV1
+from marimo._convert.markdown import convert_from_ir_to_markdown
+from marimo._schemas.serialization import NotebookSerializationV1
-class NotebookSerializer(ABC):
- """Abstract base class for notebook format handlers."""
+class NotebookSerializer(Protocol):
+ """Protocol for notebook format handlers."""
- @abstractmethod
- def serialize(
- self,
- notebook: NotebookSerializationV1,
- path: Path,
- previous_path: Optional[Path] = None,
- ) -> str:
+ def serialize(self, notebook: NotebookSerializationV1) -> str:
"""Convert notebook IR to the target format.
Args:
@@ -32,9 +26,8 @@ class NotebookSerializer(ABC):
Returns:
Serialized notebook content as string
"""
- pass
+ ...
- @abstractmethod
def extract_header(self, path: Path) -> Optional[str]:
"""Extract header/metadata from an existing file.
@@ -44,39 +37,18 @@ class NotebookSerializer(ABC):
Returns:
Header content or None
"""
- pass
+ ...
class PythonNotebookSerializer(NotebookSerializer):
"""Handler for Python (.py) notebook files."""
- def serialize(
- self,
- notebook: NotebookSerializationV1,
- path: Path,
- previous_path: Optional[Path] = None,
- ) -> str:
+ def serialize(self, notebook: NotebookSerializationV1) -> str:
"""Serialize notebook to Python format.
Handles header preservation when converting from other formats.
"""
- # Path changed, extract headers from previous path
- if previous_path is not None and path.suffix != previous_path.suffix:
- prev_header = get_format_handler(previous_path).extract_header(
- previous_path
- )
- header_comments = prev_header
- else:
- header_comments = self.extract_header(path)
-
- # Convert to Python with headers
- contents = MarimoConvert.from_ir(
- NotebookSerializationV1(
- app=notebook.app,
- cells=notebook.cells,
- header=Header(value=header_comments or ""),
- )
- ).to_py()
+ contents = MarimoConvert.from_ir(notebook).to_py()
return contents
@@ -88,22 +60,9 @@ class PythonNotebookSerializer(NotebookSerializer):
class MarkdownNotebookSerializer(NotebookSerializer):
"""Handler for Markdown (.md) notebook files."""
- def serialize(
- self,
- notebook: NotebookSerializationV1,
- path: Path,
- previous_path: Optional[Path] = None,
- ) -> str:
+ def serialize(self, notebook: NotebookSerializationV1) -> str:
"""Serialize notebook to Markdown format."""
- from marimo._server.export.exporter import Exporter
-
- contents, _ = Exporter().export_as_md(
- notebook,
- str(path),
- previous_path,
- )
-
- return contents
+ return convert_from_ir_to_markdown(notebook)
def extract_header(self, path: Path) -> Optional[str]:
"""Extract YAML frontmatter from Markdown file."""
diff --git a/tests/_convert/snapshots/has-header.md.txt b/tests/_convert/snapshots/has-header.md.txt
index 087e63561e..5c8284e069 100644
--- a/tests/_convert/snapshots/has-header.md.txt
+++ b/tests/_convert/snapshots/has-header.md.txt
@@ -3,8 +3,4 @@ title: Test Notebook
marimo-version: 0.0.0
header: |-
#!/usr/bin/env python
----
-
-```python {.marimo}
-
-```
\ No newline at end of file
+---
\ No newline at end of file
diff --git a/tests/_convert/test_convert_py.py b/tests/_convert/test_convert_py.py
index 747c4763f5..2b49fb908e 100644
--- a/tests/_convert/test_convert_py.py
+++ b/tests/_convert/test_convert_py.py
@@ -54,7 +54,7 @@ def test_basic_marimo_example_jupytext_compatibility():
# Test conversion to markdown (now works without file existing)
converted_to_md = MarimoConvert.from_py(marimo_script).to_markdown(
- "Test Notebook.py"
+ filename="Test Notebook.py"
)
snapshot("basic_marimo_example_to_md.txt", converted_to_md)
diff --git a/tests/_convert/test_convert_utils.py b/tests/_convert/test_convert_utils.py
index ea13347bbf..ff2bce57c1 100644
--- a/tests/_convert/test_convert_utils.py
+++ b/tests/_convert/test_convert_utils.py
@@ -5,6 +5,7 @@ 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
@@ -136,3 +137,28 @@ def test_markdown_with_quotes_and_cell_configs():
r"mo.md(\"Marimo is the best\")" in result
or "Marimo is the best" in result
)
+
+
+def test_get_markdown_from_cell_base():
+ empty_markdown_str = "mo.md('hello')"
+ markdown = utils.get_markdown_from_cell(
+ compile_cell(empty_markdown_str, "id"), empty_markdown_str
+ )
+ assert markdown == "hello"
+
+
+def test_get_markdown_from_cell_empty():
+ empty_markdown_str = "mo.md()"
+ markdown = utils.get_markdown_from_cell(
+ compile_cell(empty_markdown_str, "id"), empty_markdown_str
+ )
+ assert markdown is None
+
+
+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(
+ compile_cell(empty_markdown_str, "id"), "mo.md(f'{broken(}')"
+ )
+ assert markdown is None
diff --git a/tests/_convert/test_markdown_conversion.py b/tests/_convert/test_markdown_conversion.py
index 29d44594a2..571eb5af37 100644
--- a/tests/_convert/test_markdown_conversion.py
+++ b/tests/_convert/test_markdown_conversion.py
@@ -2,9 +2,7 @@
from __future__ import annotations
import os
-import re
import sys
-import tempfile
from textwrap import dedent
import pytest
@@ -14,7 +12,6 @@ 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._server.export.utils import format_filename_title
# Just a handful of scripts to test
from marimo._tutorials import dataflow, for_jupyter_users, sql
@@ -42,25 +39,8 @@ def sanitized_version(output: str) -> str:
return output.replace(__version__, "0.0.0")
-def convert_from_py(py: str) -> str:
- # Needs to be a .py for export to be invoked.
- tempfile_name = ""
- output = ""
- try:
- # in windows, can't re-open an open named temporary file, hence
- # delete=False and manual clean up
- with tempfile.NamedTemporaryFile(
- mode="w", suffix=".py", delete=False
- ) as f:
- tempfile_name = f.name
- f.write(py)
- f.seek(0)
- output = export_as_md(MarimoPath(tempfile_name)).contents
- finally:
- os.remove(tempfile_name)
-
- title = format_filename_title(tempfile_name)
- output = re.sub(rf"'?{title}'?", "Test Notebook", output)
+def convert_from_py_to_md(py: str) -> str:
+ output = MarimoConvert.from_py(py).to_markdown(filename="Test Notebook")
return sanitized_version(output)
@@ -85,7 +65,7 @@ def test_idempotent_markdown_to_marimo() -> None:
with open(DIR_PATH + f"/snapshots/{script}.md.txt") as f:
md = f.read()
python_source = sanitized_version(md_to_py(md))
- assert convert_from_py(python_source) == md.strip()
+ assert convert_from_py_to_md(python_source) == md.strip()
def test_markdown_frontmatter() -> None:
@@ -258,7 +238,7 @@ def test_python_to_md_header() -> None:
"""
)
)
- md = convert_from_py(script)
+ md = convert_from_py_to_md(script)
assert "#!/usr/bin/env python" in md
snapshot("has-header.md.txt", md)
@@ -348,13 +328,13 @@ def test_python_to_md_code_injection() -> None:
"""
)
)
- maybe_unsafe_md = convert_from_py(unsafe_app).strip()
+ maybe_unsafe_md = convert_from_py_to_md(unsafe_app).strip()
maybe_unsafe_py = md_to_py(maybe_unsafe_md).strip()
snapshot("unsafe-app.py.txt", maybe_unsafe_py)
snapshot("unsafe-app.md.txt", maybe_unsafe_md)
# Idempotent even under strange conditions.
- assert convert_from_py(maybe_unsafe_py).strip() == maybe_unsafe_md
+ assert convert_from_py_to_md(maybe_unsafe_py).strip() == maybe_unsafe_md
original_count = len(unsafe_app.split("@app.cell"))
count = len(maybe_unsafe_py.split("@app.cell"))
@@ -364,6 +344,92 @@ def test_python_to_md_code_injection() -> None:
)
+def test_md_to_python_code_injection() -> None:
+ script = dedent(
+ remove_empty_lines(
+ """
+ ---
+ title: "Casually malicious md"
+ ---
+
+ What happens if I just leave a \"""
+ " ' ! @ # $ % ^ & * ( ) + = - _ [ ] { } | \\ /
+
+ # Notebook
+ <!--
+ \\
+ ```python {.marimo}
+ print("Hello, World!")
+ ```
+ -->
+
+ ```marimo run convert document.md```
+
+ ```python {.marimo}
+ it's an unparsable cell
+ ```
+
+ <!-- Actually markdown -->
+ ```python {.marimo} `
+ print("Hello, World!")
+
+ <!-- Disabled code block -->
+ ```python {.marimo disabled="true"}
+ 1 + 1
+ ```
+
+ <!-- Hidden code block -->
+ ```python {.marimo hide_code="true"}
+ 1 + 1
+ ```
+
+ <!-- Empty code block -->
+ ```python {.marimo}
+ ```
+
+ <!-- Improperly nested code block -->
+ ```python {.marimo}
+ \"""
+ ```python {.marimo}
+ print("Hello, World!")
+ ```
+ \"""
+ ```
+
+ <!-- Improperly nested code block -->
+ ```python {.marimo}
+ ````python {.marimo}
+ print("Hello, World!")
+ ````
+ ```
+
+ -->
+
+ <!-- from the notebook, should remain unchanged -->
+ ````python {.marimo}
+ mo.md(\"""
+ This is a markdown cell with an execution block in it
+ ```python {.marimo}
+ # To ambiguous to convert
+ ```
+ \""")
+ ````
+
+ """
+ )
+ )
+
+ maybe_unsafe_py = sanitized_version(md_to_py(script).strip())
+ maybe_unsafe_md = convert_from_py_to_md(maybe_unsafe_py)
+
+ assert maybe_unsafe_py == sanitized_version(
+ md_to_py(maybe_unsafe_md).strip()
+ )
+
+ snapshot("unsafe-doc.py.txt", maybe_unsafe_py)
+ snapshot("unsafe-doc.md.txt", maybe_unsafe_md)
+
+
def remove_empty_lines(s: str) -> str:
"Just remove the first and last lines if they are empty"
lines = s.splitlines()
diff --git a/tests/_convert/test_markdown_module.py b/tests/_convert/test_markdown_module.py
new file mode 100644
index 0000000000..c22a7e9aad
--- /dev/null
+++ b/tests/_convert/test_markdown_module.py
@@ -0,0 +1,288 @@
+# Copyright 2024 Marimo. All rights reserved.
+from __future__ import annotations
+
+from marimo._ast.app import App, InternalApp
+from marimo._convert.markdown import (
+ _format_filename_title,
+ _get_sql_options_from_cell,
+ convert_from_ir_to_markdown,
+)
+
+
+def test_format_filename_title():
+ """Test that filenames are formatted correctly into titles."""
+ assert _format_filename_title("my_notebook.py") == "My Notebook"
+ assert _format_filename_title("my-notebook.md") == "My Notebook"
+ assert _format_filename_title("/path/to/my_notebook.py") == "My Notebook"
+ assert _format_filename_title("simple.py") == "Simple"
+ assert _format_filename_title("my-cool_notebook.md") == "My Cool Notebook"
+
+
+def test_get_sql_options_from_cell_basic():
+ """Test extraction of SQL options from basic SQL code."""
+ code = '_df = mo.sql("SELECT * FROM table")'
+ options = _get_sql_options_from_cell(code)
+ assert options is not None
+ assert options["query"] == "_df"
+
+
+def test_get_sql_options_from_cell_with_keywords():
+ """Test extraction of SQL options with keyword arguments."""
+ code = '_df = mo.sql("SELECT * FROM table", output=False)'
+ options = _get_sql_options_from_cell(code)
+ assert options is not None
+ assert options["query"] == "_df"
+ assert options["hide_output"] == "True"
+
+
+def test_get_sql_options_from_cell_with_engine():
+ """Test extraction of SQL options with engine parameter."""
+ code = 'result = mo.sql("SELECT * FROM table", engine="my_engine")'
+ options = _get_sql_options_from_cell(code)
+ assert options is not None
+ assert options["query"] == "result"
+ assert options["engine"] == "my_engine"
+
+
+def test_get_sql_options_from_cell_not_sql():
+ """Test that non-SQL code returns None."""
+ code = "x = 1 + 1"
+ options = _get_sql_options_from_cell(code)
+ assert options is None
+
+
+def test_get_sql_options_from_cell_invalid():
+ """Test that code without proper structure returns None."""
+ code = 'x = mo.other_method("SELECT * FROM table")'
+ options = _get_sql_options_from_cell(code)
+ assert options is None
+
+
+def test_get_sql_options_from_cell_not_assignment():
+ """Test that code without assignment returns None."""
+ code = 'mo.sql("SELECT * FROM table")'
+ options = _get_sql_options_from_cell(code)
+ assert options is None
+
+
+def test_convert_from_ir_to_markdown_empty():
+ """Test conversion of empty notebook to markdown."""
+ app = App()
+ internal_app = InternalApp(app)
+ notebook = internal_app.to_ir()
+
+ markdown = convert_from_ir_to_markdown(notebook)
+
+ # Should have frontmatter
+ assert markdown.startswith("---")
+ assert "marimo-version" in markdown
+
+
+def test_convert_from_ir_to_markdown_with_code():
+ """Test conversion of notebook with code cells to markdown."""
+ app = App()
+
+ @app.cell()
+ def test_cell():
+ x = 1
+ return (x,)
+
+ internal_app = InternalApp(app)
+ notebook = internal_app.to_ir()
+
+ markdown = convert_from_ir_to_markdown(notebook)
+
+ # Should have frontmatter
+ assert markdown.startswith("---")
+ assert "marimo-version" in markdown
+ # Should have code block
+ assert "```python" in markdown
+ assert "x = 1" in markdown
+
+
+def test_convert_from_ir_to_markdown_with_markdown_cell():
+ """Test conversion of notebook with markdown cells."""
+ app = App()
+
+ @app.cell()
+ def __():
+ import marimo as mo
+
+ return (mo,)
+
+ @app.cell()
+ def __(mo):
+ mo.md("# Hello World")
+ return
+
+ internal_app = InternalApp(app)
+ notebook = internal_app.to_ir()
+
+ markdown = convert_from_ir_to_markdown(notebook)
+
+ # Should have the markdown content
+ assert "# Hello World" in markdown
+
+
+def test_convert_from_ir_to_markdown_with_sql():
+ """Test conversion of notebook with SQL cells to markdown."""
+ app = App()
+
+ @app.cell()
+ def __():
+ import marimo as mo
+
+ return (mo,)
+
+ @app.cell()
+ def __(mo):
+ _df = mo.sql("SELECT * FROM my_table")
+ return (_df,)
+
+ internal_app = InternalApp(app)
+ notebook = internal_app.to_ir()
+
+ markdown = convert_from_ir_to_markdown(notebook)
+
+ # Should have SQL code block
+ assert "```sql" in markdown or "```python" in markdown
+ assert "SELECT * FROM my_table" in markdown
+
+
+def test_convert_from_ir_to_markdown_preserves_cell_names():
+ """Test that cell names are preserved in markdown."""
+ app = App()
+
+ @app.cell()
+ def my_cell():
+ x = 1
+ return (x,)
+
+ internal_app = InternalApp(app)
+ notebook = internal_app.to_ir()
+
+ markdown = convert_from_ir_to_markdown(notebook)
+
+ # Should have cell name in attributes
+ assert "name=my_cell" in markdown or 'name="my_cell"' in markdown
+
+
+def test_convert_from_ir_to_markdown_with_cell_config():
+ """Test that cell configurations are preserved."""
+ app = App()
+
+ @app.cell(hide_code=True)
+ def __():
+ x = 1
+ return (x,)
+
+ internal_app = InternalApp(app)
+ notebook = internal_app.to_ir()
+
+ markdown = convert_from_ir_to_markdown(notebook)
+
+ # Should have hide_code in attributes
+ assert 'hide_code="true"' in markdown or "hide_code=true" in markdown
+
+
+def test_convert_from_ir_to_markdown_with_app_title():
+ """Test that app title is preserved in frontmatter."""
+ app = App(app_title="My Test App")
+
+ @app.cell()
+ def __():
+ return
+
+ internal_app = InternalApp(app)
+ notebook = internal_app.to_ir()
+
+ markdown = convert_from_ir_to_markdown(notebook)
+
+ # Should have title in frontmatter
+ assert "title: My Test App" in markdown
+
+
+def test_convert_from_ir_to_markdown_consecutive_markdown_cells():
+ """Test that consecutive markdown cells are separated with HTML comments."""
+ app = App()
+
+ @app.cell()
+ def __():
+ import marimo as mo
+
+ return (mo,)
+
+ @app.cell()
+ def __(mo):
+ mo.md("# First markdown")
+ return
+
+ @app.cell()
+ def __(mo):
+ mo.md("# Second markdown")
+ return
+
+ internal_app = InternalApp(app)
+ notebook = internal_app.to_ir()
+
+ markdown = convert_from_ir_to_markdown(notebook)
+
+ # Should have HTML comment separator between markdown cells
+ assert "<!---->" in markdown
+ assert "# First markdown" in markdown
+ assert "# Second markdown" in markdown
+
+
+def test_convert_from_ir_to_markdown_with_column():
+ """Test that cells with column configuration force markdown to code."""
+ app = App()
+
+ @app.cell()
+ def __():
+ import marimo as mo
+
+ return (mo,)
+
+ @app.cell(column=1)
+ def __(mo):
+ mo.md("# Markdown in column")
+ return
+
+ internal_app = InternalApp(app)
+ notebook = internal_app.to_ir()
+
+ markdown = convert_from_ir_to_markdown(notebook)
+
+ # Markdown should be forced to code block due to column
+ assert "```python" in markdown
+ assert "column=1" in markdown or 'column="1"' in markdown
+
+
+def test_convert_from_ir_to_markdown_unparsable_cell():
+ """Test that unparsable cells are marked with unparsable attribute."""
+ from marimo._schemas.serialization import (
+ AppInstantiation,
+ CellDef,
+ NotebookSerializationV1,
+ )
+
+ # Create a notebook with an unparsable cell by directly constructing the IR
+ notebook = NotebookSerializationV1(
+ app=AppInstantiation(options={}),
+ cells=[
+ CellDef(
+ name="__",
+ code="this is { not valid python",
+ options={},
+ )
+ ],
+ violations=[],
+ valid=True,
+ filename="notebook.py",
+ )
+
+ markdown = convert_from_ir_to_markdown(notebook)
+
+ # Should mark as unparsable
+ assert 'unparsable="true"' in markdown or "unparsable=true" in markdown
+ assert "this is { not valid python" in markdown
diff --git a/tests/_server/api/endpoints/test_export.py b/tests/_server/api/endpoints/test_export.py
index 225e3dc3ae..9ba21d585a 100644
--- a/tests/_server/api/endpoints/test_export.py
+++ b/tests/_server/api/endpoints/test_export.py
@@ -2,6 +2,7 @@
from __future__ import annotations
import os
+import re
import shutil
import time
from pathlib import Path
@@ -177,6 +178,12 @@ def test_export_markdown(client: TestClient) -> None:
assert response.status_code == 200
assert f"marimo-version: {__version__}" in response.text
assert "```python {.marimo}" in response.text
+ # Check that the Content-Disposition header has the correct .md extension
+ assert "Content-Disposition" in response.headers
+ # The temp file has .py extension, should be converted to .md
+ assert re.match(
+ r"filename=.*\.md", response.headers["Content-Disposition"]
+ )
@with_read_session(SESSION_ID)
diff --git a/tests/_server/export/test_export_utils.py b/tests/_server/export/test_export_utils.py
deleted file mode 100644
index fc8531f2de..0000000000
--- a/tests/_server/export/test_export_utils.py
+++ /dev/null
@@ -1,27 +0,0 @@
-from marimo._ast.compiler import compile_cell
-from marimo._server.export.utils import get_markdown_from_cell
-
-
-def test_extract_markdown_base():
- empty_markdown_str = "mo.md('hello')"
- markdown = get_markdown_from_cell(
- compile_cell(empty_markdown_str, "id"), empty_markdown_str
- )
- assert markdown == "hello"
-
-
-def test_extract_markdown_empty():
- empty_markdown_str = "mo.md()"
- markdown = get_markdown_from_cell(
- compile_cell(empty_markdown_str, "id"), empty_markdown_str
- )
- assert markdown is None
-
-
-def test_extract_markdown_broken():
- empty_markdown_str = "mo.md()"
- # This can occur because the cell isn't recompiled at this point.
- markdown = 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_exporter.py b/tests/_server/export/test_exporter.py
index 1c5a8b1c4c..2fc012c717 100644
--- a/tests/_server/export/test_exporter.py
+++ b/tests/_server/export/test_exporter.py
@@ -40,10 +40,7 @@ def test_export_ipynb_empty():
internal_app = InternalApp(app)
exporter = Exporter()
- content, filename = exporter.export_as_ipynb(
- internal_app, None, sort_mode="top-down"
- )
- assert filename == "notebook.ipynb"
+ content = exporter.export_as_ipynb(internal_app, sort_mode="top-down")
snapshot("empty_notebook.ipynb.txt", content)
@@ -58,10 +55,7 @@ def test_export_ipynb_with_cells():
internal_app = InternalApp(app)
exporter = Exporter()
- content, filename = exporter.export_as_ipynb(
- internal_app, None, sort_mode="top-down"
- )
- assert filename == "notebook.ipynb"
+ content = exporter.export_as_ipynb(internal_app, sort_mode="top-down")
snapshot("notebook_with_cells.ipynb.txt", content)
@@ -88,15 +82,11 @@ def test_export_ipynb_sort_modes():
exporter = Exporter()
# Test top-down mode preserves document order
- content, _ = exporter.export_as_ipynb(
- internal_app, None, sort_mode="top-down"
- )
+ content = exporter.export_as_ipynb(internal_app, sort_mode="top-down")
snapshot("notebook_top_down.ipynb.txt", content)
# Test topological mode respects dependencies
- content, _ = exporter.export_as_ipynb(
- internal_app, None, sort_mode="topological"
- )
+ content = exporter.export_as_ipynb(internal_app, sort_mode="topological")
snapshot("notebook_topological.ipynb.txt", content)
@@ -225,10 +215,9 @@ async def test_export_ipynb_with_outputs(tmp_path: Path):
internal_app = InternalApp(app)
exporter = Exporter()
- content, filename = exporter.export_as_ipynb(
- internal_app, None, sort_mode="top-down", session_view=None
+ content = exporter.export_as_ipynb(
+ internal_app, sort_mode="top-down", session_view=None
)
- assert filename == "notebook.ipynb"
assert content is not None
test_file = tmp_path / "notebook.py"
diff --git a/tests/_server/notebook/test_serializer.py b/tests/_server/notebook/test_serializer.py
index bf9137b74d..7acd099404 100644
--- a/tests/_server/notebook/test_serializer.py
+++ b/tests/_server/notebook/test_serializer.py
@@ -8,6 +8,7 @@ import pytest
from marimo._schemas.serialization import (
AppInstantiation,
CellDef,
+ Header,
NotebookSerializationV1,
)
from marimo._server.notebook.serializer import (
@@ -19,7 +20,7 @@ from marimo._server.notebook.serializer import (
class TestPythonNotebookSerializer:
- def test_serialize_basic(self, tmp_path: Path) -> None:
+ def test_serialize_basic(self) -> None:
serializer = PythonNotebookSerializer()
notebook = NotebookSerializationV1(
app=AppInstantiation(),
@@ -30,26 +31,19 @@ class TestPythonNotebookSerializer:
)
],
)
- path = tmp_path / "notebook.py"
- result = serializer.serialize(notebook, path)
+ result = serializer.serialize(notebook)
assert "import marimo" in result
assert "x = 1" in result
- def test_serialize_with_existing_header(self, tmp_path: Path) -> None:
- """Test that headers are preserved from existing file."""
+ def test_serialize_with_header(self) -> None:
+ """Test that headers are included in output when provided in notebook."""
serializer = PythonNotebookSerializer()
- # Create existing file with header
- path = tmp_path / "notebook.py"
- path.write_text(
- "# Header comment\n# Another line\nimport marimo\n",
- encoding="utf-8",
- )
-
notebook = NotebookSerializationV1(
app=AppInstantiation(),
+ header=Header(value="# Header comment\n# Another line"),
cells=[
CellDef(
name="cell1",
@@ -58,7 +52,7 @@ class TestPythonNotebookSerializer:
],
)
- result = serializer.serialize(notebook, path)
+ result = serializer.serialize(notebook)
assert "# Header comment" in result
assert "import marimo" in result
@@ -86,17 +80,10 @@ class TestPythonNotebookSerializer:
# No header comments should return None or empty
assert header is None or header == ""
- def test_serialize_format_conversion(self, tmp_path: Path) -> None:
- """Test serializing when converting from one format to another."""
+ def test_serialize_without_header(self) -> None:
+ """Test serializing without a header."""
serializer = PythonNotebookSerializer()
- # Create a markdown file with header
- md_file = tmp_path / "notebook.md"
- md_file.write_text(
- "---\nmarimo-version: 0.1.0\n---\n# Content\n",
- encoding="utf-8",
- )
-
notebook = NotebookSerializationV1(
app=AppInstantiation(),
cells=[
@@ -107,15 +94,15 @@ class TestPythonNotebookSerializer:
],
)
- py_file = tmp_path / "notebook.py"
- result = serializer.serialize(notebook, py_file, previous_path=md_file)
+ result = serializer.serialize(notebook)
- # Should extract header from markdown file
+ # Should have import but no custom header
assert "import marimo" in result
+ assert "x = 1" in result
class TestMarkdownNotebookSerializer:
- def test_serialize_basic(self, tmp_path: Path) -> None:
+ def test_serialize_basic(self) -> None:
serializer = MarkdownNotebookSerializer()
notebook = NotebookSerializationV1(
app=AppInstantiation(),
@@ -126,9 +113,8 @@ class TestMarkdownNotebookSerializer:
)
],
)
- path = tmp_path / "notebook.md"
- result = serializer.serialize(notebook, path)
+ result = serializer.serialize(notebook)
assert "```python" in result
assert "print('hello')" in result
diff --git a/tests/_server/test_file_manager.py b/tests/_server/test_file_manager.py
index e35f13024c..bb4a3f1b14 100644
--- a/tests/_server/test_file_manager.py
+++ b/tests/_server/test_file_manager.py
@@ -130,16 +130,16 @@ def test_rename_create_new_directory_file(
def test_rename_different_filetype(app_file_manager: AppFileManager) -> None:
initial_filename = app_file_manager.filename
assert initial_filename
- assert str(initial_filename).endswith(".py")
- with open(str(initial_filename)) as f:
+ assert initial_filename.endswith(".py")
+ with open(initial_filename) as f:
contents = f.read()
assert "app = marimo.App()" in contents
assert "marimo-version" not in contents
app_file_manager.rename(str(initial_filename)[:-3] + ".md")
next_filename = app_file_manager.filename
assert next_filename
- assert str(next_filename).endswith(".md")
- with open(str(next_filename)) as f:
+ assert next_filename.endswith(".md")
+ with open(next_filename) as f:
contents = f.read()
assert "marimo-version" in contents
assert "app = marimo.App()" not in contents
@@ -148,8 +148,8 @@ def test_rename_different_filetype(app_file_manager: AppFileManager) -> None:
def test_rename_to_qmd(app_file_manager: AppFileManager) -> None:
initial_filename = app_file_manager.filename
assert initial_filename
- assert str(initial_filename).endswith(".py")
- with open(str(initial_filename)) as f:
+ assert initial_filename.endswith(".py")
+ with open(initial_filename) as f:
contents = f.read()
assert "app = marimo.App()" in contents
assert "marimo-team/marimo" not in contents
@@ -157,8 +157,8 @@ def test_rename_to_qmd(app_file_manager: AppFileManager) -> None:
app_file_manager.rename(str(initial_filename)[:-3] + ".qmd")
next_filename = app_file_manager.filename
assert next_filename
- assert str(next_filename).endswith(".qmd")
- with open(str(next_filename)) as f:
+ assert next_filename.endswith(".qmd")
+ with open(next_filename) as f:
contents = f.read()
assert "marimo-version" in contents
assert "filters:" in contents