loqs.tools.migrate.ipynb¶
Rewrite Python code cells embedded in a Jupyter .ipynb notebook, so
migrate_source() can run against a user's own real notebooks the
same way it runs against a plain .py file.
Unlike docs/notebooks/*.md (jupytext MyST Markdown, handled by
notebook), a plain .ipynb file has no
paired, separately-edited source format -- it is the notebook. Only
code cells are touched; markdown/raw cells, outputs, execution
counts, and all other notebook metadata are left exactly as they were.
A notebook is ordinary JSON, and every real .ipynb file round-trips
byte-identically through json.dumps(nb, indent=1, sort_keys=True,
ensure_ascii=False) plus a trailing newline when untouched -- the same
formatting Jupyter's own writer produces. Re-serializing the whole
notebook this way after mutating only the changed cells' source
therefore produces a minimal, cell-scoped diff rather than a spurious
whole-file reformat.
Known limitation, shared with migrate_notebook_source(): each code
cell is migrated independently, with no import context carried over from
an earlier cell, unlike a real kernel where state persists across cells.
A real notebook's code cells routinely include IPython magics (%timeit,
%matplotlib inline), shell escapes (!pip install ...), or ?/??
help syntax -- none of which are valid Python, so migrate_source()
can't parse them directly. Rather than letting one such line abort the
whole cell (real code often surrounds it, e.g. %%time followed by an
ordinary statement), every line the parser rejects is temporarily
replaced with an inert placeholder, the resulting valid Python is
migrated normally, and the original line is substituted back in
afterward -- see _migrate_cell_text for the mechanics. A cell that
still can't be made to parse this way (e.g. a %%bash cell whose entire
body is shell script, not Python) is left untouched and flagged instead.
Functions¶
migrate_ipynb_source ¶
migrate_ipynb_source(source: str, *, rename_iz: bool = False, rename_patch_label: str | None = None) -> MigrationResult
Run migrate_source() over every code cell in a Jupyter
notebook, leaving markdown/raw cells, outputs, and all other
notebook structure untouched.
A ManualReviewItem/RewriteItem's line is relative
to the cell it was found in (a whole-file line number has no
meaningful counterpart in a JSON notebook); its cell is set to that
cell's 1-indexed position among all cells, so the two together locate
it unambiguously (see location). Whenever
anything in the notebook changes, every remaining flagged cell also
gets its own annotate_manual_review() pass, even a cell that
had no rewrite of its own -- the notebook is being rewritten either
way, so there's no reason to leave some of its flagged spots
undocumented in the file itself.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str
|
The full text of an |
required |
rename_iz
|
bool
|
Forwarded to every cell's own |
False
|
rename_patch_label
|
bool
|
Forwarded to every cell's own |
False
|