Skip to content

Commit

Permalink
Use nbqa and ruff to check style of notebooks (#34)
Browse files Browse the repository at this point in the history
Add `nbqa` and `ruff` 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.
  • Loading branch information
santisoler authored Nov 20, 2024
1 parent 79cd2cc commit 1af946d
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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:
PYTHON: "3.12"

steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON }}

- name: Install required packages
run: pip install ruff nbqa

- name: Check style of notebooks
run: make check
29 changes: 29 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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}"

format:
nbqa ruff --fix "${NOTEBOOKS_DIR}"
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ dependencies:
- simpeg==0.22.*
- discretize==0.10.*
- pymatsolver
# Code quality
- nbqa
- ruff

0 comments on commit 1af946d

Please sign in to comment.