Is there an easy way to check that iPython notebook code, while it's being written, is compliant with PEP8?
问题:
回答1:
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.
回答2:
In case this helps anyone, I'm using:
conttest "jupyter nbconvert notebook.ipynb --stdout --to script | flake8 - --ignore=W391"
conttestreruns when saving changes to the notebookflake8 -tells flake8 to take input from stdin--ignore=W391- this is because the output ofjupyter nbconvertseems 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`.
回答3:
Make sure you've the module pycodestyle to be able to check your code against the style guide. Then enable the magic function by using the pycodestyle_magic module:
pip install pycodestyle pip install pycodestyle_magic - first load the magic in a Jupyter Notebook cell:
%load_ext pycodestyle_magic
- and then use the function in your cell to check compliance with
pycodestyle
%%pycodestyle
