Skip to content

Commit

Permalink
FEAT: remove tags from notebook cell if empty (#350)
Browse files Browse the repository at this point in the history
* FIX: remove `slideshow` from config cell
  • Loading branch information
redeboer authored Jul 8, 2024
1 parent b91c433 commit a64df28
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: >
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
45 changes: 45 additions & 0 deletions src/compwa_policy/remove_empty_tags.py
Original file line number Diff line number Diff line change
@@ -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())
1 change: 0 additions & 1 deletion src/compwa_policy/set_nb_cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"hideOutput": True,
"hidePrompt": True,
"jupyter": {"source_hidden": True},
"slideshow": {"slide_type": "skip"},
"tags": ["remove-cell"],
}

Expand Down

0 comments on commit a64df28

Please sign in to comment.