diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml new file mode 100644 index 00000000..c77650cd --- /dev/null +++ b/.github/workflows/check.yml @@ -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 diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..328f21bc --- /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}" + +format: + nbqa ruff --fix "${NOTEBOOKS_DIR}" 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 diff --git a/environment.yml b/environment.yml index 559c9e83..fc489338 100644 --- a/environment.yml +++ b/environment.yml @@ -13,3 +13,6 @@ dependencies: - simpeg==0.22.* - discretize==0.10.* - pymatsolver + # Code quality + - nbqa + - ruff