Skip to content

Commit

Permalink
Fix notebook patching
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeNaccarato committed Apr 10, 2024
1 parent 19b7241 commit 3ecd778
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 103 deletions.
58 changes: 35 additions & 23 deletions docs/boilercv_docs/__main__.py → docs/boilercv_docs/patch_nbs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Insert `hide-input` tag to all documentation notebooks."""
"""Patch notebooks."""

from pathlib import Path
from textwrap import dedent
Expand All @@ -14,39 +14,55 @@


def main(): # noqa: D103
patch_nbs()


def patch_nbs():
"""Patch notebooks.
Patch Thebe buttons in. Insert `parameters` and `thebe-init` tags to the first code
cell. Insert `hide-input` tags to code cells.
"""
for path in Path("docs").rglob("*.ipynb"):
nb: NotebookNode = read(path, NO_CONVERT) # type: ignore # pyright 1.1.348, # nbformat: 5.9.2
if path not in EXCLUDE_THEBE:
# Patch the first Markdown cell
i, first_md_cell = get_first(nb, MD)
# ? Patch the first Markdown cell
i, first = next(
(i, c) for i, c in enumerate(nb.cells) if c.cell_type == "markdown"
)
nb.cells[i][SRC] = patch(
first_md_cell.get(SRC, ""),
first.get(SRC, ""),
"""
::::
:::{thebe-button}
:::
::::
""",
)
i, first_code_cell = get_first(nb, CODE)
if path not in EXCLUDE_THEBE:
# Insert Thebe tags to the first code cell
nb.cells[i] = insert_tag(first_code_cell, ["thebe-init"])
# Patch the first code cell
# ? Patch the first code cell
code_cells = ((i, c) for i, c in enumerate(nb.cells) if c.cell_type == "code")
i, first = next(code_cells)
nb.cells[i][SRC] = patch(
first_code_cell.get(SRC, ""),
first.get(SRC, ""),
"""
from boilercv_docs.nbs import init
paths = init()
""",
)
# Insert tags to all code cells
for i, cell in enumerate(nb.cells):
if cell.cell_type != "code":
continue
nb.cells[i] = insert_tag(cell, ["hide-input", "parameters"])
# Write the notebook back
# ? Insert tags to first code cell
nb.cells[i] = insert_tag(
first,
[
"hide-input",
"parameters",
*([] if path in EXCLUDE_THEBE else ["thebe-init"]),
],
)
# ? Insert tags to remaining code cells
for i, cell in code_cells:
nb.cells[i] = insert_tag(cell, ["hide-input"])
# ? Write the notebook back
write(nb, path)


Expand All @@ -56,19 +72,15 @@ def insert_tag(cell: NotebookNode, tags_to_insert: list[str]) -> NotebookNode:
See: https://jupyterbook.org/en/stable/content/metadata.html?highlight=python#add-tags-using-python-code
"""
tags = cell.get("metadata", {}).get("tags", [])
cell["metadata"]["tags"] = tags_to_insert + list(set(tags) - set(tags_to_insert))
cell["metadata"]["tags"] = sorted(set(tags) | set(tags_to_insert))
return cell


def get_first(nb: NotebookNode, cell_type: str) -> tuple[int, NotebookNode]:
"""Get the first cell of a given type."""
return next((i, c) for i, c in enumerate(nb.cells) if c.cell_type == cell_type)


def patch(src: str, content: str, end: str = "\n\n") -> str:
"""Prepend source lines to cell source if not there already."""
content = dedent(content).strip()
return src if src.startswith(content) else f"{content}{end}{src}"


main()
if __name__ == "__main__":
main()
4 changes: 3 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
from boilercv_docs import DOCS, PYPROJECT, chdir_docs
from boilercv_docs.intersphinx import get_ispx, get_rtd, get_url
from boilercv_docs.nbs import init_nb_env
from boilercv_docs.patch_nbs import patch_nbs
from boilercv_docs.types import IspxMappingValue
from ruamel.yaml import YAML
from sphinx.application import Sphinx

# ! Root
# ! Initialization
patch_nbs()
ROOT = chdir_docs()
"""Root directory of the project."""
# ! Paths
Expand Down
16 changes: 5 additions & 11 deletions docs/experiments/e230920_subcool/find_centers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
"metadata": {
"tags": [
"hide-input",
"parameters",
"remove-output"
]
},
Expand Down Expand Up @@ -139,8 +138,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand Down Expand Up @@ -192,8 +190,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand Down Expand Up @@ -231,8 +228,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand Down Expand Up @@ -262,8 +258,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand All @@ -289,8 +284,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand Down
3 changes: 1 addition & 2 deletions docs/experiments/e230920_subcool/find_contours.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand Down
18 changes: 6 additions & 12 deletions docs/experiments/e230920_subcool/find_objects.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand Down Expand Up @@ -127,8 +126,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand Down Expand Up @@ -167,8 +165,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand Down Expand Up @@ -212,8 +209,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand Down Expand Up @@ -243,8 +239,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand Down Expand Up @@ -276,8 +271,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand Down
6 changes: 2 additions & 4 deletions docs/experiments/e230920_subcool/find_tracks.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand Down Expand Up @@ -102,8 +101,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand Down
24 changes: 8 additions & 16 deletions docs/experiments/e230920_subcool/find_tracks_trackpy.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand Down Expand Up @@ -140,8 +139,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand Down Expand Up @@ -186,8 +184,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand Down Expand Up @@ -276,8 +273,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand Down Expand Up @@ -332,8 +328,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand Down Expand Up @@ -424,8 +419,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand Down Expand Up @@ -495,8 +489,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand Down Expand Up @@ -563,8 +556,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand Down
12 changes: 4 additions & 8 deletions docs/experiments/e230920_subcool/get_thermal_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand Down Expand Up @@ -84,8 +83,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand All @@ -102,8 +100,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand All @@ -122,8 +119,7 @@
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"parameters"
"hide-input"
]
},
"outputs": [],
Expand Down
Loading

0 comments on commit 3ecd778

Please sign in to comment.