|
60 | 60 | "id": "3c669488", |
61 | 61 | "metadata": {}, |
62 | 62 | "source": [ |
63 | | - "To avoid pointless conflicts while working with jupyter notebooks (with different execution counts or cell metadata), it is recommended to clean the notebooks before committing anything (done automatically if you install the git hooks with `nbdev-install-hooks`). The following functions are used to do that. Cleaning also adds cell `id`s if missing (required by nbformat 4.5+)." |
| 63 | + "Clean notebooks before committing to remove execution counts and metadata that cause unnecessary merge conflicts. Install the hooks with `nbdev-install-hooks` to clean notebooks automatically. Cleaning also adds missing cell IDs, which nbformat 4.5+ requires." |
64 | 64 | ] |
65 | 65 | }, |
66 | 66 | { |
|
237 | 237 | "id": "20e64f41", |
238 | 238 | "metadata": {}, |
239 | 239 | "source": [ |
240 | | - "Jupyter adds a trailing <code>\\n</code> to images in cell outputs. Vscode-jupyter does not.\\\n", |
241 | | - "Notebooks should be brought to a common style to avoid unnecessary diffs:" |
| 240 | + "Jupyter adds a trailing `\\n` to images in cell outputs. VS Code's Jupyter extension does not. In this PNG example, `clean_nb` removes the newline to avoid editor-dependent diffs:" |
242 | 241 | ] |
243 | 242 | }, |
244 | 243 | { |
|
259 | 258 | "id": "62dd6b38", |
260 | 259 | "metadata": {}, |
261 | 260 | "source": [ |
262 | | - "The test notebook has metadata in both the main metadata section and contains cell level metadata in the second cell:" |
| 261 | + "The test notebook contains notebook metadata and metadata on its second cell:" |
263 | 262 | ] |
264 | 263 | }, |
265 | 264 | { |
|
280 | 279 | "id": "5261f00d", |
281 | 280 | "metadata": {}, |
282 | 281 | "source": [ |
283 | | - "After cleaning the notebook, all extra metadata is removed, only some keys are allowed by default:" |
| 282 | + "`clean_nb` keeps the default allowed metadata keys and removes the others:" |
284 | 283 | ] |
285 | 284 | }, |
286 | 285 | { |
|
301 | 300 | "id": "5c387f44", |
302 | 301 | "metadata": {}, |
303 | 302 | "source": [ |
304 | | - "`clean_nb` also repairs structural problems by default (via `repair_nb`), so notebooks that Jupyter would reject, such as a markdown cell carrying an `outputs` attr, are fixed on every clean:" |
| 303 | + "`clean_nb` calls `repair_nb` by default to fix invalid notebook structure. For example, Markdown cells must not have `outputs` or `execution_count` attributes:" |
305 | 304 | ] |
306 | 305 | }, |
307 | 306 | { |
|
367 | 366 | "id": "286cb214", |
368 | 367 | "metadata": {}, |
369 | 368 | "source": [ |
370 | | - "Passing `clean_ids=True` removes `id`s from plaintext repr outputs, to avoid notebooks whose contents change on each run since they often lead to git merge conflicts. For example:\n", |
| 369 | + "`clean_ids=True` removes object memory addresses from text representations in outputs. These addresses can change between runs and cause unnecessary Git diffs and merge conflicts. For example:\n", |
371 | 370 | "\n", |
372 | 371 | "```\n", |
373 | 372 | "<PIL.PngImagePlugin.PngImageFile image mode=L size=28x28 at 0x7FB4F8979690>\n", |
|
385 | 384 | "id": "89d0a249", |
386 | 385 | "metadata": {}, |
387 | 386 | "source": [ |
388 | | - "*Cell* IDs, on the other hand, are always added if missing" |
| 387 | + "`clean_nb` adds missing cell IDs:" |
389 | 388 | ] |
390 | 389 | }, |
391 | 390 | { |
|
460 | 459 | "source": [ |
461 | 460 | "## Directive migrations\n", |
462 | 461 | "\n", |
463 | | - "Deliberate, opt-in rewrites for moving to the directive conventions introduced in v3.3: directives can live in cell metadata (an `nbdev` dict key), notebook-scope directives like `default_exp` can live in notebook metadata, and comment spellings have one canonical form. None of these run by default; they're flags on `nbdev-clean` for one-off migration runs, so the git hook never triggers them." |
| 462 | + "`nbdev-clean` can rewrite directive comments and move directives into metadata. These migrations require explicit flags. Git hooks never run them." |
464 | 463 | ] |
465 | 464 | }, |
466 | 465 | { |
|
504 | 503 | "id": "233f7207", |
505 | 504 | "metadata": {}, |
506 | 505 | "source": [ |
507 | | - "`_to_meta` and `_to_comments` move named directives between a cell's comments and its `nbdev` metadata key, preserving values through the same mapping the parser uses (bare directives become `true`, and so on):" |
| 506 | + "Move selected directives between comments and cell metadata without changing their meaning:" |
508 | 507 | ] |
509 | 508 | }, |
510 | 509 | { |
|
567 | 566 | "id": "bb81ec0f", |
568 | 567 | "metadata": {}, |
569 | 568 | "source": [ |
570 | | - "`_canon_dirs` respells each directive line canonically without touching anything else, and `_hoist_nb_meta` moves `default_exp` to notebook metadata, deleting a first cell that held nothing but directives (a bare `#| hide` on an otherwise-empty cell hides nothing). `_dir_moves` bundles the migrations for the CLI, converting the raw loaded dict to `NbCell`s first:" |
| 569 | + "`_canon_dirs` rewrites directive comments in canonical form and leaves other source unchanged. `_hoist_nb_meta` moves `default_exp` into notebook metadata. It removes cells left empty by the move, including those containing only `#| hide`:" |
571 | 570 | ] |
572 | 571 | }, |
573 | 572 | { |
|
644 | 643 | "source": [ |
645 | 644 | "By default (`fname` left to `None`), all the notebooks in `config.nbs_path` are cleaned. You can opt in to fully clean the notebook by removing every bit of metadata and the cell outputs by passing `clear_all=True`.\n", |
646 | 645 | "\n", |
647 | | - "If you want to keep some keys in the main notebook metadata you can set `allowed_metadata_keys` in `[tool.nbdev]` in `pyproject.toml`.\n", |
648 | | - "Similarly for cell level metadata use `allowed_cell_metadata_keys`, and for output metadata use `allowed_out_metadata_keys`. For example, to preserve both `k1` and `k2` at both the notebook and cell level add the following to `pyproject.toml`:\n", |
| 646 | + "To preserve extra metadata, use these settings under `[tool.nbdev]` in `pyproject.toml`:\n", |
| 647 | + "\n", |
| 648 | + "- `allowed_metadata_keys` preserves notebook metadata.\n", |
| 649 | + "- `allowed_cell_metadata_keys` preserves cell metadata.\n", |
| 650 | + "- `allowed_out_metadata_keys` preserves output metadata.\n", |
| 651 | + "\n", |
| 652 | + "This example preserves `k1` and `k2` at all three levels:\n", |
649 | 653 | "```toml\n", |
650 | 654 | "[tool.nbdev]\n", |
651 | 655 | "allowed_metadata_keys = [\"k1\", \"k2\"]\n", |
|
952 | 956 | "source": [ |
953 | 957 | "See `clean_jupyter` and `nbdev-merge` for more about how each hook works.\n", |
954 | 958 | "\n", |
955 | | - "Both git drivers are registered under the name `jupyternotebook`, the same name nbdime uses. Repo installs define them in a repo-local `.gitconfig` (wired in via `include.path`) and activate them in the committed `.gitattributes`; `--globally` instead defines them in `~/.gitconfig` and activates them in the global attributes file (`core.attributesFile`, defaulting to `~/.config/git/attributes`). Since the attribute lines just name a driver, a committed `.gitattributes` means \"use your preferred notebook driver\": whichever tool defined `jupyternotebook` last in a config git consults wins, so switching between nbdev and nbdime is just re-running either tool's enable command. Global installs skip the repo-only `post-merge` trust hook." |
| 959 | + "Both Git drivers use the name `jupyternotebook`, matching nbdime. The attributes file selects this name. Git configuration determines which program runs it. You can therefore commit `.gitattributes` while letting collaborators use their preferred driver.\n", |
| 960 | + "\n", |
| 961 | + "A repo installation defines the drivers in `.gitconfig`, included through `include.path`, and activates them in `.gitattributes`. With `--globally`, definitions go in `~/.gitconfig`. Attributes go in `core.attributesFile`, which defaults to `~/.config/git/attributes`.\n", |
| 962 | + "\n", |
| 963 | + "Re-running either tool's enable command changes its driver configuration. Git uses the definition with highest precedence. Global installations skip the repo's `post-merge` trust hook." |
956 | 964 | ] |
957 | 965 | }, |
958 | 966 | { |
|
0 commit comments