Code formatting in Jupyter cells

Michael Aye
Dec 10, 2018 · 2 min read

TL;DR: I show you how to get simply pretty code in Jupyter notebooks AND Jupyter Lab.

Update (2020–03–09)

If you are using exclusively Jupyterlab, I now recommend using this extension, because one does not need to use jupyter %magics anymore, which makes cleaner notebooks: https://jupyterlab-code-formatter.readthedocs.io/en/latest/index.html

Another advantage of that extension is, that it supports 4 different code formatters (black, yapf, autopep8, isort)

Update (2019–07–31)

If you have installed an older version of nb_black , do an update ( pip install -U nb_black), because some of the recent updates were quite helpful, e.g. adding compatibility with jupyter %magics.

Introduction

Are you too busy to make code look pretty (i.e. “readable”) while developing it in Jupyter notebooks? Here’s something you might be interested in.

One of the Python core devs, Łukasz Langa, has developed a Python code formatter a while ago called Black. He calls it “the uncompromising code formatter”. What he means is, that code has “opinions” on how code should look like, but maybe you simply don’t care precisely, you just want it done efficiently, so that you can get on with your work. (I paraphrase). I like this idea, because discussing the merits of how precisely code should be aligned can be … challenging, for the lack of a better word.

Black plugins

Now, my current favorite editor VSCode (free, open source) has nice Python linting (i.e. code evaluation) enabled via different code linters and reformatters. So, you can simply enable black in there (search the settings via “python.linting.black”). But this is about Jupyter notebooks and labs.

Install Jupyter plugin and use

I just found this little wrapper around a small formatting function using black , it’s called nb_black. Here’s how you install it:

First, you install the black library using conda from the conda-forge channel:

conda install -c conda-forge black

Now you add the nb_black package via pip:

pip install nb_black

and to use it in Jupyter notebooks, you add this to the first cell of a notebook:

%load_ext nb_black

while for Jupyter Lab you add this to the first cell of a notebook:

%load_ext lab_black

Done. To use it, simply execute a cell as usual. And if you are tired of it or need it to stop doing its thing for a while, simply reload the notebook and skip executing the first cell. I think this is a convenient enough way to get pretty code cells until we get a button up there to switch it on and off. I’ll update this post if I hear anything about that.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store