Skip to content

GitHub Action that runs flake8 code checks on Python code within Jupyter Notebooks

License

Notifications You must be signed in to change notification settings

mhitza/flake8-jupyter-notebook

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A container and a wrapper script around flake8 to validate python code within Jupyter notebooks.

Motivation

An easy way to automate flake8 code checks over code blocks defined in a Jupyter notebook.

Example usage

jobs:
  flake8:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4
    - uses: mhitza/flake8-jupyter-notebook@v1

annotation-screenshot

Implementation details

There is an existing project, called flake8-nb that performs the same task as this action. While initial implementation tried to wrapp the annotation script around that utility, it was abandoned and instead flake8 was used because:

  1. flake8-nb did not report absolute line number within the notebook file, instead it reported only relative line numbers within the checked code blocks.
  2. A notebook might be checked in the repository without the code cells evaluated. In that case flake8-nb would report on cells without a number, and tracking back from the reported error to absolute line numbers became a more difficult task than wrapping around flake8.

In order to check the notebook, the annotate script keeps track of all the various code blocks within the notebook, concatenates them into a single source and pipes it into flake8.

Known limitations

Supports version 4 compatible notebook formats. It will just silently skip over other notebook formats, as it's using regular expressions based on indentation level to extract source blocks. If you're of aware of any JavaScript JSON parser that keeps track of the source lines parsed I'd be happy to hear about it.

Due to implementation details and Jupyter notebook specific idiosyncrasies, some warnings and errors reported by flake8 are ignored by default (hardcoded in source code). The following list is not necessarily exhaustive and might change based on testing and issues raised.