r"""Convert a Jupyter notebook, Markdown file, or Python script to a marimo notebook. Supported input formats: - `.ipynb` (local or GitHub-hosted) - `.md` files with `{python}` code fences - `.py` scripts in py:percent format Behavior: - Jupyter notebooks: outputs are strip
(
filename: str,
output: Path | None,
)
| 32 | ), |
| 33 | ) |
| 34 | def convert( |
| 35 | filename: str, |
| 36 | output: Path | None, |
| 37 | ) -> None: |
| 38 | r"""Convert a Jupyter notebook, Markdown file, or Python script to a marimo notebook. |
| 39 | |
| 40 | Supported input formats: |
| 41 | - `.ipynb` (local or GitHub-hosted) |
| 42 | - `.md` files with `{python}` code fences |
| 43 | - `.py` scripts in py:percent format |
| 44 | |
| 45 | Behavior: |
| 46 | - Jupyter notebooks: outputs are stripped. |
| 47 | |
| 48 | - Markdown files: only `{python}` fenced code blocks are converted. |
| 49 | |
| 50 | Example: |
| 51 | ```{python} |
| 52 | x = 1 + 2 |
| 53 | print(x) |
| 54 | ``` |
| 55 | - Python scripts: |
| 56 | - If already a valid marimo notebook, no conversion is performed. |
| 57 | - Otherwise, marimo attempts to convert using py:percent formatting, |
| 58 | preserving top-level comments and docstrings. |
| 59 | |
| 60 | Example usage: |
| 61 | |
| 62 | marimo convert your_nb.ipynb -o your_nb.py |
| 63 | |
| 64 | or |
| 65 | |
| 66 | marimo convert your_nb.md -o your_nb.py |
| 67 | |
| 68 | or |
| 69 | |
| 70 | marimo convert script.py -o your_nb.py |
| 71 | |
| 72 | You can also pass global flags to the main marimo command. |
| 73 | For example, use `-q` to suppress output or `-y` |
| 74 | to automatically accept all prompts of the command. |
| 75 | |
| 76 | marimo -q -y convert script.py -o your_nb.py |
| 77 | |
| 78 | After conversion: |
| 79 | |
| 80 | marimo edit your_nb.py |
| 81 | |
| 82 | Note: |
| 83 | Since marimo's reactive execution differs from traditional notebooks, |
| 84 | you may need to refactor code that mutates variables across cells |
| 85 | (e.g., modifying a dataframe in multiple cells), which can lead to |
| 86 | unexpected behavior. |
| 87 | """ |
| 88 | |
| 89 | ext = Path(filename).suffix |
| 90 | if ext not in (".ipynb", ".md", ".qmd", ".py"): |
| 91 | raise click.UsageError("File must be an .ipynb, .md, or .py file") |
no test coverage detected
searching dependent graphs…