Tips to make it easier to comply with the PEP8 style guide

Read the style guide here. Please add any additional tips or tricks you discover!

Tip #1 - line length

One challenge I have in ipython notebook is writing lines that are too long (>79 char), but I don't realize it because the notebook window in my browser is set quite wide. Here is a way to make your ipython notebook cells show an 80 character-wide window so you know when you've written too long of a line.

You are going to make a new profile (or if you want you can edit the default) with a custom css to change how the code cells look. You can do all this from ipython (as I've done here), or you can do it in terminal if you prefer. Change the directory names as appropriate below.

NOTE: these instructions for creating a custom profile don't work if you've upgraded to ipython or jupyter 4.x (+)

Instructions for these newer versions are included at the end of this section


In [7]:
%%bash
ipython profile create blake
mkdir /Users/houghb/.ipython/profile_blake/static/
mkdir /Users/houghb/.ipython/profile_blake/static/custom/
touch /Users/houghb/.ipython/profile_blake/static/custom/custom.css


mkdir: /Users/houghb/.ipython/profile_blake/static/: File exists
mkdir: /Users/houghb/.ipython/profile_blake/static/custom/: File exists

The file that you edit in the next cell (custom.css) will change how your notebook cells look. You can add other customizations, but the one I've included here will make the shaded area of code cells 80 characters long (assuming you have the same default font that I do, you may need to edit the width for your system if you change the font). You will still be able to type lines longer than 80 characters if you want, but will need to scroll left and right to see them.

If you want to make all cells in your notebook (including Markdown cells) only 80 characters then substitue "cell" for "input" in this code.


In [8]:
%%file /Users/houghb/.ipython/profile_blake/static/custom/custom.css
/**This is Blake's custom css file**/

div.input{
    width:107ex;  /* on my system this is an 80 char window */
}


Overwriting /Users/houghb/.ipython/profile_blake/static/custom/custom.css

To use your newly created custom.css you need to start ipython notebook with the command following command.

ipython notebook --profile=blake (substitute your profile name instead of blakecss)

If you've updated to ipython or jupyter 4.X(+):

Instead of saving your file in .../.ipython/profile_blake/static/custom/custom.css, save it in the .jupyter directory:


In [9]:
%%file /Users/houghb/.jupyter/custom/custom.css
/**This is Blake's custom css file**/

div.input{
    width:107ex;  /* on my system this is an 80 char window */
}


Overwriting /Users/houghb/.jupyter/custom/custom.css

Tip #2 - PEP8 cell magic

This doesn't work for me, and I think it is because I am still using python 2.7, but if you have upgraded to python 3.x then it might work for you and would be really helpful. From the examples I've seen, this cell magic will use the pep8 package to check the code in a notebook cell and identify everything that is not PEP8 compliant. You can also do this yourself on a .py file from terminal using the pep8 package, but this magic would be much more convenient if you are already using python 3. Get instructions here: https://github.com/ipython/ipython/wiki/Extensions-Index#pep8

As an alternative, Michael found this website that does the same thing, all you need to do is copy and paste your code: http://pep8online.com/


In [ ]: