Jupyter Notebook: Little-Known Tricks!

Jupyter Notebook: Little-Known Tricks!

We’ve written before about the evolution of Project Jupyter, how the notebook is great for teams, and how it can be even used with Apache Spark. And there is still so much more of it worth exploring!

Jupyter (formerly IPython) has an extremely rich set of functionalities and great potential for customization, which makes it the ideal tool to prototype, share, present, and collaborate on data science and research projects.

Here we want to share three lesser-known features of the notebook that we use all the time at 3Blades, and that greatly enhance its capabilities. So let’s get right into it!

1. Internal and external hyperlinks

A computational notebook would not be complete without the ability to add hyperlinks to different sections of the same notebook, or to different notebooks. This allows, for example, to create a table of contents or a self-referenced multi-page report.

Internal hyperlinks, i.e. within the same notebook, can be implemented with a mixture of Markdown and HTML. Just add the anchor to the Markdown cell you want to refer to, and link to it using standard Markdown syntax.

For example, the anchor can be the following:

<a id=’section_1’></a>
### Section 1

and the reference can be like:

For more details, see [Section 1](#section_1).

Notice the pound sign before the reference. Here is how the above cells look when rendered:

Section 1 link appears after running cell

The anchor will not be rendered as text, as Markdown is a superset of HTML and it allows arbitrary HTML code for formatting, including images and videos, and other functionalities.

External hyperlinks, i.e. between different notebooks, are even easier to implement. Simply use the path of the target notebook, without the pound sign this time:

For more details, see [my other notebook](./notebook_2).

The two methods can be combined if you want to link to a specific section of the second notebook, e.g.

For more details, see [my other notebook](./notebook_2#section_1).

2. Automatic module reload

Wouldn’t it be great to load an external python module in your notebook, and have it automatically reloaded when it changes, without re-executing the import command every time? Well, Jupyter has your back!

First, use the %load_ext line magic to load an extension called autoreload, then use the extension followed by a 2. So something like:

%load_ext autoreload
%autoreload 2
import my_module

That’s it! Now my_module and all other modules will be reloaded every time before executing the Python code typed below this cell.

3. Run demos

Any Python script can be loaded into a notebook and, with a minimal amount of markup, be used to present interactive demos to an audience by controlling its execution flow.

When it comes to programming (and data science in particular), this is a much better alternative than a PowerPoint presentation; seeing how the output changes when specific variables and model parameters change allows for a much richer experience.

All is needed here is to separate the Python script into blocks by using a tag at the end of each block. The tag looks like this:

# <demo> — — stop — -

Two additional tags can be used to create blocks with different purposes:

# <demo> silent

Silent block, executed but not printed.

# <demo> auto

Automatic block, executed without asking:

So the file demo.py might look something like:

a = 2
b = 3
# <demo> — — stop — -
c = a * b
print(“c = ”, c)

Now that our script is ready, we can load it as a demo in a notebook,

from IPython.lib.demo import Demo
next_block = Demo(‘demo.py’);

Notice the semi colon at the end of the last line, it will prevent the output of the whole demo to be printed to screen under some circumstances (bonus trick!). Then, every time you want to execute a block, just type


and the notebook will first print the block, then ask whether you want to execute it or quit the demo.

Feel free to try these out! Any comments are welcome ;-).

Like what you read? Give Greg Werner a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.