From a64df28d5ce5788a124e7353c696241087defa22 Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Mon, 8 Jul 2024 16:58:28 +0200 Subject: [PATCH] FEAT: remove tags from notebook cell if empty (#350) * FIX: remove `slideshow` from config cell --- .pre-commit-hooks.yaml | 9 ++++++ pyproject.toml | 1 + src/compwa_policy/remove_empty_tags.py | 45 ++++++++++++++++++++++++++ src/compwa_policy/set_nb_cells.py | 1 - 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/compwa_policy/remove_empty_tags.py diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 155abe1b..2e012046 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -19,6 +19,15 @@ types: - jupyter +- id: remove-empty-tags + name: Remove empty tags metadata field from Jupyter notebooks + description: > + Remove the tags metadata field from Jupyter notebooks if there are no tags + entry: remove-empty-tags + language: python + types: + - jupyter + - id: set-nb-cells name: Add or update default cells in a Jupyter notebook description: > diff --git a/pyproject.toml b/pyproject.toml index 64d368fd..e3fb1fca 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -87,6 +87,7 @@ file = "README.md" check-dev-files = "compwa_policy.check_dev_files:main" colab-toc-visible = "compwa_policy.colab_toc_visible:main" fix-nbformat-version = "compwa_policy.fix_nbformat_version:main" +remove-empty-tags = "compwa_policy.remove_empty_tags:main" self-check = "compwa_policy.self_check:main" set-nb-cells = "compwa_policy.set_nb_cells:main" diff --git a/src/compwa_policy/remove_empty_tags.py b/src/compwa_policy/remove_empty_tags.py new file mode 100644 index 00000000..0167cc7a --- /dev/null +++ b/src/compwa_policy/remove_empty_tags.py @@ -0,0 +1,45 @@ +"""Remove tags metadata from notebook cells if they are empty.""" + +from __future__ import annotations + +import argparse +import sys +from typing import Sequence + +import nbformat + +from compwa_policy.errors import PrecommitError +from compwa_policy.utilities.executor import Executor +from compwa_policy.utilities.notebook import load_notebook + + +def main(argv: Sequence[str] | None = None) -> int: + parser = argparse.ArgumentParser(__doc__) + parser.add_argument("filenames", nargs="*", help="Filenames to check.") + args = parser.parse_args(argv) + + with Executor(raise_exception=False) as do: + for filename in args.filenames: + do(_remove_cells, filename) + return 1 if do.error_messages else 0 + + +def _remove_cells(filename: str) -> None: + notebook = load_notebook(filename) + updated = False + for cell in notebook["cells"]: + metadata = cell["metadata"] + tags = metadata.get("tags") + if tags is None: + continue + if not tags: + metadata.pop("tags") + updated = True + if updated: + nbformat.write(notebook, filename) + msg = f'Removed empty tags cell metadata from "{filename}"' + raise PrecommitError(msg) + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/src/compwa_policy/set_nb_cells.py b/src/compwa_policy/set_nb_cells.py index a5119af4..9c4d4d43 100644 --- a/src/compwa_policy/set_nb_cells.py +++ b/src/compwa_policy/set_nb_cells.py @@ -41,7 +41,6 @@ "hideOutput": True, "hidePrompt": True, "jupyter": {"source_hidden": True}, - "slideshow": {"slide_type": "skip"}, "tags": ["remove-cell"], }