Skip to content

loqs.tools.migrate.notebook

Extract/splice Python code cells out of a MyST Markdown tutorial, so migrate_source() can run against docs/notebooks/*.md's embedded code the same way it runs against a plain .py file.

docs/notebooks/*.md is jupytext-formatted MyST Markdown, not bare Python: an ```{code-cell} ipython3 fence opens a code cell, a bare ``` closes it, and everything else (prose, other fence types like ```{note}, front matter) is untouched. This is the only committed representation (the paired .ipynb is gitignored and regenerated locally via jupytext --to ipynb), so it's also the only one this tool needs to touch directly.

A code cell may open with one or more :key: value MyST field lines (e.g. :tags: [scroll-output]) before its actual code, as docs/notebooks/workflow.md does -- these aren't parseable as bare Python on their own. These lines are passed through unchanged, alongside the fence itself, never handed to migrate_source.

Known limitation: each cell is migrated independently, with no import context carried over from earlier cells -- unlike a real notebook kernel, where state (including imports) persists across cells. A renamed class used bare in a later cell, relying on an import done in an earlier one, is neither rewritten nor flagged, since rewrite_renames()'s QualifiedNameProvider-based resolution can't see that import from inside this cell alone. Not currently a problem for docs/notebooks/*.md itself, but worth knowing about for a user's own notebook.

Whenever anything in the document changes, every remaining flagged cell also gets its own annotate_manual_review() pass, even a cell that had no rewrite of its own -- the document is being rewritten either way, so there's no reason to leave some of its flagged spots undocumented in the file itself.

Functions

migrate_notebook_source

migrate_notebook_source(source: str, *, rename_iz: bool = False, rename_patch_label: str | None = None) -> MigrationResult

Run migrate_source() over every code cell in a MyST Markdown document, leaving everything else untouched.

A ManualReviewItem/RewriteItem's line number is relative to the whole document (not the cell), matching migrate_source()'s own per-file convention. rename_iz/rename_patch_label are forwarded to every cell's own migrate_source() call unchanged.