From 98d2fa8d579a2e37564728054f1c9b944584af55 Mon Sep 17 00:00:00 2001 From: Santiago Soler Date: Mon, 18 Nov 2024 15:24:06 -0800 Subject: [PATCH 1/9] Use nbqa and ruff to check style of notebooks Add nbqa to the `environment.yml` file. Add a Makefile to the repo to easily run regular tasks like building the website, running the notebooks, checking style and autoformatting the notebooks. Add a `check.yml` GitHub Action to check style of notebooks in PRs. --- .github/workflows/check.yml | 32 ++++++++++++++++++++++++++++++++ Makefile | 29 +++++++++++++++++++++++++++++ environment.yml | 3 +++ 3 files changed, 64 insertions(+) create mode 100644 .github/workflows/check.yml create mode 100644 Makefile diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml new file mode 100644 index 00000000..b75e0161 --- /dev/null +++ b/.github/workflows/check.yml @@ -0,0 +1,32 @@ +name: Check style + +on: + # Check style after every push to main + push: + branches: + - main + # Check style on every PR + pull_request: + +jobs: + + style: + runs-on: ubuntu-latest + env: + ENVIRONMENT_FILE: environment.yml + ENVIRONMENT_NAME: simpeg-user-tutorials + + steps: + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Setup Miniforge + uses: conda-incubator/setup-miniconda@v3 + with: + miniforge-version: Miniforge3 + environment-file: $ENVIRONMENT_FILE + activate-environment: $ENVIRONMENT_NAME + + - name: Check style of notebooks + bash: + make check diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..4e7b0c1b --- /dev/null +++ b/Makefile @@ -0,0 +1,29 @@ +NOTEBOOKS_DIR=notebooks + +.PHONY: build run clean check format + +help: + @echo "Commands:" + @echo "" + @echo " build build Myst website (without running notebooks)" + @echo " clean clean output of Myst website" + @echo " run run all notebooks" + @echo " check lint notebooks with nbqa and ruff" + @echo " format autoformat notebooks with nbqa and ruff" + @echo "" + + +build: + msyt build --html + +clean: + msyt clean --all + +run: + jupyter nbconvert --to notebook --execute --inplace "${NOTEBOOKS_DIR}/**/*.ipynb" + +check: + nbqa ruff "${NOTEBOOKS_DIR}/**/*.ipynb" + +format: + nbqa ruff --fix "${NOTEBOOKS_DIR}/**/*.ipynb" diff --git a/environment.yml b/environment.yml index b2bfc159..6ede2116 100644 --- a/environment.yml +++ b/environment.yml @@ -12,3 +12,6 @@ dependencies: - simpeg==0.22.* - discretize==0.10.* - pymatsolver + # Code quality + - nbqa + - ruff From 3591c6f7bf68daf0e7f8adf0db144dcb50a9aeba Mon Sep 17 00:00:00 2001 From: Santiago Soler Date: Mon, 18 Nov 2024 15:28:40 -0800 Subject: [PATCH 2/9] Add missing pipe --- .github/workflows/check.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index b75e0161..59aedaad 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -28,5 +28,5 @@ jobs: activate-environment: $ENVIRONMENT_NAME - name: Check style of notebooks - bash: + bash: | make check From 02d337ac046d00c43b27dc421fb6e5ede723a460 Mon Sep 17 00:00:00 2001 From: Santiago Soler Date: Mon, 18 Nov 2024 15:31:13 -0800 Subject: [PATCH 3/9] Fix syntax --- .github/workflows/check.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 59aedaad..19a39e7a 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -28,5 +28,4 @@ jobs: activate-environment: $ENVIRONMENT_NAME - name: Check style of notebooks - bash: | - make check + run: make check From 746d70894abb39e0311c25622bdf1da62610c82a Mon Sep 17 00:00:00 2001 From: Santiago Soler Date: Mon, 18 Nov 2024 15:34:12 -0800 Subject: [PATCH 4/9] Set miniforge-version to latest --- .github/workflows/check.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 19a39e7a..bd83f8ea 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -23,7 +23,7 @@ jobs: - name: Setup Miniforge uses: conda-incubator/setup-miniconda@v3 with: - miniforge-version: Miniforge3 + miniforge-version: latest environment-file: $ENVIRONMENT_FILE activate-environment: $ENVIRONMENT_NAME From b2e1535c15e4ee6299a854ff7b74c94eb5067052 Mon Sep 17 00:00:00 2001 From: Santiago Soler Date: Mon, 18 Nov 2024 15:35:34 -0800 Subject: [PATCH 5/9] Fix syntax for accessing env variables --- .github/workflows/check.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index bd83f8ea..1f112760 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -24,8 +24,8 @@ jobs: uses: conda-incubator/setup-miniconda@v3 with: miniforge-version: latest - environment-file: $ENVIRONMENT_FILE - activate-environment: $ENVIRONMENT_NAME + environment-file: ${{ env.ENVIRONMENT_FILE }} + activate-environment: ${{ env.ENVIRONMENT_NAME }} - name: Check style of notebooks run: make check From 56581fc650dc00dc3ad5d96cdf282bb2217f558f Mon Sep 17 00:00:00 2001 From: Santiago Soler Date: Mon, 18 Nov 2024 15:40:11 -0800 Subject: [PATCH 6/9] Check if the environment was activated --- .github/workflows/check.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 1f112760..967f3297 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -26,6 +26,12 @@ jobs: miniforge-version: latest environment-file: ${{ env.ENVIRONMENT_FILE }} activate-environment: ${{ env.ENVIRONMENT_NAME }} + auto-activate-base: false + + - name: List installed packages + run: | + conda info + conda list - name: Check style of notebooks run: make check From 70067e30f6eebb7250a8262922f01409808a2f06 Mon Sep 17 00:00:00 2001 From: Santiago Soler Date: Mon, 18 Nov 2024 15:43:36 -0800 Subject: [PATCH 7/9] Install ruff and nbqa through pip --- .github/workflows/check.yml | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 967f3297..c77650cd 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -13,25 +13,19 @@ jobs: style: runs-on: ubuntu-latest env: - ENVIRONMENT_FILE: environment.yml - ENVIRONMENT_NAME: simpeg-user-tutorials + PYTHON: "3.12" steps: - name: Checkout repo uses: actions/checkout@v4 - - name: Setup Miniforge - uses: conda-incubator/setup-miniconda@v3 + - name: Setup Python + uses: actions/setup-python@v5 with: - miniforge-version: latest - environment-file: ${{ env.ENVIRONMENT_FILE }} - activate-environment: ${{ env.ENVIRONMENT_NAME }} - auto-activate-base: false + python-version: ${{ env.PYTHON }} - - name: List installed packages - run: | - conda info - conda list + - name: Install required packages + run: pip install ruff nbqa - name: Check style of notebooks run: make check From 7d3f4f3efd7c4b541cce0c90d2d887150f965d7f Mon Sep 17 00:00:00 2001 From: Santiago Soler Date: Mon, 18 Nov 2024 15:45:43 -0800 Subject: [PATCH 8/9] Change targets in Makefile Avoid globing, since nbqa can recursively find notebooks in the dir. --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 4e7b0c1b..328f21bc 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ run: jupyter nbconvert --to notebook --execute --inplace "${NOTEBOOKS_DIR}/**/*.ipynb" check: - nbqa ruff "${NOTEBOOKS_DIR}/**/*.ipynb" + nbqa ruff "${NOTEBOOKS_DIR}" format: - nbqa ruff --fix "${NOTEBOOKS_DIR}/**/*.ipynb" + nbqa ruff --fix "${NOTEBOOKS_DIR}" From d5667257e5b4b0dc4d71993fd70e1a56e83524c2 Mon Sep 17 00:00:00 2001 From: Santiago Soler Date: Tue, 19 Nov 2024 16:49:51 -0800 Subject: [PATCH 9/9] Add instructions to the README.md --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index 8c364a5a..d6553611 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,26 @@ jupyter nbconvert --to notebook --execute --inplace notebooks/**/*.ipynb [mystmd.org]: https://mystmd.org +## Check style of notebooks + +We can check the code style of our notebooks using [`ruff`][ruff] and +[`nbqa`][nbqa]. Simply run the following command to check the style of the +notebooks: + +```bash +nbqa ruff notebooks +``` + +And run this to autoformat them: + +```bash +nbqa ruff --fix notebooks +``` + +Alternatively, you can use the targets we have in the `Makefile`, like `make +check` and `make format`. Read more information about the available targets +by running `make help`. + ## License All text and figures are licensed under a