Using the IPython notebook

The IPython notebook is a new feature that has been implemented into IPython, and allows you to write notebooks similar to e.g. Mathematica. 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!

Starting up

The normal way to start up the IPython notebook is:

ipython notebook

Once you do this, your web browser should open and go to a page showing a list of folders. You can now go to the py4sci directory that you created before.

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

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 [2]:
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot([1,2,3],[4,5,6])
plt.xlabel("x")
plt.ylabel("y")
Out[2]:
Text(0,0.5,'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

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

Note: for now, the following section is a little manual, but the IPython developers are currently working on a much easier way to publish notebooks.

Once you are happy with your notebook, you can share it with other people. The file containing your notebook is the .ipynb file that is in the directory in which you started the notebook and has the correct name. We need to get this file online, so one way to do with (which we will use here) is to open the .ipynb file in your normal code editor (you can do this while the notebook is open, but don't make any changes to the file!), then select all, and copy. Then, go to http://gist.github.com and paste the content, then click 'Create Public Gist'.

In the address bar, copy the 'id' of the gist, which will look something like 5794970 or bc960df2fc71675fc85a. Then, go to http://nbviewer.ipython.org/ and enter the number in the form, and click 'Go!' - your notebook should now be visible, and you can send the address to others to share your notebook.

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 [3]:
a = 1

then go higher up and type:

In [4]:
print(a)
1

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.