ndpretty

python
Author

Patrick Deutschmann

Published

January 21, 2021

I created a little Python module to display numpy ndarrays in a pretty table in Jupyter notebooks. Find the code on GitHub or get it with pip install ndpretty.

Recently I got into using Jupyter notebooks a lot. After watching Jeremy Howard’s great talk I like notebooks, I finally decided to make the jump and replace my beloved PyCharm by Jupyter notebooks as my primary development environment. As I’m not too big of a fan of doing everything in the browser, I went to Visual Studio Code with its truly excellent notebook support. I know that there is also notebook support in PyCharm, but it just always felt buggy and unpleasant to use.

Everything went well, but there was one big thing that annoyed me and that I missed from PyCharm: Looking at numpy arrays. I’m quite a visual person and enjoy looking at what my matrices and tensors look like. This is why I loved the PyCharm SciView so much:

PyCharm’s SciView for numpy arrays

When looking at them in VS code’s notebook environment, they rather looked like this, and I wasn’t a big fan:

While it’s quite okay for small arrays, I missed grasping the data by the colour highlighting and simply scrolling through a table. Also, I found I didn’t seem to be the only one with this problem, as these issues suggest. I then also explored some alternatives but wasn’t quite happy with the results either.

Therefore I just built my own thing. It’s called ndpretty, as it displays ndarrays in a pretty way.

It makes use of the flexibility offered by Jupyter notebooks. In fact, it just prints a coloured HTML table – there’s not much magic there. In order to make it that bit more convenient, I also used ipywidgets to give users the possibility to slice the arrays in a text field right above the table:

That’s quite convenient if you also want to look at arrays with dimension higher than two. Also, for very big arrays, I perform automatic slicing so that your Jupyter notebook doesn’t get sluggish due to huge HTML tables:

To make it the default way of displaying numpy arrays, I register them as IPython third-party formatters.

Overall, I’m quite happy with it, and I plan on using it a lot for my upcoming projects. You can look at the code at GitHub and get it with pip install ndpretty. Usage is as easy as importing it and calling ndpretty.default(). All subsequently executed cells that return a numpy array (or a PyTorch tensor) will be displayed using ndpretty. More customisation options are explained in the documentation.

If you want to try it out right now:

Open In Colab