Write a notebook using nbformat. Note: nbformat writes JSON in a canonical form; it *will* rewrite the file, so expect diffs if the notebook wasn't previously normalized to the same style.
(path: Path, nb: Any)
| 549 | |
| 550 | |
| 551 | def write_ipynb_meta(path: Path, nb: Any) -> None: |
| 552 | """ |
| 553 | Write a notebook using nbformat. |
| 554 | |
| 555 | Note: nbformat writes JSON in a canonical form; it *will* rewrite the file, |
| 556 | so expect diffs if the notebook wasn't previously normalized to the same style. |
| 557 | """ |
| 558 | # Validate before writing (optional but recommended) |
| 559 | nbformat.validate(nb) |
| 560 | |
| 561 | # Use a stable indentation to reduce churn (choose 2 if your repo tends that way) |
| 562 | text = nbformat.writes(nb, version=4, indent=2, ensure_ascii=False) |
| 563 | |
| 564 | path.write_text(text + "\n", encoding="utf-8") |
| 565 | |
| 566 | |
| 567 | def parse_dlc_meta(raw: Any) -> tuple[DLCMeta | None, bool]: |
no test coverage detected