Deep Learning lectures

πŸ§‘πŸΌβ€πŸ« Here is the material for a course of two-weeks I gave in a Master of Data Science and AI

View project on GitHub

jupyterlogo

Jupyter Notebook cheatsheet

In this article, I will walk you through some simple tricks on how to improve your experience with Jupyter Notebook. We will start from useful shortcuts and we will end up adding themes, automatically generated table of contents.

Introduction

As you know, Jupyter Notebook is a client-server application used for running notebook documents in the browser. Notebook documents are documents able to contain both code and rich text elements such as paragraphs, equations, etc…

Jupyter Notebook is nowadays probably the most used environment for solving Machine Learning/Data Science tasks in Python. Although it is a good debug and experiment environment and allows many nice things from the point of view of visualisation (code documentation, inline graphs, etc.), you have to keep in mind, Jupyter Notebook is not a development environment.

The main reason for this is the fact that notebooks have tons and tons of hidden state that is easy to screw up and difficult to reason about. This makes notebook really difficult to debug and to version efficiently.

It is true that nowadays there are tools out there (e.g. Ploomber) able to make you use your notebook to build pipelines and put those in production, however, my feeling is that these great tools are more a workaround such that people do not have to fix their bad habits.

Just to have a reference, there is this nice presentation at the Jupyter Conference 2018 that collects a lot of well known Jupyter notebooks environment problems.

However, since these lectures are demanded to be on jupyter notebooks, let’s start by getting the max out of it.

Commandments

  1. You will always use notebook responsibly, i.e. you never execute a notebook you do not understand just to get to the end.
  2. You will always run cells in order, if you need to rerun a previous cell, always restart the kernel.
  3. You will always prefer jupyterlab to jupyter-notebook.

Shortcuts

Shortcuts can be really useful to speed up writing and executing your code, I will now walk you through some of the shortcuts I found most useful to use in Jupyter.

There are two possible way to interact with Jupyter Notebook: Command Mode and Edit Mode.

Some shortcuts works only on one mode or another while others are common to both modes.

Common shortcuts

Some shortcuts which are common in both modes are:

  • Ctrl + Enter : to run all the selected cells;
  • Shift + Enter : run the current cell and move the next one;
  • Ctrl + s : save notebook.

Command mode shortcuts

In order to enter Jupyter command mode, we need to press Esc and then any of the following commands, the cell selection will change colour (which one depends on you theme):

  • H : show all the shortcuts available in Jupyter Notebook
  • Shift + Up/Down Arrow : to select multiple notebook cells at the same time (pressing enter after selecting multiple cells, will make run all of them!);
  • A : insert a new cell above;
  • B : insert a new cell below;
  • X : cut the selected cells;
  • Z : undo the deletion of a cell;
  • Y : change the type of cell to Code;
  • M : change the type of cell to Markdown;
  • Space : scroll notebook down;
  • Shift + Space : scroll notebook up.

Edit mode shortcuts

In order to instead enter Jupyter edit mode, we need to press Enter and successively any of the following commands:

  • Tab : code completition suggestions;
  • Ctrl + ] : indent code;
  • Ctrl + [ : dedent code;
  • Ctrl + z : undo;
  • Ctrl + y : redo;
  • Ctrl + a : select all;
  • Ctrl + Home : move cursor to cell start;
  • Ctrl + End : move cursor to the end of the cell;
  • Ctrl + Left : move cursor one word left;
  • Ctrl + Right : move cursor one word right.

Jupyter Themes

If you are interested in changing how your Jupyter notebook looks like, it is possible to install a package with a collection of different themes. The default Jupyter theme looks like the one in Figure $1$, in Figure $2$ you will see how we will be able to personalise its aspect.

Figure1 Figure2

You can install a theme package by running in a shell (or in a magic cell in a terminal)

pip install jupyterthemes

and to see the themes available,

jt -l

Finally, we can choose a theme using the following command (in this example I decided to use the solarizedl theme):

jt -t solarizedl

If you want to come back to your original theme, it is sufficient to run,

jt -r

Jupyter Notebook Extensions

Notebook extensions can be used to enhance user experience offering a wide variety of personalizations techniques.

In this example, I will be using the nbextensions library in order to install all the necessary widgets. Such library makes use of different Javascript models in order to enrich the notebook frontend.

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --system

Once nbextensions is installed you will notice that there is an extra tab on your Jupyter notebook homepage.

Figure3

By clicking on the Nbextensions tab, we will be provided with a list of available widgets. In my case, I decided to enable the ones shown below.

Figure4

Some of my favourite extensions are:

Table of Contents

Auto-generate a table of contents from markdown headings.

toc

Snippets

Sample codes to load common libraries and create sample plots which you can use as starting point for your data analysis

toc

Hinterland

Code autocompletion for Jupyter Notebooks.

toc

The nbextensions library provides many other extensions apart for these mentioned here, I encourage you to experiment and test any-other which can be of interest for you.

Markdown Options

By default, the last output in a Jupyter Notebook cell is the only one that gets printed (There is an implicit display() command).

Additionally, it is possible to write LaTex in a Markdown cell by enclosing the text between dollar signs ($).

Notebook Slides

It is possible to create a slideshow presentation of a Jupyter Notebook by going to View -> Cell Toolbar -> Slideshow and then selecting the slides configuration for each cell in the notebook.

Finally, going to the terminal and typing the following commands the slideshow will be created.

pip install jupyter_contrib_nbextensions
jupyter nbconvert my_notebook_name.ipynb --to slides --post serve

toc

Cell Magic

Magics are commands which can be used to perform specific commands. Some examples are: inline plotting, printing the execution time of a cell, printing the memory consumption of running a cell, etc…

Magic commands which starts with just one % apply their functionality just for one single line of a cell (where the command is placed). Magic commands which instead starts with two %% are applied to the whole cell.

It is possible to print out all the available magic commands by using the following command,

%lsmagic