23

Is it possible to have a linter inside of a Jupyter Notebook?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
magisterbrownie
  • 233
  • 1
  • 3
  • 6

4 Answers4

12
  • Yes it is possible
  • You can install pycodestyle for Jupyter Notebook which is similar to pylint. You can use the below commands from inside a Jupyter Notebook shell:
# install
!pip install pycodestyle pycodestyle_magic


# load
%load_ext pycodestyle_magic


# use
%%pycodestyle
def square_of_number(
     num1, num2, num3, 
     num4):
    return num1**2, num2**2, num3*

# Output
2:1: E302 expected 2 blank lines, found 0
3:23: W291 trailing whitespace

  • Check this question out for more details. My answer is inspired by the answers in the link.
  • Check out this question for more similar linters and solutions.
Amit Yadav
  • 4,422
  • 5
  • 34
  • 79
5

Yes - you can run any standard Python code quality tool on a Jupyter Notebook using nbQA

e.g.:

pip install -U nbqa pylint
nbqa pylint notebook.ipynb

disclaimer: I"m the author of nbQA

ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65
0

You can use FlakeHell to run any number of linters supported by flake8 on entire notebooks

RoyalTS
  • 9,545
  • 12
  • 60
  • 101
0

If you want to use the black linter in Jupyter:

pip install black "black[jupyter]"
black {source_file_or_directory}

If you want to auto-lint your notebooks with a pre-commit hook, you have to replace id: black with id: black-jupyter (more info here).

crypdick
  • 16,152
  • 7
  • 51
  • 74