Using the Jupyter notebook

The Jupyter notebook is a relatively new feature that has been implemented with Python, and allows you to write notebooks similar to e.g. Mathematica. It runs within your Web-Browser. The advantage of doing this is that you can include text, code, and plots in the same document. This makes it ideal 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: Juypter notebooks are the successor of IPython-notebooks. The latter only could be used with Python. In contrast Jupyter notebooks (Jupyter is a short-cut for Julia - Python - R) can be used with a large variety of programming languages at the same time, e.g. the statistical R-language. See this page for a complete list of languages that can be integrated into Jupyter notebooks.

Starting up

CIP Pool

If you are using a computer in the CIP pool, you will need to add the following lines:

source /usr/share/modules/init/bash
module load anaconda/36

to a file called .bashrc in your home directory. This ensures that the Anaconda Python3.6 Distribution is used by default. Then start the notebook with:

jupyter notebook

Please see the computer setup page on github for information how to set up Python3.6 at AIfA and on own computers.

First steps

Click on File -> New Notebook, which will start a new document. Ensure that you launch the notebook with a Python3-kernel if there are several possibilities. 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 (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 = 1
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.

Plotting

To make plots, enter any Matplotlib commands (see later lectures), and just press shift-enter - note that all commands for a plot should be entered in one cell, you cannot split it up over multiple cells:


In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot([1,2,3],[4,5,6])
plt.xlabel("x")
plt.ylabel("y")

As before, you can always go back and edit the cell to re-make the plot. If you want to save it, make sure you include plt.savefig(filename) as the last command, where filename is the name of the plot, such as my_plot.png.

Text (markdown cells)

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, you can use LaTeX within Markdown cells:

\begin{equation}
  E = m c^2

\end{equation}

for equations centered on a separate line, or:

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

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

\begin{equation} E = m c^2 \end{equation}

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

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.

Markdown language cheat sheet

The markdown language is extremly powerful. It allows for instance the inclusion of images, web links, videos and offers very rich text formatting possibilities. There are many good markdown tutorials and cheat sheets on the Web. You can visit this page to get started!

Note: Take some time to learn keyboard short-cuts for the most important cell-operations. You can find a list in the Help -> Keyboard Shortcuts menu or in the web, e.g. here

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.