Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: add hook for making Colab TOC visible #174

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
always_run: true
pass_filenames: false

- id: colab-toc-visible
name: Show the TOC sidebar in Google Colab by default
entry: colab-toc-visible
language: python
types:
- jupyter

- id: fix-nbformat-version
name: Set nbformat minor version to 4 and remove cell IDs
entry: fix-nbformat-version
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ dev =
[options.entry_points]
console_scripts =
check-dev-files = repoma.check_dev_files:main
colab-toc-visible = repoma.colab_toc_visible:main
fix-nbformat-version = repoma.fix_nbformat_version:main
format-setup-cfg = repoma.format_setup_cfg:main
pin-nb-requirements = repoma.pin_nb_requirements:main
Expand Down
54 changes: 54 additions & 0 deletions src/repoma/colab_toc_visible.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""Add notebook metadata to open the TOC sidebar on Google Colab.

See `ComPWA/repo-maintenance#40 <https://github.com/ComPWA/repo-maintenance/issues/40>`_
for more information.
"""

import argparse
import sys
from typing import Optional, Sequence

import nbformat
from nbformat import NotebookNode

from .errors import PrecommitError
from .utilities.executor import Executor


def main(argv: Optional[Sequence[str]] = None) -> int:
parser = argparse.ArgumentParser(__doc__)
parser.add_argument(
"filenames",
nargs="*",
help="Paths to the notebooks of which the metadata should be updated.",
)
args = parser.parse_args(argv)
executor = Executor()
for filename in args.filenames:
executor(_update_metadata, filename)
return executor.finalize(exception=False)


def _update_metadata(path: str) -> None:
notebook = open_notebook(path)
metadata = notebook["metadata"]
updated = False
if metadata.get("colab") is None:
updated = True
metadata["colab"] = {}
if not metadata["colab"].get("toc_visible"):
updated = True
metadata["colab"]["toc_visible"] = True
if not updated:
return
nbformat.write(notebook, path)
msg = f"Colab TOC is now visible for notebook {path}"
raise PrecommitError(msg)


def open_notebook(path: str) -> NotebookNode:
return nbformat.read(path, as_version=nbformat.NO_CONVERT)


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