Skip to content

Commit

Permalink
Merge branch 'main' into 6bf698a3-dc63-11ee-a878-aac83d19daa0
Browse files Browse the repository at this point in the history
  • Loading branch information
malmans2 committed Jul 3, 2024
2 parents 3206645 + dcf5051 commit a58fdbe
Show file tree
Hide file tree
Showing 9 changed files with 1,265 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/notebook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ body:
attributes:
label: Workflow ID
description: Provide the workflow ID.
placeholder: e.g., 1123e4567-e89b-12d3-a456-42665544000
placeholder: e.g., eqctier3-1123e4567-e89b-12d3-a456-42665544000
validations:
required: true

Expand Down
93 changes: 93 additions & 0 deletions .github/workflows/on-pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
on: pull_request

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

defaults:
run:
shell: bash -l {0}

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.x
- uses: pre-commit/[email protected]

build-book:
runs-on: ubuntu-latest
needs: pre-commit
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.x
- name: Get changed notebooks
id: changed-files
uses: tj-actions/changed-files@v44
with:
files: |
**.ipynb
- name: Build permalinks
id: build-permalinks
env:
ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
run: |-
permalinks="<ul>"
for fullfile in ${ALL_CHANGED_FILES}; do
fullfile="${fullfile//\\&/&}"
commit=$(git rev-list -1 HEAD "$fullfile")
suffix="${fullfile//&/%26}"
url=${{ github.server_url }}/${{ github.repository }}/blob/$commit/$suffix
permalinks+="<li><a href=\"$url\">$url</a></li>"
done
permalinks+="</ul>"
echo "permalinks=$permalinks" >>"$GITHUB_OUTPUT"
- name: Comment PR with permalinks
uses: thollander/actions-comment-pull-request@v2
with:
message: |
**Permalinks:**
${{ steps.build-permalinks.outputs.permalinks }}
comment_tag: permalinks
- name: Checkout book repo
uses: actions/checkout@v4
with:
repository: ecmwf-projects/c3s_eqc_book_main
path: book
- name: Install jupyter-book
run: |
python -m pip install jupyter-book
- name: Build pages
env:
ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
run: |-
mkdir book_preview || exit
cd book/c3s_eqc_quality_assessments || exit
for fullfile in ${ALL_CHANGED_FILES}; do
mkdir tmp || exit
fullfile="${fullfile//\\&/&}"
filename=$(basename -- "$fullfile")
prefix="${filename%.*}"
cp ../../"$fullfile" tmp/"$filename" || exit
python ../../scripts/convert-to-book-format.py tmp/"$filename" || exit
jupyter-book build tmp/"$filename" || exit
mv _build/_page/tmp-"$prefix"/html ../../book_preview/"$prefix" || exit
rm -fr _build
rm -fr tmp
done
- uses: actions/upload-artifact@v4
id: upload-artifact
with:
name: book-preview
path: book_preview/*
- name: Comment PR with book preview url
uses: thollander/actions-comment-pull-request@v2
with:
message: |
**Jupyter Book Preview:** ${{ steps.upload-artifact.outputs.artifact-url }}
comment_tag: artifact-url
21 changes: 0 additions & 21 deletions .github/workflows/on-push.yml

This file was deleted.

4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__MACOSX/
*.zip
*.html
*.png
11 changes: 7 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ repos:
rev: v4.6.0
hooks:
- id: check-yaml
- id: check-toml
- id: check-merge-conflict
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.2
rev: v0.4.3
hooks:
- id: ruff
args: [--fix, --show-fixes]
Expand All @@ -15,6 +16,8 @@ repos:
hooks:
- id: pretty-format-yaml
args: [--autofix, --preserve-quotes]
- id: pretty-format-toml
args: [--autofix]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.10.0
hooks:
Expand All @@ -27,9 +30,9 @@ repos:
entry: python scripts/validate-filenames.py
language: python
types: [jupyter]
- id: lint-tags
name: lint-tags
entry: python scripts/lint-tags.py
- id: format-tags
name: format-tags
entry: python scripts/format-tags.py
language: python
types: [jupyter]
additional_dependencies: [nbformat]

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Same as Black.

indent-width = 4
line-length = 88

[lint]
ignore = [
# pydocstyle: Missing Docstrings
"D1"
]
select = [
# pyflakes
"F",
# pycodestyle
"E",
"W",
# isort
"I",
# pydocstyle
"D"
]

[lint.pycodestyle]
max-line-length = 110

[lint.pydocstyle]
convention = "numpy"
37 changes: 37 additions & 0 deletions scripts/convert-to-book-format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import argparse
import base64
from pathlib import Path

import nbformat
import nbformat.v4

LOGO_PATH = "../LogoLine_horizon_C3S.png"


def convert_notebook(path: Path) -> None:
notebook = nbformat.read(path, nbformat.NO_CONVERT)

# Add logo
notebook.cells.insert(0, nbformat.v4.new_markdown_cell(f"![logo]({LOGO_PATH})"))

# Decode figure
for cell in notebook.cells:
attachments = cell.pop("attachments", {})
for name, data in attachments.items():
cell["source"] = cell["source"].replace(f"(attachment:{name})", f"({name})")
for encoded in data.values():
(path.parent / name).write_bytes(base64.b64decode(encoded))

nbformat.write(notebook, path)


def main(paths: list[Path]) -> None:
for path in paths:
convert_notebook(path)


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("paths", action="store", type=Path, nargs="*")
args = parser.parse_args()
main(args.paths)
14 changes: 7 additions & 7 deletions scripts/lint-tags.py → scripts/format-tags.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import nbformat
from pathlib import Path
import argparse
from pathlib import Path

import nbformat

REQUIRED_TAGS: dict[str, set[str]] = {"code": {"hide-input"}}
MANDATORY_TAGS: dict[str, set[str]] = {"code": {"hide-input"}}


def main(paths: list[Path]) -> None:
Expand All @@ -12,11 +13,10 @@ def main(paths: list[Path]) -> None:

for cell in notebook.cells:
tags = set(cell["metadata"].get("tags", []))
required_tags = REQUIRED_TAGS.get(cell["cell_type"], set())
if not required_tags <= tags:
mandatory_tags = MANDATORY_TAGS.get(cell["cell_type"], set())
if not mandatory_tags <= tags:
write = True
tags |= required_tags
cell["metadata"]["tags"] = list(tags)
cell["metadata"]["tags"] = list(tags | mandatory_tags)

if write:
nbformat.write(notebook, path)
Expand Down

0 comments on commit a58fdbe

Please sign in to comment.