Using the IPython notebook (now known as 'Jupyter notebook') on the Jupyter server

The Jupyter notebook is a feature that allows you to write "notebooks" similar to, e.g., Mathematica or Matlab. An advantage of doing this is that you can include text, code, and plots in the same document. This makes it suitable for example to write up a report about a project that uses mostly Python code, in order to share with others. They are also great to make notes for your lab courses, record data and analyse it. In fact, the notes for this course are written using the Jupyter notebook.

Note: If you are more comfortable to simply write programs and execute them with python or ipython, that is fine! There is no obligation to use notebooks. Make sure your problem sets run/finish with python3/ipython3 and that they create all the necessary output.

Starting a session

On the Jupyter server you simply log in with your URZ account. A folder called "data" will automatically be created that contains data for later use.

Alternatively, using a Python installation, e.g. on your laptop, the normal way to start an IPython notebook is to open a shell, go to the directory containing your notebooks and execute:

ipython notebook

Once you do this, your web browser should open and go to a page showing a list of folders and notebooks.

In more recent versions of ipython this has become

jupyter notebook

(there are also different notebook versions involved)

First steps

Click on New Notebook on the right, which will start a new document. You can change the name of the document by clicking on the Untitled name at the top and entering a new name. Make sure you save the document (while the notebook is automatically saved from timt to time, please ensure that you save regularly to prevent data loss when you close the browser window!).

At first glance, a notebook looks like a fairly typical application - it has a menubar (File, Edit, View, etc.) and a tool bar with icons. Below them, you will find an empty cell, in which you can type any Python code. You can write several lines of code, and once it is ready to run, you can press shift-enter and it will be executed:

In [ ]:
a = 3.14
print(a)

You can then click on that cell, change the Python code, and press shift-enter again to re-execute the code. Once you have executed a cell, a new cell appears. You can again enter some code and press shift-enter for execution.

Text

It is likely that you will want to enter actual text (non-code) in the notebook. To do this, click on a cell, and in the drop-down menu in the toolbar, select 'Markdown'. This is a specific type of syntax for writing text. You can just write text normally and press shift-enter to render it:

This is some plain text

To edit it, double click on the cell. You can also enter section headings using the following syntax:

This is a title
===============

This is a sub-title
-------------------

which will look like:

This is a title

This is a sub-title

Finally, if you are familiar with LaTeX, you can enter equations using:

$$E = m c^2$$

on a separate line, or:

The equation $p=h/\lambda$ is very **important**

to include it in a sentence. This will look like:

$$E = m c^2$$

The equation $p=h/\lambda$ is very important

For more information about using LaTeX for equations, see this guide.

Splitting/deleting/moving cells

You can split, delete, and move cells by selecting the apropriate item from the 'Edit' menu. Some of the commands are also available in the toolbar - browse your mouse over the icon and briefly wait to see what it does.

Sharing your notebook

Jupyter/IPython notebooks are plain text files – you can view them with a text editor, and other people can dump them into their own folders to re-run them.

By default, notebooks can be found under the name you gave them with an .ipynb extension.

Of course, if you know version control, the notebooks can be used with diff and merge. If you are already using subversion or github: try it with your notebooks.

Important notes

A few important notes about using the notebook:

  • Save regularly! There is an auto-save in the notebook, but better be safe and save explicitly from time to time (on most OS, Ctrl-S is your friend).

  • Code can be executed in an order different from top to bottom, but note that if you do this variables will not be reset. So for example if you type:

In [ ]:
a = 1

then go higher up and type:

In [ ]:
print(a)

it will give the value you previously set at the bottom of the document. To make sure that your code works from top to bottom, go to the 'Cell' menu item and go to All Output -> Clear then in the Cell menu, select Run All.

In addition, even if you remove a cell, then variables set in that cell still exist unless you restart the notebook. If you want to restart a notebook, you can select Kernel -> Restart. This removes any variables from memory, and you have to start running the notebook from the start.

Another word of warning

Jupyter/IPython notebooks contain executable code. Executing a notebook of unknown origin is dangerous. From within such a notebook, people can do anything you can do, e.g., delete files, create backdoors to your system, etc.

The notebook runner will not execute untrusted code without your doing. If you see code you don't understand (in particular if it contains calls from modules like os, subprocess, sys, and so on): Do not execute!