The IPython web notebook is a frontend that allows for new modes
of interaction with IPython: this web-based interface allows you to execute Python and IPython
commands in each input cell just like you would at the IPython terminal, but you can
also save an entire session as a document in a file with the .ipynb
extension.
The document you are reading now is precisely an example of one such notebook, and we will show you here how to best use this new interface.
The first thing to understand is that a notebook consists of a sequence of 'cells' that can contain either text (such as this one) or code meant for execution (such as the next one):
Text cells can be written using Markdown syntax
Code cells take IPython input (i.e. Python code, %magics
, !system calls
, etc) like IPython at
the terminal or at the Qt Console. The only difference is that in order to execute a cell, you must
use Shift-Enter
, as pressing Enter
will add a new line of text to the cell. When you type
Shift-Enter
, the cell content is executed, output displayed and a new cell is created below. Try
it now by putting your cursor on the next cell and typing Shift-Enter
:
In [1]:
"This is the new IPython notebook"
Out[1]:
You can re-execute the same cell over and over as many times as you want. Simply put your
cursor in the cell again, edit at will, and type Shift-Enter
to execute.
IPython can execute shell commands, which shall be prefixed with !
.
For example, in the next cell, try issuing several system commands in-place with Ctrl-Enter
, such as pwd
and then ls
:
In [2]:
!ls
In [3]:
!ls -la
In a cell, you can type anything from a single python expression to an arbitrarily long amount of code (although for reasons of readability, you are recommended to limit this to a few dozen lines):
In [4]:
def f(x):
"""My function
x : parameter"""
return x + 1
print "f(3) = ", f(3)
When you start a new notebook server with ipython notebook
, your
browser should open into the Dashboard, a page listing all notebooks
available in the current directory as well as letting you create new
notebooks. In this page, you can also drag and drop existing .py
files
over the file list to import them as notebooks (see the manual for
further details on how these files are
interpreted).
Once you open an existing notebook (like this one) or create a new one, you are in the main notebook interface, which consists of a main editing area (where these cells are contained) as well as a collapsible left panel, a permanent header area at the top, and a pager that rises from the bottom when needed and can be collapsed again.
This panel contains a number of panes that can be collapsed vertically by clicking on their title bar, and the whole panel can also be collapsed by clicking on the vertical divider (note that you can not drag the divider, for now you can only click on it).
The Notebook section contains actions that pertain to the whole notebook,
such as downloading the current notebook either in its original format
or as a .py
script, and printing/exporting it to different markup languages.
The Cell section lets you manipulate individual cells, and the names should be fairly self-explanatory.
The Kernel section lets you signal the kernel executing your code.
Interrupt
does the equivalent of hitting Ctrl-C
at a terminal, and
Restart
fully kills the kernel process and starts a fresh one. Obviously
this means that all your previous variables are destroyed, but it also
makes it easy to get a fresh kernel in which to re-execute a notebook.
The Help section contains links to the documentation of some projects
closely related to IPython as well as the minimal keybindings you need to
know. But you should use Esc-h
(or click the QuickHelp
button at
the top) and learn some of the other keybindings, as it will make your
workflow much more fluid and efficient.
Please pay attention that there are two modes: Command mode
and Edit mode
(details in the reference: press Esc-h
).
Whenever IPython needs to display additional
information, such as when you type somefunction?
in a cell, the notebook
opens a pane at the bottom where this information is shown. You can keep
this pager pane open for reference (it doesn't block input in the main area)
or dismiss it by clicking on its divider bar.
Try by executing the following cell:
In [5]:
dict??
The notebook uses the same underlying machinery for tab completion that
IPython uses at the terminal, but displays the information differently.
Whey you complete with the Tab
key, IPython shows a drop list with all
available completions. If you type more characters while this list is open,
IPython automatically eliminates from the list options that don't match the
new characters; once there is only one option left you can hit Tab
once
more (or Enter
) to complete. You can also select the completion you
want with the arrow keys or the mouse, and then hit Enter
.
In addition, if you hit Tab
inside of open parentheses, IPython will
search for the docstring of the last object left of the parens and will
display it on a tooltip. For example, type list(<TAB>
and you will
see the docstring for the builtin list
constructor:
In [6]:
# Position your cursor after the ( and hit the Tab key:
list()
Out[6]:
As the 'tour' notebook shows, the IPython notebook has fairly sophisticated display capabilities. In addition
to the examples there, you can study the display_protocol
notebook in this same examples folder, to
learn how to customize arbitrary objects (in your own code or external libraries) to display in the notebook
in any way you want, including graphical forms or mathematical expressions.
In [7]:
%matplotlib inline
# an alternative is the following code:
# %pylab inline
# which does also many imports from numpy and matplotlib libraries and this is very handy,
# though one shall remember that global imports are evil
In [8]:
import matplotlib.pyplot as plt
plt.plot([1, 2, 4, 8, 16])
Out[8]:
IPython allows you upload files on the server and modify any text files on the server. Revisit Dashboard to check these features. Also, from Dashboard you can start shell sessions, which is good when some configuring on a server-side is needed.
However, running IPython locally is very popular option and doesn't require much experience. But the greatest benefit you can get from using IPython on your server. Note, that apart from installing IPython and giving access from network, you should protect access .
short summary of special commands available will be shown after executing this cell:
In [9]:
%quickref
http://nbviewer.ipython.org/github/ipython/ipython/blob/3.x/examples/Notebook/Index.ipynb