Getting More Out of Your Jupyter Notebooks

Jupyter notebooks are incredibly powerful to develop ideas quickly, then share them if need be. The notebooks run in a web browser, and support many languages out of the box (Jupyter actually stands for Julia, Python and R, when it does not refer to Galileo’s notebook).

You can dynamically code in your favourite language, include some explanation in rich text (LaTeX, Markdown, HTML, you chose). The output is generated straight away (after hitting shift+enter), then you can publish your notebook on GitHub, nbviewer, or in a pdf/HTML page.

Developing does not get much easier than this to be honest!

Here a few tips I use daily to make the best of Jupyter notebooks.

Shortcuts

Like most software/web app, Jupyter notebook handles keyboard shortcuts. They loosely follow the vim shortcuts pattern, here, with an edit mode accessed by hitting the Esc key, and a reading/command mode, accessed by hitting the enter key.

If remembering shortcuts is not your thing, remember h is the help key which accesses the list of shortcuts (in command mode, otherwise you just typed h in the active cell).

If you are familiar with modern text editors such as sublime text or visual studio code, you might know how powerful ctrl+shift+p is. Well, Jupyter Notebooks also have such quick command access!

Switches

A notebook runs a kernel (more on that later). It can be IPython, IRkernel, IGo, etc. As one would expect, typing some text in a code cell means the interpreter will try to execute it.

But a Jupyter environment can also execute system/shell commands from a code cell. Prefix the shell command by !, and the notebook will execute the code in the scope of your system. Great for installing packages without leaving the notebook!

!pip show ipythonName: ipython
Version: 7.19.0
Summary: IPython: Productive Interactive Computing
Home-page: https://ipython.org
Author: The IPython Development Team
Author-email: ipython-dev@python.org
License: BSD
Location: /usr/lib/python3.8/site-packages
Requires:
Required-by: jupyter-console, ipykernel

Another great switch is the question mark. Prefix an environment command by ? and it will display its documentation. Another way to access this help page is to press shift+tab when the cursor is on the command word.

?list
Docstring

Last “special character” to know about, the percentage sign: %. It is known as the magic command. With this, you can run commands embedded in Jupyter notebook, such as:

  • %lsmagic, which lists all the magic commands available.
  • %time, which times the execution of a cell.
  • %timeit, which times the execution per iteration, after running the code for 100,000 times (by default).
  • %matplotlib inline, which displays plots at the end of the cell execution (even if there is no .show() method called).
  • %matplotlib notebook, which displays matplotlib in an interactive way. You can now zoom, or get point coordinates on the fly!
  • %who_ls, which will list the variables allocated in the current environment. You can add a data type as an argument to filter the output.
  • %config InlineBackend.figure_format =’retina’, which renders plots at “retina” resolution, for a smoother experience on retina display.
%system pip show ipython['Name: ipython',
'Version: 7.19.0',
'Summary: IPython: Productive Interactive Computing',
'Home-page: https://ipython.org',
'Author: The IPython Development Team',
'Author-email: ipython-dev@python.org',
'License: BSD',
'Location: /usr/lib/python3.8/site-packages',
'Requires: ',
'Required-by: jupyter-console, ipykernel']
%timeit [i*i for i in range(50)]2.61 µs ± 228 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)string_s, integer_i, tuple_t = 'wololo', 4, (35, 42)
%who_ls tuple
['tuple_t']

Kernels

As mentioned before, Jupyter notebooks run kernels. The system kernels are available out of the box, but if you are used to working in a virtual environment, chances are you will need to add those manually.

The first step is to install ipykernel in the virtual environment, then to reference the new environment to Jupyter:

# Create a folder to host the virtual environment
!mkdir virtual_environments
# Move to the folder and create the virtual environment
!cd virtual_environments
!virtualenv my_env
# Activate the virtual environment
!source my_env/bin/activate
# Install ipykernel in the virtual environment
!pip install ipykernel
# Let Jupyter know there is a new kid on the block
!python -m ipykernel install --user --name=my_env

After this, Jupyter should see your environment kernel (Check Kernel/Change kernel to see it listed there). When you are done with that environment/kernel, remove it with:

# List kernels installed
!jupyter kernelspec list
# Uninstall kernel
!jupyter kernelspec uninstall [kernelName]

Extensions

Technically, iPython bundles a couple of extensions. The most useful being autoreload, which will refresh the various scripts/libraries imported by the kernel regularly (as opposed to having to restart the kernel every time you amend your script). Add %load_ext autoreload to your notebook to activate it.

For the other extensions, we need to install the nbextensions package. The extensions can then be activated via the Nbextensions tab.

!pip install jupyter_contrib_nbextensions
!jupyter contrib nbextension install --user

Autopep8, ExecuteTime, Table of Contents and Variable inspector are definitely adding to the experience!

iPython can also be configured via commands such as the following. It activates the output of all the statements of a code cell (as opposed to the output of last one):

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

Final Thoughts

There is a lot more to Jupyter notebooks than what has been tackled in this short article. I did not talk about changing the theme, for example.

I like to keep things light and simple (yet customised), which is why I am still using Jupyter Notebooks. If you like to have extensions and a custom theme, you might want to have a look at Jupyter Labs. It is like a Jupyter Notebook on steroids.

Last thing, if you require examples/inspiration, head to this collection of notebooks. It is a great list to discover new things and realise the potential of Jupyter (and coding)!

Image for post
Image for post
Photo by Greg Rakozy on Unsplash

Data Scientist

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