Is there an easy way to check that iPython notebook code, while it\'s being written, is compliant with PEP8?
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.
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 notebookflake8 -
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`.
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
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
%load_ext pycodestyle_magic
%pycodestyle_on
or %flake8_on
depending against which style guide you want to check.
To turn off the auto-compliance-checking run:
%pycodestyle_off
or %flake8_off