Verifying PEP8 in iPython notebook code

半城伤御伤魂 提交于 2019-11-27 10:29:57

问题


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


回答1:


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




回答2:


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

To turn off the auto-compliance-checking run:

%pycodestyle_off or %flake8_off




回答3:


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.



来源:https://stackoverflow.com/questions/26126853/verifying-pep8-in-ipython-notebook-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!