Using the jupyter notebook on the jupyter server

The jupyter notebook is a feature that allows you to write your code in the framework of "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 jupyter notebook.

Note: If you are more comfortable, to simply write programs and execute them with 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

On the jupyter server you simply log in.

Alternatively, using a set installation (e.g., on your laptop), the normal way to start up the IPython 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. We use here the jupyter server, which runs the JupyterLab. There may be differences here and there.

First steps

In the launcher under Notebook click on python 3, which will start a new document. You can change the name of the document by right-clicking on the Untitled name at the top and entering a new name. Make sure you then save the document (make sure that you 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 [ ]:
a = 2.718
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 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:

Louis likes the equation $p=h/\lambda$ **very much**.

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

$$E = m c^2$$

Louis likes the equation $p=h/\lambda$ very much.

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

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.

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

If you know how to use 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 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 'Edit' menu item and go to Clear All Outputs then in the Run 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.

Please try out your notebooks after a fresh kernel restart before you submit your solutions.

Another word of warning

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 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!