Using the Jupyter notebook

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. In fact, the notes for this course are written using the IPython notebook.

Note: If you are more comfortable, however, to simply write programs and execute them python or ipython, that is OK! 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 up

The normal way to start up the Jupyter notebook is to open a shell, go to the directory containing your notebooks and say:

jupyter notebook

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

(Note: There are also different, but much less common, notebook types available but you don't need to worry about that.)

First steps

Click on File on the top menu and select New Notebook --> Python 3. This 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 then save the document (you should save regularly as you might lose content if 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 this, you will see 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 get executed:

In [1]:
a = 1
print(a)
1

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 once, a new cell will appear below. You can again enter some code, then press shift-enter to execute it.

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 going to Edit and selecting the appropriate command. Some of the commands are also available in the toolbar - put your mouse over the icon and wait for a second, and it will tell you what it does.

Sharing your notebook

Jupyter 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.

Find the files under the name you gave the notebook, with an ipynb extension.

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

Important notes

A few important notes about using the notebook:

  • Save often! There is an auto-save in the notebook, but better to also save explicitly from time to time.

  • 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. 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 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, etc.

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

Data Files used in this course

There are a number of data files that we will need at different points during this course. There are stored in separate directories on the same level as the .ipynb files. You can move them, if you want, to a different location, but be aware that you will then need to redefine the paths to these files in your own code if you do so. Otherwise you will get File not found error messages.

In [ ]: