Skip to content

Commit

Permalink
Add GitHub Action to run style checks on notebooks (#6)
Browse files Browse the repository at this point in the history
Use `nbqa` to run `ruff` and `black` to check style format of notebooks.
Add a `Makefile` with targets for checking and formatting the notebooks.
Add a `.github/requirements-style.yml` file with dependencies to run the
checks.
  • Loading branch information
santisoler authored Jan 18, 2024
1 parent 7b30b6d commit 30e66d2
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/requirements-style.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
nbqa==1.7.1
ruff==0.1.13
black==23.12.1
65 changes: 65 additions & 0 deletions .github/workflows/style.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Linting and style checks with GitHub Actions
#
# NOTE: Pin actions to a specific commit to avoid having the authentication
# token stolen if the Action is compromised. See the comments and links here:
# https://github.com/pypa/gh-action-pypi-publish/issues/27
#
# Use nbQA to run linters and code formatters on notebooks
#
name: code-style

# Only build PRs and the main branch. Pushes to branches will only be built
# when a PR is opened.
on:
pull_request:
push:
branches:
- main

###############################################################################
jobs:
ruff:
name: ruff
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
persist-credentials: false

- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: "3.11"

- name: Install requirements
run: pip install -r .github/requirements-style.txt

- name: List installed packages
run: pip freeze

- name: Check code format
run: make ruff

black:
name: black
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
persist-credentials: false

- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: "3.11"

- name: Install requirements
run: pip install -r .github/requirements-style.txt

- name: List installed packages
run: pip freeze

- name: Check code format
run: make black
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
NOTEBOOKS_DIR=notebooks

.PHONY: help black ruff check format

help:
@echo "Commands:"
@echo ""
@echo " check run code style and quality checks (black and ruff)"
@echo " format run black and ruff to format notebooks"
@echo ""

check: ruff black

ruff:
nbqa ruff ${NOTEBOOKS_DIR}/*.ipynb

black:
nbqa black --check ${NOTEBOOKS_DIR}/*.ipynb

format:
nbqa black ${NOTEBOOKS_DIR}/*.ipynb
nbqa ruff --fix ${NOTEBOOKS_DIR}/*.ipynb
4 changes: 4 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ dependencies:
- ipython
- jupyter
- jupyterlab
# Linters
- nbqa==1.7.1
- ruff==0.1.13
- black==23.12.1

0 comments on commit 30e66d2

Please sign in to comment.