Skip to content

Commit 1af946d

Browse files
authored
Use nbqa and ruff to check style of notebooks (#34)
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.
1 parent 79cd2cc commit 1af946d

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

.github/workflows/check.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Check style
2+
3+
on:
4+
# Check style after every push to main
5+
push:
6+
branches:
7+
- main
8+
# Check style on every PR
9+
pull_request:
10+
11+
jobs:
12+
13+
style:
14+
runs-on: ubuntu-latest
15+
env:
16+
PYTHON: "3.12"
17+
18+
steps:
19+
- name: Checkout repo
20+
uses: actions/checkout@v4
21+
22+
- name: Setup Python
23+
uses: actions/setup-python@v5
24+
with:
25+
python-version: ${{ env.PYTHON }}
26+
27+
- name: Install required packages
28+
run: pip install ruff nbqa
29+
30+
- name: Check style of notebooks
31+
run: make check

Makefile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
NOTEBOOKS_DIR=notebooks
2+
3+
.PHONY: build run clean check format
4+
5+
help:
6+
@echo "Commands:"
7+
@echo ""
8+
@echo " build build Myst website (without running notebooks)"
9+
@echo " clean clean output of Myst website"
10+
@echo " run run all notebooks"
11+
@echo " check lint notebooks with nbqa and ruff"
12+
@echo " format autoformat notebooks with nbqa and ruff"
13+
@echo ""
14+
15+
16+
build:
17+
msyt build --html
18+
19+
clean:
20+
msyt clean --all
21+
22+
run:
23+
jupyter nbconvert --to notebook --execute --inplace "${NOTEBOOKS_DIR}/**/*.ipynb"
24+
25+
check:
26+
nbqa ruff "${NOTEBOOKS_DIR}"
27+
28+
format:
29+
nbqa ruff --fix "${NOTEBOOKS_DIR}"

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,26 @@ jupyter nbconvert --to notebook --execute --inplace notebooks/**/*.ipynb
113113
[mystmd.org]: https://mystmd.org
114114

115115

116+
## Check style of notebooks
117+
118+
We can check the code style of our notebooks using [`ruff`][ruff] and
119+
[`nbqa`][nbqa]. Simply run the following command to check the style of the
120+
notebooks:
121+
122+
```bash
123+
nbqa ruff notebooks
124+
```
125+
126+
And run this to autoformat them:
127+
128+
```bash
129+
nbqa ruff --fix notebooks
130+
```
131+
132+
Alternatively, you can use the targets we have in the `Makefile`, like `make
133+
check` and `make format`. Read more information about the available targets
134+
by running `make help`.
135+
116136
## License
117137

118138
All text and figures are licensed under a

environment.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ dependencies:
1313
- simpeg==0.22.*
1414
- discretize==0.10.*
1515
- pymatsolver
16+
# Code quality
17+
- nbqa
18+
- ruff

0 commit comments

Comments
 (0)