Skip to content

Commit

Permalink
ENH: always overwrite kernel name to "Python 3 (ipykernel)" (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer authored Jan 22, 2025
1 parent c4d5547 commit 46ee6b4
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 7 deletions.
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
"Deepnote",
"docstrings",
"graphviz",
"ipykernel",
"ipython",
"mkdir",
"mypy",
Expand Down
7 changes: 7 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,10 @@
language: python
types:
- jupyter

- id: set-nb-display-name
name: Revert display name of the notebook kernel
entry: set-nb-display-name
language: python
types:
- jupyter
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ 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"
set-nb-display-name = "compwa_policy.set_nb_display_name:main"

[project.urls]
Source = "https://github.com/ComPWA/policy"
Expand Down
19 changes: 12 additions & 7 deletions src/compwa_policy/check_dev_files/precommit.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,18 @@ def _update_policy_hook(precommit: ModifiablePrecommit, has_notebooks: bool) ->
msg = "Could not find ComPWA/policy pre-commit repo"
raise KeyError(msg)
hook_ids = {h["id"] for h in repo["hooks"]}
remove_empty_tags_ids = "remove-empty-tags"
if remove_empty_tags_ids in hook_ids:
return
precommit.update_hook(
repo_url=repo["repo"],
expected_hook=Hook(id=remove_empty_tags_ids),
)
with Executor() as do:
for expected_hook_id in [
"remove-empty-tags",
"set-nb-display-name",
]:
if expected_hook_id in hook_ids:
continue
do(
precommit.update_hook,
repo_url=repo["repo"],
expected_hook=Hook(id=expected_hook_id),
)


def _update_precommit_ci_commit_msg(precommit: ModifiablePrecommit) -> None:
Expand Down
51 changes: 51 additions & 0 deletions src/compwa_policy/set_nb_display_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""Remove tags metadata from notebook cells if they are empty."""

from __future__ import annotations

import argparse
import sys
from typing import TYPE_CHECKING

import nbformat

from compwa_policy.errors import PrecommitError
from compwa_policy.utilities.executor import Executor
from compwa_policy.utilities.notebook import load_notebook

if TYPE_CHECKING:
from collections.abc import Sequence


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(_set_nb_display_name, filename)
return 1 if do.error_messages else 0


def _set_nb_display_name(filename: str) -> None:
notebook = load_notebook(filename)
display_name = (
notebook.get("metadata", {})
.get("kernelspec", {}) # cspell:ignore kernelspec
.get("display_name")
)
expected_display_name = "Python 3 (ipykernel)"
if display_name != expected_display_name:
if "metadata" not in notebook:
notebook["metadata"] = {}
metadata = notebook["metadata"]
if "kernelspec" not in metadata:
metadata["kernelspec"] = {}
metadata["kernelspec"]["display_name"] = expected_display_name
nbformat.write(notebook, filename)
msg = f"Set display name to {expected_display_name!r} in {filename}"
raise PrecommitError(msg)


if __name__ == "__main__":
sys.exit(main())

0 comments on commit 46ee6b4

Please sign in to comment.