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.

Move marimo import to top in convert command (#7828)

vincent d warmerdam799c073775fb
diff --git a/marimo/_convert/ipynb/to_ir.py b/marimo/_convert/ipynb/to_ir.py
index 462bac90d0..4af7fd81a2 100644
--- a/marimo/_convert/ipynb/to_ir.py
+++ b/marimo/_convert/ipynb/to_ir.py
@@ -119,7 +119,7 @@ def transform_add_marimo_import(sources: list[CodeCell]) -> list[CodeCell]:
return sources
if any(contains_mo(cell.source) for cell in sources):
- return sources + [CodeCell("import marimo as mo")]
+ return [CodeCell("import marimo as mo")] + sources
return sources
diff --git a/tests/_convert/ipynb/snapshots/converted_comments_preservation.py.txt b/tests/_convert/ipynb/snapshots/converted_comments_preservation.py.txt
index 780d7940ad..1e144702f6 100644
--- a/tests/_convert/ipynb/snapshots/converted_comments_preservation.py.txt
+++ b/tests/_convert/ipynb/snapshots/converted_comments_preservation.py.txt
@@ -3,6 +3,12 @@ import marimo
app = marimo.App()
+def _():
+ import marimo as mo
+ return (mo,)
+
+
@app.cell
def _():
# Cell 1: Basic inline and line comments
@@ -99,11 +105,5 @@ def _():
return
-def _():
- import marimo as mo
- return (mo,)
-
-
if __name__ == "__main__":
app.run()
diff --git a/tests/_convert/ipynb/snapshots/converted_hides_markdown_cells.py.txt b/tests/_convert/ipynb/snapshots/converted_hides_markdown_cells.py.txt
index 911f0ed74a..1fb40fa708 100644
--- a/tests/_convert/ipynb/snapshots/converted_hides_markdown_cells.py.txt
+++ b/tests/_convert/ipynb/snapshots/converted_hides_markdown_cells.py.txt
@@ -3,6 +3,12 @@ import marimo
app = marimo.App()
+def _():
+ import marimo as mo
+ return (mo,)
+
+
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
@@ -29,11 +35,5 @@ def _(mo):
return
-def _():
- import marimo as mo
- return (mo,)
-
-
if __name__ == "__main__":
app.run()
diff --git a/tests/_convert/ipynb/test_ipynb_to_ir.py b/tests/_convert/ipynb/test_ipynb_to_ir.py
index a17d42b091..76dd020226 100644
--- a/tests/_convert/ipynb/test_ipynb_to_ir.py
+++ b/tests/_convert/ipynb/test_ipynb_to_ir.py
@@ -142,7 +142,7 @@ def test_transform_add_marimo_import():
"print('World')",
]
result = strip_cells(transform_add_marimo_import)(sources)
- assert "import marimo as mo" in result
+ assert result == ["import marimo as mo"] + sources
# mo.sql
sources = [
@@ -150,7 +150,7 @@ def test_transform_add_marimo_import():
"print('World')",
]
result = strip_cells(transform_add_marimo_import)(sources)
- assert "import marimo as mo" in result
+ assert result == ["import marimo as mo"] + sources
# if `import marimo as mo` is already present
# it should not be added again
@@ -183,7 +183,7 @@ def test_transform_add_marimo_import_already_but_in_comment_or_definition():
"# import marimo as mo",
]
result = strip_cells(transform_add_marimo_import)(sources)
- assert result == sources + ["import marimo as mo"]
+ assert result == ["import marimo as mo"] + sources
# Definition
sources = [
@@ -191,7 +191,7 @@ def test_transform_add_marimo_import_already_but_in_comment_or_definition():
"def foo():\n import marimo as mo",
]
result = strip_cells(transform_add_marimo_import)(sources)
- assert result == sources + ["import marimo as mo"]
+ assert result == ["import marimo as mo"] + sources
def test_transform_magic_commands():
diff --git a/tests/_convert/snapshots/pypercent_format.py.txt b/tests/_convert/snapshots/pypercent_format.py.txt
index 532ed2ee7f..85f6d7e41d 100644
--- a/tests/_convert/snapshots/pypercent_format.py.txt
+++ b/tests/_convert/snapshots/pypercent_format.py.txt
@@ -4,6 +4,12 @@ __generated_with = "0.0.0"
app = marimo.App()
+def _():
+ import marimo as mo
+ return (mo,)
+
+
@app.cell
def _():
"""Pypercent format notebook."""
@@ -45,11 +51,5 @@ def _(x):
return
-def _():
- import marimo as mo
- return (mo,)
-
-
if __name__ == "__main__":
app.run()
diff --git a/tests/_convert/snapshots/pypercent_markdown_only.py.txt b/tests/_convert/snapshots/pypercent_markdown_only.py.txt
index aba2e90dd2..b22ba22e4a 100644
--- a/tests/_convert/snapshots/pypercent_markdown_only.py.txt
+++ b/tests/_convert/snapshots/pypercent_markdown_only.py.txt
@@ -4,6 +4,12 @@ __generated_with = "0.0.0"
app = marimo.App()
+def _():
+ import marimo as mo
+ return (mo,)
+
+
@app.cell
def _():
"""Documentation in pypercent format."""
@@ -28,11 +34,5 @@ def _(mo):
return
-def _():
- import marimo as mo
- return (mo,)
-
-
if __name__ == "__main__":
app.run()