Verifying PEP8 in iPython notebook code

Make sure you've the module pycodestyle or flake8 to be able to check your code against the style guides. Then enable the magic function by using the pycodestyle_magic module (github repo):

pip install flake8 pycodestyle_magic
  • first load the magic in a Jupyter Notebook cell:

%load_ext pycodestyle_magic

  • and then turn on the magic to do compliance checking for each cell using:

%pycodestyle_on or %flake8_on

depending against which style guide you want to check.

enter image description here

To turn off the auto-compliance-checking run:

%pycodestyle_off or %flake8_off


In case this helps anyone, I'm using:

conttest "jupyter nbconvert notebook.ipynb --stdout --to script | flake8 - --ignore=W391"

  • conttest reruns when saving changes to the notebook
  • flake8 - tells flake8 to take input from stdin
  • --ignore=W391 - this is because the output of jupyter nbconvert seems to always have a "blank line at end of file", so I don't want flake8 to complain about that.

I'm having a problem with markdown cells (whose line lengths may legitimately be quite long, though): ignore markdown cells in `jupyter nbconvert` with `--to script`.