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.

improvement: hide_code for markdown when converting from ipynb (#3417)

Myles Scolnick578e19e66cc9
diff --git a/marimo/_convert/ipynb.py b/marimo/_convert/ipynb.py
index b91287fb3d..232e4bb9f5 100644
--- a/marimo/_convert/ipynb.py
+++ b/marimo/_convert/ipynb.py
@@ -8,6 +8,7 @@ import sys
from collections import defaultdict
from typing import Any, Callable, Dict, List, Union
+from marimo._ast.cell import CellConfig
from marimo._ast.compiler import compile_cell
from marimo._ast.transformers import NameTransformer
from marimo._ast.visitor import Block, NamedNode, ScopedVisitor
@@ -641,9 +642,11 @@ def _transform_sources(
def convert_from_ipynb(raw_notebook: str) -> str:
notebook = json.loads(raw_notebook)
- sources: List[str] = []
- metadata: List[Dict[str, Any]] = []
+ sources: list[str] = []
+ metadata: list[Dict[str, Any]] = []
+ cell_configs: list[CellConfig] = []
inline_meta: Union[str, None] = None
+ md_cells: set[str] = set()
for cell in notebook["cells"]:
source = (
@@ -651,8 +654,10 @@ def convert_from_ipynb(raw_notebook: str) -> str:
if isinstance(cell["source"], list)
else cell["source"]
)
- if cell["cell_type"] == "markdown":
+ is_markdown: bool = cell["cell_type"] == "markdown"
+ if is_markdown:
source = markdown_to_marimo(source)
+ md_cells.add(source)
elif inline_meta is None:
# Eagerly find PEP 723 metadata, first match wins
inline_meta, source = extract_inline_meta(source)
@@ -661,6 +666,16 @@ def convert_from_ipynb(raw_notebook: str) -> str:
sources.append(source)
metadata.append(cell.get("metadata", {}))
+ transformed_sources = _transform_sources(sources, metadata)
+
+ # Cell configs must come after _transform_sources since this may add/remove cells
+ cell_configs = [
+ CellConfig(hide_code=source in md_cells)
+ for source in transformed_sources
+ ]
+
return generate_from_sources(
- _transform_sources(sources, metadata), header_comments=inline_meta
+ sources=transformed_sources,
+ header_comments=inline_meta,
+ cell_configs=cell_configs,
)
diff --git a/marimo/_convert/utils.py b/marimo/_convert/utils.py
index be3d2156c6..ce25e71f69 100644
--- a/marimo/_convert/utils.py
+++ b/marimo/_convert/utils.py
@@ -24,18 +24,27 @@ def markdown_to_marimo(source: str) -> str:
def generate_from_sources(
+ *,
sources: list[str],
config: Optional[_AppConfig] = None,
header_comments: Optional[str] = None,
+ cell_configs: Optional[list[CellConfig]] = None,
) -> str:
"""
Given a list of Python source code,
generate the marimo file contents.
"""
+ if cell_configs is None:
+ cell_configs = [CellConfig() for _ in range(len(sources))]
+ else:
+ assert len(cell_configs) == len(
+ sources
+ ), "cell_configs must be the same length as sources"
+
return codegen.generate_filecontents(
sources,
[DEFAULT_CELL_NAME for _ in sources],
- [CellConfig() for _ in range(len(sources))],
+ cell_configs,
config=config,
header_comments=header_comments,
)
diff --git a/tests/_cli/snapshots/converted_markdown.py.txt b/tests/_cli/snapshots/converted_markdown.py.txt
index dceab66391..28f2d53fc6 100644
--- a/tests/_cli/snapshots/converted_markdown.py.txt
+++ b/tests/_cli/snapshots/converted_markdown.py.txt
@@ -3,7 +3,7 @@ import marimo
app = marimo.App()
[email protected](hide_code=True)
def _(mo):
mo.md(
r"""
@@ -18,7 +18,7 @@ def _(mo):
return
[email protected](hide_code=True)
def _(mo):
mo.md(
r"""
diff --git a/tests/_convert/test_convert_utils.py b/tests/_convert/test_convert_utils.py
index edd903b5af..fcb724edb2 100644
--- a/tests/_convert/test_convert_utils.py
+++ b/tests/_convert/test_convert_utils.py
@@ -3,6 +3,7 @@ from __future__ import annotations
import re
from textwrap import dedent
+from marimo._ast.cell import CellConfig
from marimo._convert import utils
@@ -25,7 +26,7 @@ def test_markdown_to_marimo():
def test_generate_from_sources():
# Test with basic sources
sources = ["print('Hello')", "x = 5"]
- result = utils.generate_from_sources(sources)
+ result = utils.generate_from_sources(sources=sources)
result = re.sub(r"__generated_with = .*", "", result)
assert result == dedent(
@@ -52,3 +53,36 @@ if __name__ == "__main__":
app.run()
""".lstrip()
)
+
+
+def test_generate_from_sources_with_cell_configs():
+ sources = ["print('Hello')", "x = 5"]
+ cell_configs = [CellConfig(hide_code=True), CellConfig(hide_code=False)]
+ result = utils.generate_from_sources(
+ sources=sources, cell_configs=cell_configs
+ )
+ result = re.sub(r"__generated_with = .*", "", result)
+ assert result == dedent(
+ """
+import marimo
+
+
+app = marimo.App()
+
+
[email protected](hide_code=True)
+def _():
+ print('Hello')
+ return
+
+
+def _():
+ x = 5
+ return (x,)
+
+
+if __name__ == "__main__":
+ app.run()
+ """.lstrip()
+ )