-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: always overwrite kernel name to "Python 3 (ipykernel)" (#499)
- Loading branch information
Showing
5 changed files
with
72 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,6 +109,7 @@ | |
"Deepnote", | ||
"docstrings", | ||
"graphviz", | ||
"ipykernel", | ||
"ipython", | ||
"mkdir", | ||
"mypy", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |