Welcome to Wakari

Wakari has tight integration with the IPython notebook.

  • Click the New Notebook button in the navigation bar to create new IPython notebooks.

  • Use the file browser on the left to open any existing notebooks

  • Click on the share icon in the file browser button bar to share a notebook with someone else

An introduction to the IPython notebook

The IPython 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 or Qt console, 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 notebooks, and we will show you here how to best use this new interface.

The first thing to understand is that the user interface of the notebook is now modal. In a modal user interface, there are different modes of keyboard inputs. The notebook has two modes:

  • Edit mode: for typing into a cell
  • Command mode: for performing notebook and cell actions

The second one 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):

  • Markdown cells can be written using Markdown syntax. When a Markdown cell is executed, the Markdown code is converted into the corresponding formatted rich text. You can also include mathematics using standard LaTeX notation. When the Markdown cell is executed, the LaTeX portions are automatically rendered in the HTML output as equations with high quality typography.

  • Header cells can provide a conceptual structure for your computational document as a whole using different levels of headings. There are 6 levels available, from level 1 (top level) down to level 6 (paragraph). As with Markdown cells, a heading cell is replaced by a rich text rendering of the heading when the cell is executed.

  • Raw cells provide a place in which you can write output directly. Raw cells are not evaluated by the notebook. When passed through nbconvert, raw cells arrive in the destination format unmodified. For example, this allows you to type full LaTeX into a raw cell, which will only be rendered by LaTeX after conversion by nbconvert.

  • 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 in Edit mode will add a new line of text to the cell. When you type Shift-Enter, the cell content is executed, output displayed and the next cell is selected (or a new cell is created if the next one doesn't exist yet). 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]:
'This is the new IPython notebook'

You can re-execute the same cell over and over as many times as you want. If you are in Command mode, select the cell of interest and press Enter to activate the Edit mode in that cell or, simply, put your cursor in the cell of interest, edit at will, and type Shift-Enter to execute.

Tip: A cell can also be executed in-place, where IPython executes its content but stays in the same cell. This is done by typing Ctrl-Enter instead, and is useful if you want to quickly run a command to check something before typing the real content you want to leave in the cell. For example, in the next cell, try issuing several system commands in-place with Ctrl-Enter, such as pwd and then ls (you will need to shift to Edit mode after using Ctrl-Enter, just typing Enter):


In [1]:
ls


00_notebook_tour.ipynb*
01_notebook_introduction.ipynb*
auto-mpg.csv*
close.png
csna3_ohlc.csv
img-df-100MA.png
img-df-3d-01.png
img-df-3d-HLxClose.png
img-df-3d-HLxVol.png
img-df-Close+100MA.png
img-df-hpm-100MA.png
img-df-inteiro.png
img-df-mpl-close-std.png
img-df-ohlc+100MA.png
ising_model.py*
logo.png*
manipulacao-estatistica-de-dados-meteorologicos.ipynb
mlb_analysis.py*
nba_analysis.py*
nfl_analysis.py*
nhl_analysis.py*
numba_mandelblot.py*
pairs_plotting.py*
pandas_example.py*
python-logo.svg*
uefa_2012_analysis.py*
weather_station.py*
webplot_example.py*

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 should probably limit this to a few dozen lines):


In [2]:
def f(x):
    """My function
    x : parameter"""
    
    return x+1

print "f(3) = ", f(3)


f(3) =  4

User interface

When you start a new notebook server with ipython notebook, your browser should open into the Dashboard, a page listing all folders and notebooks available in the current directory as well as letting you create new notebooks. In this page, you can also drag and drop existing .ipynb notebook files over the file list to import them (see the manual for further details). IPython also supports multi-directory navigation from the Dashboard.

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 fixed header bar and a toggle-able menu at the top, and a pager that rises from the bottom when needed and can be collapsed again.

Main editing area

Here, you can move with the arrow keys or using the scroll bars. The cursor enters code cells immediately, but only selects text (markdown) cells without entering in them; to enter a text cell, use Enter, to exit it use Esc and to render it use Shift-Enter (just like to execute a code cell).

Header bar

The header area at the top allows you to rename an existing notebook and open up the Dashboard just clicking on the IP[y]: Notebook logo. This area also indicates the day and time when the last Checkpoint was created. For more information about Saving and Checkpoints see below.

The Menu bar presents different options that may be used to manipulate the way the notebook functions.

The File 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 it. When you click the Print button, a new HTML page opens with a static copy of the notebook; you can then use your web browser's mechanisms to save or print this file. You can also Save your document or revert to a previous Checkpoint, make a copy of the current notebook or open a new one.

The Edit section contains actions that pertain to the manipulation of cells, such as cut, copy and paste cells. Move up or down cells, merge above or below the current cell, and finally, access to edit the notebook metadata.

The View section lets you toggle the Menu and the Toolbar.

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, perhaps after changing an extension module for which Python's reload mechanism does not work. When you close the page IPython will not automatically shut down the running kernel (use Close and halt from the File menu to do it). The kernels won't close until you stop them at the Dashboard or hitting Ctrl-C at a terminal.

The Help section contains links to the documentation of IPython itself and to some projects closely related to IPython as well as the keybindings you need to know. You should press h (or click the Keyboard Shortcuts button at the top of Help section) and learn some of the keybindings, as it will make your workflow much more fluid and efficient.

At the right corner of the Menu bar you will see two icons. The first one is a pencil icon and it will be shown when you are in Edit mode. The second one is a full circle when the kernel is running computations and a empty circle otherwise.

The Tool bar gives a quick way of performing the most-used operations within the notebook, by clicking on an icon. The icons are self-explanatory, but if you have doubts about the function they trigger just put your cursor over them and hover with minimal information will show you which function that icon executes.

Finally, you will have two dropdown menus:

  • the Cell Tollbar, which enable a new toolbar above each cell and let you modified cell-level metadata.
  • the Environment (Wakaki-specific feature), which lets you select the environment (unique version of Python and NumPy, along with various versions of other packages) you want to use.

The pager at the bottom

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 or the closing icon at the right corner.

Tab completion and tooltips

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 Shift-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(<Shift><TAB> and you will see the docstring for the builtin list constructor:


In [4]:
# Position your cursor after the ( and hit the Tab key:
list(


  File "<ipython-input-4-4e5b03466919>", line 2
    list(
         ^
SyntaxError: invalid syntax

The frontend/kernel model

The IPython notebook works on a client/server model where an IPython kernel starts in a separate process and executes the code you type, while the web browser acts as a client, providing a front end environment for you to type. But one kernel is capable of simultaneously talking to more than one client, and they do not all need to be of the same kind. All IPython frontends are capable of communicating with a kernel, and any number of them can be active at the same time. In addition to allowing you to have, for example, more than one browser session active, this lets you connect clients with different user interface features.

For example, you may want to connect a Qt console to your kernel and use it as a help browser, calling ?? on objects in the Qt console (whose pager is more flexible than the one in the notebook). You can start a new Qt console connected to your current kernel by using the %qtconsole magic, this will automatically detect the necessary connection information.

If you want to open one manually, or want to open a text console from a terminal, you can get your kernel's connection information with the %connect_info magic:


In [3]:
%connect_info


{
  "stdin_port": 33488, 
  "ip": "127.0.0.1", 
  "control_port": 48060, 
  "hb_port": 48697, 
  "signature_scheme": "hmac-sha256", 
  "key": "51dd0c7d-8668-42d2-8804-a13b267e1f76", 
  "shell_port": 44372, 
  "transport": "tcp", 
  "iopub_port": 40856
}

Paste the above JSON into a file, and connect with:
    $> ipython <app> --existing <file>
or, if you are local, you can connect with just:
    $> ipython <app> --existing kernel-fb8940ee-0229-409e-a2e7-af0eb9385d35.json 
or even just:
    $> ipython <app> --existing 
if this is the most recent IPython session you have started.

The kernel's raw_input and %debug

One of the major missing features in the notebook prior to version 1.0 is the lack of raw_input support. This has been remedied! Now the notebook supports the ability to send data to the kernel's standard input socket. This happens for example if you run a script that queries interactively for parameters, and very importantly, is how the interactive IPython debugger that activates when you type %debug works.

So, in order to be able to use %debug or anything else that requires raw_input, just use the notebook interface.

Display of complex objects

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.

Plotting support

As we've explained already, the notebook is just another frontend talking to the same IPython kernel that you're already familiar with, so the same options for plotting support apply.

If you start the notebook with --matplotlib, you will get matplotlib's floating, interactive windows and you can call the display function to paste figures into the notebook document. If you start it with --matplotlib inline, all plots will appear inline automatically. In this regard, the notebook works identically to the Qt console. Note that if you start the notebook server with matplotlib support, all kernels are automatically started in matplotlib mode and with the same choice of backend (i.e. floating windows or inline figures), moreover if you want to share your notebook with others you have to explicitly say to them about starting the notebook server with the matplotlib suppport. So, it is strongly recommended to start the notebook server simply by typing ipython notebook, and then selectively turn on matplotlib support only for the notebooks you want by using the %matplotlib magic (see its docstring for details).


In [5]:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline

plt.plot(np.random.rand(100));


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-5-caad0021754d> in <module>()
      1 import numpy as np
      2 import matplotlib
----> 3 import matplotlib.pyplot as plt
      4 get_ipython().magic(u'matplotlib inline')
      5 

/opt/anaconda/envs/np18py27-1.9/lib/python2.7/site-packages/matplotlib/pyplot.py in <module>()
     22 
     23 import matplotlib
---> 24 import matplotlib.colorbar
     25 from matplotlib import _pylab_helpers, interactive
     26 from matplotlib.cbook import dedent, silent_list, is_string_like, is_numlike

/opt/anaconda/envs/np18py27-1.9/lib/python2.7/site-packages/matplotlib/colorbar.py in <module>()
     27 import matplotlib.artist as martist
     28 import matplotlib.cbook as cbook
---> 29 import matplotlib.collections as collections
     30 import matplotlib.colors as colors
     31 import matplotlib.contour as contour

/opt/anaconda/envs/np18py27-1.9/lib/python2.7/site-packages/matplotlib/collections.py in <module>()
     21 import matplotlib.artist as artist
     22 from matplotlib.artist import allow_rasterization
---> 23 import matplotlib.backend_bases as backend_bases
     24 import matplotlib.path as mpath
     25 from matplotlib import _path

/opt/anaconda/envs/np18py27-1.9/lib/python2.7/site-packages/matplotlib/backend_bases.py in <module>()
     48 
     49 import matplotlib.tight_bbox as tight_bbox
---> 50 import matplotlib.textpath as textpath
     51 from matplotlib.path import Path
     52 from matplotlib.cbook import mplDeprecation

/opt/anaconda/envs/np18py27-1.9/lib/python2.7/site-packages/matplotlib/textpath.py in <module>()
     12 from matplotlib.ft2font import FT2Font, KERNING_DEFAULT, LOAD_NO_HINTING
     13 from matplotlib.ft2font import LOAD_TARGET_LIGHT
---> 14 from matplotlib.mathtext import MathTextParser
     15 import matplotlib.dviread as dviread
     16 from matplotlib.font_manager import FontProperties

/opt/anaconda/envs/np18py27-1.9/lib/python2.7/site-packages/matplotlib/mathtext.py in <module>()
     60 
     61 import matplotlib.colors as mcolors
---> 62 import matplotlib._png as _png
     63 ####################
     64 

ImportError: libpng15.so.15: cannot open shared object file: No such file or directory

Autosave and Checkpoints

As of 1.0, IPython notebooks are autosaved at a frequency of approximately two minutes. You can change this frequency with the %autosave magic. If you save manually, IPython creates a Checkpoint to which you can revert at a later time, so that autosave won't result in overwriting unwanted changes. In this way, autosave is strictly safer than the previous behavior in terms of data loss - manually saved state will never be lost due to autosave.


In [ ]:

Security

By default the notebook only listens on localhost, so it does not expose your computer to attacks coming from the internet. By default the notebook does not require any authentication, but you can configure it to ask for a password before allowing access to the files.

Furthermore, you can require the notebook to encrypt all communications by using SSL and making all connections using the https protocol instead of plain http. This is a good idea if you decide to run your notebook on addresses that are visible from the internet. For further details on how to configure this, see the security section of the manual.

Signing Notebooks

To prevent untrusted code from executing on users' behalf when notebooks open, we have added a signature to the notebook, stored in metadata. The notebook server verifies this signature when a notebook is opened. If the signature stored in the notebook metadata does not match, javascript and HTML output will not be displayed on load, and must be regenerated by re-executing the cells.

Any notebook that you have executed yourself in its entirety will be considered trusted, and its HTML and javascript output will be displayed on load.

If you need to see HTML or javascript output without re-executing, you can explicitly trust notebooks, such as those shared with you, or those that you have written yourself prior to IPython 2.0, at the command-line with:

$ ipython trust mynotebook.ipynb [other notebooks.ipynb]

This just generates a new signature stored in each notebook.


In [ ]: