MCPcopy
hub / github.com/marimo-team/marimo / convert

Function convert

marimo/_cli/convert/commands.py:34–142  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

32 ),
33)
34def 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")

Callers 1

py__doc__Function · 0.85

Calls 12

load_external_fileFunction · 0.90
parse_notebookFunction · 0.90
echoFunction · 0.90
prompt_to_overwriteFunction · 0.90
maybe_make_dirsFunction · 0.90
_build_rerun_commandFunction · 0.85
from_ipynbMethod · 0.80
from_mdMethod · 0.80
to_pyMethod · 0.45
write_textMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…