Verifying PEP8 in iPython notebook code
Asked Answered
M

7

54

Is there an easy way to check that iPython notebook code, while it's being written, is compliant with PEP8?

Merovingian answered 30/9, 2014 at 17:41 Comment(0)
A
15

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`.

Anacoluthon answered 24/5, 2016 at 20:39 Comment(1)
This will result in false positives if the IPython notebook contains magics. I'd suggest conttest "nbqa flake8 notebook.ipynb --extend-ignore=W391" instead. This will also ignore the markdown cellsPrime
S
45

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

Shipe answered 9/11, 2017 at 14:28 Comment(10)
rephrased the first sentence, glad it's helpfulShipe
Does this not work anymore? I get "ModuleNotFoundError: No module named 'pycodestyle_magic'" even though it's installed.Electuary
Good thing, but seems like it multiply outputs an error message and does not cleaning before cell is executed. Is there a way to get rid of multiple error messages?Alek
@Alek could you file an issue at the GitHub page of pycodestyle_magic? Thanks!Shipe
This is the new best answer. Exactly what I have been looking forDelaney
I'm not sure if this answer works on Google Colaboratory. The packages installed fine, but I'm getting a strange error trying to run either %flake8_on or %pycodestyle_off: TypeError: auto_run_flake8() missing 1 required positional argument: 'result' and TypeError: auto_run_pycodestyle() missing 1 required positional argument: 'result'.Jeremiad
This works fine, what about auto edit the code in cell?Dovev
What about disabling checks? I tried to add a tox.ini but seems like it's ignored.Snoopy
Looks like pycodestyle_magic is now unmaintainedPrime
This has not worked for me either when attempting to run %pycodestyle_on or %flake8_on . Returing an example error message: Error in callback <bound method VarWatcher.auto_run_pycodestyle of <pycodestyle_magic.VarWatcher object at 0x000001D8E3361908>> (for post_run_cell): --------------------------------------------------------------------------- TypeError Traceback (most recent call last) TypeError: auto_run_pycodestyle() missing 1 required positional argument: 'result'Defecate
A
15

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`.

Anacoluthon answered 24/5, 2016 at 20:39 Comment(1)
This will result in false positives if the IPython notebook contains magics. I'd suggest conttest "nbqa flake8 notebook.ipynb --extend-ignore=W391" instead. This will also ignore the markdown cellsPrime
G
10

Install the pep8 extension for ipython notebook using the following command :

%install_ext https://raw.githubusercontent.com/SiggyF/notebooks/master/pep8_magic.py

Refer the official docs for more info.

After that use the %%pep8 Cell magic function to check your particular cell for pep8 styling.

Note that this has to be put inside every cell for which pep8 checking needs to be enforced.

Refer this example.

Gantrisin answered 5/10, 2014 at 13:27 Comment(4)
install_ext has been deprecated and removed. Extensions now need to be published on PyPI and installed with pip.Roomy
pep8 is deprecated and extensions now needs to be published on PyPi, see answer https://mcmap.net/q/336368/-verifying-pep8-in-ipython-notebook-code or srcoll downShipe
I was unable to get this to work and as per @Shipe +1 this approach is no longer viable.Mitchellmitchem
No longer viable answer.Judicial
P
3

I'd suggest using nbQA for this:

pip install -U nbqa flake8
conttest "nbqa flake8 notebook.ipynb"

as it'll skip markdown cells and robustly handle IPython magics.

Issues with the other answers are:

  • "jupyter nbconvert" won't be robust to magics
  • pycodestyle_magic is unfortunately no longer maintained (last commit is from 2019)
  • install_ext is deprecated

disclaimer: I'm the author of nbQA

Prime answered 14/7, 2021 at 10:33 Comment(0)
B
1

I like keeping my notebooks tidy using the combination of the black linter and pre-commit git hooks (to automatically lint with every commit).

pip install pre-commit "black[jupyter]"
# <-- create your .pre-commit-config.yaml
pre-commit install
black {source_file_or_directory}
# your notebooks will be linted whenever you commit now

For instructions on how to setup your .pre-commit-config.yaml, see the black docs) and pre-commit docs.

Bluff answered 13/12, 2021 at 14:3 Comment(0)
C
0

You could include parts of my script (link below) to see details which code lines need specific PEP8 corrections. You could also runs it independently.

The script includes PEP8 comments into the py file code based on flake8. This makes it very easy to immediately jump to the place where the correction of the style is needed.

You can check it here: https://github.com/Softerec/PEP8-commenter

Colburn answered 3/12, 2019 at 13:23 Comment(1)
Hello! Please write out the specific part of your code that answers the question, don't link to it.Michaelmas
D
0

If you want to free yourself from bugs in the code or program that will pay you too much attention to EPE8, install two correction programs at once.

we install it:

pip install nb_black   
pip install flake8 pycodestyle_magic

paste into the code at the beginning:

%load_ext nb_black
%load_ext pycodestyle_magic 
%pycodestyle_on 

attention the order is important

Dorcasdorcea answered 9/12, 2021 at 17:23 Comment(1)
This looks like a good solution. Could you elaborate a little more on what each package is doing, particularly nb_black, and why the order matters?Turnsole

© 2022 - 2024 — McMap. All rights reserved.