diff --git a/docs/boilercv_docs/__main__.py b/docs/boilercv_docs/patch_nbs.py similarity index 54% rename from docs/boilercv_docs/__main__.py rename to docs/boilercv_docs/patch_nbs.py index 4dea5836..efbc517c 100644 --- a/docs/boilercv_docs/__main__.py +++ b/docs/boilercv_docs/patch_nbs.py @@ -1,4 +1,4 @@ -"""Insert `hide-input` tag to all documentation notebooks.""" +"""Patch notebooks.""" from pathlib import Path from textwrap import dedent @@ -14,13 +14,24 @@ 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} @@ -28,25 +39,30 @@ def main(): # noqa: D103 :::: """, ) - 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) @@ -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() diff --git a/docs/conf.py b/docs/conf.py index c79099e2..1541594f 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -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 diff --git a/docs/experiments/e230920_subcool/find_centers.ipynb b/docs/experiments/e230920_subcool/find_centers.ipynb index 7308735b..20906dc4 100644 --- a/docs/experiments/e230920_subcool/find_centers.ipynb +++ b/docs/experiments/e230920_subcool/find_centers.ipynb @@ -82,7 +82,6 @@ "metadata": { "tags": [ "hide-input", - "parameters", "remove-output" ] }, @@ -139,8 +138,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -192,8 +190,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -231,8 +228,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -262,8 +258,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -289,8 +284,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], diff --git a/docs/experiments/e230920_subcool/find_contours.ipynb b/docs/experiments/e230920_subcool/find_contours.ipynb index d08bf458..2f90acb0 100644 --- a/docs/experiments/e230920_subcool/find_contours.ipynb +++ b/docs/experiments/e230920_subcool/find_contours.ipynb @@ -42,8 +42,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], diff --git a/docs/experiments/e230920_subcool/find_objects.ipynb b/docs/experiments/e230920_subcool/find_objects.ipynb index 28dd81fd..0bd44469 100644 --- a/docs/experiments/e230920_subcool/find_objects.ipynb +++ b/docs/experiments/e230920_subcool/find_objects.ipynb @@ -83,8 +83,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -127,8 +126,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -167,8 +165,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -212,8 +209,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -243,8 +239,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -276,8 +271,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], diff --git a/docs/experiments/e230920_subcool/find_tracks.ipynb b/docs/experiments/e230920_subcool/find_tracks.ipynb index 2008d41a..8ea27d3e 100644 --- a/docs/experiments/e230920_subcool/find_tracks.ipynb +++ b/docs/experiments/e230920_subcool/find_tracks.ipynb @@ -71,8 +71,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -102,8 +101,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], diff --git a/docs/experiments/e230920_subcool/find_tracks_trackpy.ipynb b/docs/experiments/e230920_subcool/find_tracks_trackpy.ipynb index d4b02235..0451d032 100644 --- a/docs/experiments/e230920_subcool/find_tracks_trackpy.ipynb +++ b/docs/experiments/e230920_subcool/find_tracks_trackpy.ipynb @@ -79,8 +79,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -140,8 +139,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -186,8 +184,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -276,8 +273,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -332,8 +328,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -424,8 +419,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -495,8 +489,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -563,8 +556,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], diff --git a/docs/experiments/e230920_subcool/get_thermal_data.ipynb b/docs/experiments/e230920_subcool/get_thermal_data.ipynb index e14fc6b2..fcf977b3 100644 --- a/docs/experiments/e230920_subcool/get_thermal_data.ipynb +++ b/docs/experiments/e230920_subcool/get_thermal_data.ipynb @@ -48,8 +48,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -84,8 +83,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -102,8 +100,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -122,8 +119,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], diff --git a/docs/experiments/e230920_subcool/plot_tracks.ipynb b/docs/experiments/e230920_subcool/plot_tracks.ipynb index 3bbef27f..80e7442c 100644 --- a/docs/experiments/e230920_subcool/plot_tracks.ipynb +++ b/docs/experiments/e230920_subcool/plot_tracks.ipynb @@ -120,8 +120,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -155,8 +154,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -213,8 +211,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -300,8 +297,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -366,8 +362,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], diff --git a/docs/experiments/e240215_plotting/palettes.ipynb b/docs/experiments/e240215_plotting/palettes.ipynb index f030384a..4c04bfc6 100644 --- a/docs/experiments/e240215_plotting/palettes.ipynb +++ b/docs/experiments/e240215_plotting/palettes.ipynb @@ -48,8 +48,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -63,8 +62,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -82,8 +80,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -97,8 +94,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -111,8 +107,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], @@ -126,8 +121,7 @@ "execution_count": null, "metadata": { "tags": [ - "hide-input", - "parameters" + "hide-input" ] }, "outputs": [], diff --git a/scripts/Sync-Py.ps1 b/scripts/Sync-Py.ps1 index a420ad1c..6ed7c039 100644 --- a/scripts/Sync-Py.ps1 +++ b/scripts/Sync-Py.ps1 @@ -113,14 +113,14 @@ else { # ? Post-sync if (!$NoPostSync) { '*** RUNNING POST-SYNC TASKS' | Write-Progress - 'SYNCING LOCAL DEV CONFIGS' | Write-Progress - & $py -m boilercv_tools 'sync-local-dev-configs' - 'LOCAL DEV CONFIGS SYNCED' | Write-Progress -Done 'INSTALLING PRE-COMMIT HOOKS' | Write-Progress pre-commit install + 'SYNCING LOCAL DEV CONFIGS' | Write-Progress + & $py -m boilercv_tools 'sync-local-dev-configs' 'SYNCING PIPELINE PARAMS' | Write-Progress & $py -m boilercv_pipeline.models.params - 'PIPELINE PARAMS SYNCED' | Write-Progress + 'PATCHING NOTEBOOKS' | Write-Progress + & $py -m boilercv_docs.patch_nbs '' | Write-Host '*** POST-SYNC DONE ***' | Write-Progress -Done }