A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: whitespace discrepancies on round trip conversions (#6521)
Dylan Madisettibbcc4d87f29a
diff --git a/frontend/src/core/codemirror/language/languages/markdown.ts b/frontend/src/core/codemirror/language/languages/markdown.tsindex 5d423c93be..c3a9d5da51 100644--- a/frontend/src/core/codemirror/language/languages/markdown.ts+++ b/frontend/src/core/codemirror/language/languages/markdown.ts@@ -103,6 +103,9 @@ export class MarkdownLanguageAdaptercode: string,metadata: MarkdownLanguageAdapterMetadata,): [string, number] {+ // NB. Must be kept consistent with marimo/_convert/utils.py+ // ::markdown_to_marimo+// Empty stringif (code === "") {// Need at least a space, otherwise the output will be 6 quotesdiff --git a/marimo/_convert/markdown/markdown.py b/marimo/_convert/markdown/markdown.pyindex d53e7ed371..791f1443d7 100644--- a/marimo/_convert/markdown/markdown.py+++ b/marimo/_convert/markdown/markdown.py@@ -247,7 +247,7 @@ def _tree_to_ir(root: Element) -> SafeWrap[NotebookSerializationV1]:header = root.get("header", None)pyproject = root.get("pyproject", None)if pyproject and not header:- header = "\n# ".join(["# ///script", *pyproject.splitlines(), "///"])+ header = "\n# ".join(["# /// script", *pyproject.splitlines(), "///"])notebook = NotebookSerializationV1(app=AppInstantiation(options=app_config),cells=[diff --git a/marimo/_convert/utils.py b/marimo/_convert/utils.pyindex 3033af6987..781370f95a 100644--- a/marimo/_convert/utils.py+++ b/marimo/_convert/utils.py@@ -5,14 +5,25 @@ from marimo._ast import codegendef markdown_to_marimo(source: str) -> str:+ # NB. This should be kept in sync with the logic in+ # frontend/src/core/codemirror/language/languages/markdown.ts+ # ::transformOutsource = source.replace('"""', '\\"\\"\\"')++ # 6 quotes in a row breaks+ if not source:+ source = " "++ if "\n" not in source:+ return f'mo.md(r"""{source}""")'+return "\n".join(["mo.md(",# r-string: a backslash is just a backslash!codegen.indent_text('r"""'),- codegen.indent_text(source),- codegen.indent_text('"""'),+ source,+ '"""',")",])diff --git a/marimo/_server/export/exporter.py b/marimo/_server/export/exporter.pyindex 6515f5135c..5c3623505f 100644--- a/marimo/_server/export/exporter.py+++ b/marimo/_server/export/exporter.py@@ -291,7 +291,7 @@ class Exporter:metadata["header"] = header.strip()else:header_file = previous if previous else filename- if header_file:+ 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)diff --git a/tests/_cli/snapshots/markdown_to_marimo.txt b/tests/_cli/snapshots/markdown_to_marimo.txtindex 7f7a29a050..c2b118c4c7 100644--- a/tests/_cli/snapshots/markdown_to_marimo.txt+++ b/tests/_cli/snapshots/markdown_to_marimo.txt@@ -8,10 +8,10 @@ app = marimo.App()def _(mo):mo.md(r"""- # Test Markdown+ # Test Markdown- print('Hello from Markdown!')- """+ print('Hello from Markdown!')+ """)returndiff --git a/tests/_cli/snapshots/remote_markdown_to_marimo.txt b/tests/_cli/snapshots/remote_markdown_to_marimo.txtindex 74d88bff82..56cd5739e9 100644--- a/tests/_cli/snapshots/remote_markdown_to_marimo.txt+++ b/tests/_cli/snapshots/remote_markdown_to_marimo.txt@@ -8,12 +8,12 @@ app = marimo.App()def _(mo):mo.md(r"""- # Remote Markdown Test+ # Remote Markdown Test- ```python- print('Hello from Remote Markdown!')- ```- """+ ```python+ print('Hello from Remote Markdown!')+ ```+ """)returndiff --git a/tests/_convert/snapshots/basic_marimo_example.py.txt b/tests/_convert/snapshots/basic_marimo_example.py.txtnew file mode 100644index 0000000000..c420262568--- /dev/null+++ b/tests/_convert/snapshots/basic_marimo_example.py.txt@@ -0,0 +1,33 @@+import marimo++__generated_with = "0.15.2"+app = marimo.App(width="medium")++[email protected](hide_code=True)+def _(mo):+ mo.md(r"""This is a simple marimo notebook""")+ return+++def _():+ x = 1+ return (x,)+++def _(x):+ y = x+1+ y+ return+++def _():+ import marimo as mo+ return (mo,)+++if __name__ == "__main__":+ app.run()\ No newline at end of filediff --git a/tests/_convert/snapshots/basic_marimo_example_roundtrip.py.txt b/tests/_convert/snapshots/basic_marimo_example_roundtrip.py.txtnew file mode 100644index 0000000000..ca05e922ff--- /dev/null+++ b/tests/_convert/snapshots/basic_marimo_example_roundtrip.py.txt@@ -0,0 +1,33 @@+import marimo++__generated_with = "0.0.0"+app = marimo.App(width="medium", app_title="Test Notebook")++[email protected](hide_code=True)+def _(mo):+ mo.md(r"""This is a simple marimo notebook""")+ return+++def _():+ x = 1+ return (x,)+++def _(x):+ y = x+1+ y+ return+++def _():+ import marimo as mo+ return (mo,)+++if __name__ == "__main__":+ app.run()diff --git a/tests/_convert/snapshots/basic_marimo_example_to_md.txt b/tests/_convert/snapshots/basic_marimo_example_to_md.txtnew file mode 100644index 0000000000..598723b93a--- /dev/null+++ b/tests/_convert/snapshots/basic_marimo_example_to_md.txt@@ -0,0 +1,20 @@+---+title: Test Notebook+marimo-version: 0.0.0+width: medium+---++This is a simple marimo notebook++```python {.marimo}+x = 1+```++```python {.marimo}+y = x+1+y+```++```python {.marimo}+import marimo as mo+```\ No newline at end of filediff --git a/tests/_convert/snapshots/convert_hides_markdown_cells.py.txt b/tests/_convert/snapshots/convert_hides_markdown_cells.py.txtindex 8071efd075..536f19a9a1 100644--- a/tests/_convert/snapshots/convert_hides_markdown_cells.py.txt+++ b/tests/_convert/snapshots/convert_hides_markdown_cells.py.txt@@ -5,33 +5,21 @@ app = marimo.App()@app.cell(hide_code=True)def _(mo):- mo.md(- r"""- A markdown cell.- """- )+ mo.md(r"""A markdown cell.""")return@app.cell(hide_code=True)def _(mo):# Cell tags: blah- mo.md(- r"""- A markdown cell with tags: ['blah'].- """- )+ mo.md(r"""A markdown cell with tags: ['blah'].""")return@app.cell(hide_code=True)def _(mo):# Cell tags: blah- mo.md(- r"""- A markdown cell with tags: ['blah', 'hide-cell'].- """- )+ mo.md(r"""A markdown cell with tags: ['blah', 'hide-cell'].""")returndiff --git a/tests/_convert/snapshots/frontmatter-test.py.txt b/tests/_convert/snapshots/frontmatter-test.py.txtindex 6f93ed8d66..bb2785e617 100644--- a/tests/_convert/snapshots/frontmatter-test.py.txt+++ b/tests/_convert/snapshots/frontmatter-test.py.txt@@ -9,11 +9,7 @@ app = marimo.App(app_title="My Title")@app.cell(hide_code=True)def _(mo):- mo.md(- r"""- # Notebook- """- )+ mo.md(r"""# Notebook""")returndiff --git a/tests/_convert/snapshots/no-frontmatter.py.txt b/tests/_convert/snapshots/no-frontmatter.py.txtindex 00297ae7c9..2aeae204c4 100644--- a/tests/_convert/snapshots/no-frontmatter.py.txt+++ b/tests/_convert/snapshots/no-frontmatter.py.txt@@ -6,11 +6,7 @@ app = marimo.App()@app.cell(hide_code=True)def _(mo):- mo.md(- r"""- # My Notebook- """- )+ mo.md(r"""# My Notebook""")return@@ -24,9 +20,9 @@ def _():def _(mo):mo.md(r"""- **Appendix**- - This is the end of the notebook- """+ **Appendix**+ - This is the end of the notebook+ """)returndiff --git a/tests/_convert/snapshots/pypercent_format.py.txt b/tests/_convert/snapshots/pypercent_format.py.txtindex 8853c44e8e..083dbe410a 100644--- a/tests/_convert/snapshots/pypercent_format.py.txt+++ b/tests/_convert/snapshots/pypercent_format.py.txt@@ -16,20 +16,16 @@ def _():def _(mo):mo.md(r"""- This is a markdown cell- with multiple lines- """+ This is a markdown cell+ with multiple lines+ """)return@app.cell(hide_code=True)def _(mo):- mo.md(- r"""- This is a doc string, but also markdown- """- )+ mo.md(r"""This is a doc string, but also markdown""")returndiff --git a/tests/_convert/snapshots/pypercent_markdown_only.py.txt b/tests/_convert/snapshots/pypercent_markdown_only.py.txtindex f7fcaadd82..f419e15935 100644--- a/tests/_convert/snapshots/pypercent_markdown_only.py.txt+++ b/tests/_convert/snapshots/pypercent_markdown_only.py.txt@@ -14,9 +14,9 @@ def _():def _(mo):mo.md(r"""- Introduction- This is a documentation file.- """+ Introduction+ This is a documentation file.+ """)return@@ -25,9 +25,9 @@ def _(mo):def _(mo):mo.md(r"""- Usage- Here's how to use this module.- """+ Usage+ Here's how to use this module.+ """)returndiff --git a/tests/_convert/snapshots/sql-notebook.py.txt b/tests/_convert/snapshots/sql-notebook.py.txtindex 3c9acf931b..5b37f069f1 100644--- a/tests/_convert/snapshots/sql-notebook.py.txt+++ b/tests/_convert/snapshots/sql-notebook.py.txt@@ -6,11 +6,7 @@ app = marimo.App(app_title="My Title")@app.cell(hide_code=True)def _(mo):- mo.md(- r"""- # SQL notebook- """- )+ mo.md(r"""# SQL notebook""")returndiff --git a/tests/_convert/snapshots/unsafe-app.py.txt b/tests/_convert/snapshots/unsafe-app.py.txtindex 774cfc76ee..3673bb1509 100644--- a/tests/_convert/snapshots/unsafe-app.py.txt+++ b/tests/_convert/snapshots/unsafe-app.py.txt@@ -52,12 +52,12 @@ def _(mo):def _(mo):mo.md(r"""- Nested fence- ````text- The guards are- ```python {.marimo}- ````- """+ Nested fence+ ````text+ The guards are+ ```python {.marimo}+ ````+ """)returndiff --git a/tests/_convert/snapshots/unsafe-doc-old.md.txt b/tests/_convert/snapshots/unsafe-doc-old.md.txtindex 2990574346..52b396acf0 100644--- a/tests/_convert/snapshots/unsafe-doc-old.md.txt+++ b/tests/_convert/snapshots/unsafe-doc-old.md.txt@@ -17,10 +17,10 @@ print("Hello, World!")````python {.marimo hide_code="true"}mo.md(r"""- -->+-->- ```marimo run convert document.md```- """+```marimo run convert document.md```+""")````@@ -31,12 +31,12 @@ it's an unparsable cell````python {.marimo hide_code="true"}mo.md(r"""- <!-- Actually markdown -->- ```{python} `- print("Hello, World!")+<!-- Actually markdown -->+```{python} `+ print("Hello, World!")- <!-- Disabled code block -->- """+<!-- Disabled code block -->+""")````diff --git a/tests/_convert/snapshots/unsafe-doc-old.py.txt b/tests/_convert/snapshots/unsafe-doc-old.py.txtindex 53cc9b8037..c3564744cd 100644--- a/tests/_convert/snapshots/unsafe-doc-old.py.txt+++ b/tests/_convert/snapshots/unsafe-doc-old.py.txt@@ -8,13 +8,13 @@ app = marimo.App(app_title="Casually malicious md")def _(mo):mo.md(r"""- What happens if I just leave a \"\"\"- " ' ! @ # $ % ^ & * ( ) + = - _ [ ] { } | \ /+ What happens if I just leave a \"\"\"+ " ' ! @ # $ % ^ & * ( ) + = - _ [ ] { } | \ /- # Notebook- <!--- \- """+ # Notebook+ <!--+ \+ """)return@@ -29,10 +29,10 @@ def _():def _(mo):mo.md(r"""- -->+ -->- ```marimo run convert document.md```- """+ ```marimo run convert document.md```+ """)return@@ -49,12 +49,12 @@ app._unparsable_cell(def _(mo):mo.md(r"""- <!-- Actually markdown -->- ```{python} `- print("Hello, World!")+ <!-- Actually markdown -->+ ```{python} `+ print("Hello, World!")- <!-- Disabled code block -->- """+ <!-- Disabled code block -->+ """)return@@ -67,11 +67,7 @@ def _():@app.cell(hide_code=True)def _(mo):- mo.md(- r"""- <!-- Hidden code block -->- """- )+ mo.md(r"""<!-- Hidden code block -->""")return@@ -83,11 +79,7 @@ def _():@app.cell(hide_code=True)def _(mo):- mo.md(- r"""- <!-- Empty code block -->- """- )+ mo.md(r"""<!-- Empty code block -->""")return@@ -98,11 +90,7 @@ def _():@app.cell(hide_code=True)def _(mo):- mo.md(- r"""- <!-- Improperly nested code block -->- """- )+ mo.md(r"""<!-- Improperly nested code block -->""")return@@ -120,20 +108,20 @@ app._unparsable_cell(def _(mo):mo.md(r"""- \"\"\"- ```+ \"\"\"+ ```- <!-- Improperly nested code block -->- ```{python}- ````{python}- print("Hello, World!")- ````- ```+ <!-- Improperly nested code block -->+ ```{python}+ ````{python}+ print("Hello, World!")+ ````+ ```- -->+ -->- <!-- from the notebook, should remain unchanged -->- """+ <!-- from the notebook, should remain unchanged -->+ """)returndiff --git a/tests/_convert/snapshots/unsafe-doc.md.txt b/tests/_convert/snapshots/unsafe-doc.md.txtindex b83cf2f005..7757beb333 100644--- a/tests/_convert/snapshots/unsafe-doc.md.txt+++ b/tests/_convert/snapshots/unsafe-doc.md.txt@@ -17,10 +17,10 @@ print("Hello, World!")````python {.marimo hide_code="true"}mo.md(r"""- -->+-->- ```marimo run convert document.md```- """+```marimo run convert document.md```+""")````@@ -31,12 +31,12 @@ it's an unparsable cell````python {.marimo hide_code="true"}mo.md(r"""- <!-- Actually markdown -->- ```python {.marimo} `- print("Hello, World!")+<!-- Actually markdown -->+```python {.marimo} `+ print("Hello, World!")- <!-- Disabled code block -->- """+<!-- Disabled code block -->+""")````diff --git a/tests/_convert/snapshots/unsafe-doc.py.txt b/tests/_convert/snapshots/unsafe-doc.py.txtindex 402ca8af4c..a3033e6817 100644--- a/tests/_convert/snapshots/unsafe-doc.py.txt+++ b/tests/_convert/snapshots/unsafe-doc.py.txt@@ -8,13 +8,13 @@ app = marimo.App(app_title="Casually malicious md")def _(mo):mo.md(r"""- What happens if I just leave a \"\"\"- " ' ! @ # $ % ^ & * ( ) + = - _ [ ] { } | \ /+ What happens if I just leave a \"\"\"+ " ' ! @ # $ % ^ & * ( ) + = - _ [ ] { } | \ /- # Notebook- <!--- \- """+ # Notebook+ <!--+ \+ """)return@@ -29,10 +29,10 @@ def _():def _(mo):mo.md(r"""- -->+ -->- ```marimo run convert document.md```- """+ ```marimo run convert document.md```+ """)return@@ -49,12 +49,12 @@ app._unparsable_cell(def _(mo):mo.md(r"""- <!-- Actually markdown -->- ```python {.marimo} `- print("Hello, World!")+ <!-- Actually markdown -->+ ```python {.marimo} `+ print("Hello, World!")- <!-- Disabled code block -->- """+ <!-- Disabled code block -->+ """)return@@ -67,11 +67,7 @@ def _():@app.cell(hide_code=True)def _(mo):- mo.md(- r"""- <!-- Hidden code block -->- """- )+ mo.md(r"""<!-- Hidden code block -->""")return@@ -83,11 +79,7 @@ def _():@app.cell(hide_code=True)def _(mo):- mo.md(- r"""- <!-- Empty code block -->- """- )+ mo.md(r"""<!-- Empty code block -->""")return@@ -98,11 +90,7 @@ def _():@app.cell(hide_code=True)def _(mo):- mo.md(- r"""- <!-- Improperly nested code block -->- """- )+ mo.md(r"""<!-- Improperly nested code block -->""")return@@ -120,20 +108,20 @@ app._unparsable_cell(def _(mo):mo.md(r"""- \"\"\"- ```+ \"\"\"+ ```- <!-- 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 -->- """+ <!-- from the notebook, should remain unchanged -->+ """)returndiff --git a/tests/_convert/test_basic_marimo_example.py b/tests/_convert/test_basic_marimo_example.pynew file mode 100644index 0000000000..8ff937804a--- /dev/null+++ b/tests/_convert/test_basic_marimo_example.py@@ -0,0 +1,62 @@+# Copyright 2024 Marimo. All rights reserved.+from __future__ import annotations++from textwrap import dedent++from marimo._convert.converters import MarimoConvert+from tests.mocks import snapshotter++snapshot = snapshotter(__file__)+++def test_basic_marimo_example_jupytext_compatibility():+ """Test a basic marimo example that matches what jupytext would generate."""+ # This is the exact content from the jupytext PR test file+ marimo_script = dedent('''+ import marimo++ __generated_with = "0.15.2"+ app = marimo.App(width="medium")+++ @app.cell(hide_code=True)+ def _(mo):+ mo.md(r"""This is a simple marimo notebook""")+ return+++ @app.cell+ def _():+ x = 1+ return (x,)+++ @app.cell+ def _(x):+ y = x+1+ y+ return+++ @app.cell+ def _():+ import marimo as mo+ return (mo,)+++ if __name__ == "__main__":+ app.run()+ ''').strip()++ # Snapshot the original marimo script+ snapshot("basic_marimo_example.py.txt", marimo_script)++ # Test conversion to markdown (now works without file existing)+ converted_to_md = MarimoConvert.from_py(marimo_script).to_markdown(+ "Test Notebook.py"+ )+ snapshot("basic_marimo_example_to_md.txt", converted_to_md)++ # Test conversion back from markdown to marimo+ converted_back = MarimoConvert.from_md(converted_to_md).to_py()+ snapshot("basic_marimo_example_roundtrip.py.txt", converted_back)diff --git a/tests/_convert/test_convert_utils.py b/tests/_convert/test_convert_utils.pyindex 83278072f8..d716a67503 100644--- a/tests/_convert/test_convert_utils.py+++ b/tests/_convert/test_convert_utils.py@@ -10,17 +10,16 @@ from marimo._convert import utilsdef test_markdown_to_marimo():markdown = "# Hello, World!\nThis is a test."- expected = 'mo.md(\n r"""\n # Hello, World!\n This is a test.\n """\n)' # noqa: E501+ expected = 'mo.md(\n r"""\n# Hello, World!\nThis is a test.\n"""\n)' # noqa: E501assert utils.markdown_to_marimo(markdown) == expectedmarkdown = 'Here are some quotes: """'- expected = (- 'mo.md(\n r"""\n Here are some quotes: \\"\\"\\"\n """\n)' # noqa: E501- )+ expected = r'mo.md(r"""Here are some quotes: \"\"\"""")'+assert utils.markdown_to_marimo(markdown) == expectedmarkdown = r"This has a backslash: \\"- expected = 'mo.md(\n r"""\n This has a backslash: \\\\\n """\n)' # noqa: E501+ expected = 'mo.md(r"""This has a backslash: \\\\""")' # noqa: E501assert utils.markdown_to_marimo(markdown) == expected