-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Learning IPython for Interactive Computing and Data Visualization, Second Edition
By :

Originally, IPython provided an enhanced command-line console to run Python code interactively. The Jupyter Notebook is a more recent and more sophisticated alternative to the console. Today, both tools are available, and we recommend that you learn to use both.
To run the IPython console, type ipython
in an OS terminal. There, you can write Python commands and see the results instantly. Here is a screenshot:
IPython console
The IPython console is most convenient when you have a command-line-based workflow and you want to execute some quick Python commands.
You can exit the IPython console by typing exit
.
Let's mention the Qt console, which is similar to the IPython console but offers additional features such as multiline editing, enhanced tab completion, image support, and so on. The Qt console can also be integrated within a graphical application written with Python and Qt. See http://jupyter.org/qtconsole/stable/ for more information.
To run the Jupyter Notebook, open an OS terminal, go to ~/minibook/
(or into the directory where you've downloaded the book's notebooks), and type jupyter notebook
. This will start the Jupyter server and open a new window in your browser (if that's not the case, go to the following URL: http://localhost:8888
). Here is a screenshot of Jupyter's entry point, the Notebook dashboard:
The Notebook dashboard
At the time of writing, the following browsers are officially supported: Chrome 13 and greater; Safari 5 and greater; and Firefox 6 or greater. Other browsers may work also. Your mileage may vary.
The Notebook is most convenient when you start a complex analysis project that will involve a substantial amount of interactive experimentation with your code. Other common use-cases include keeping track of your interactive session (like a lab notebook), or writing technical documents that involve code, equations, and figures.
In the rest of this section, we will focus on the Notebook interface.
Closing the Notebook server
To close the Notebook server, go to the OS terminal where you launched the server from, and press Ctrl + C. You may need to confirm with y
.
The dashboard contains several tabs:
A notebook is an interactive document containing code, text, and other elements. A notebook is saved in a file with the .ipynb
extension. This file is a plain text file storing a JSON data structure.
A kernel is a process running an interactive session. When using IPython, this kernel is a Python process. There are kernels in many languages other than Python.
We follow the convention to use the term notebook for a file, and Notebook for the application and the web interface.
In Jupyter, notebooks and kernels are strongly separated. A notebook is a file, whereas a kernel is a process. The kernel receives snippets of code from the Notebook interface, executes them, and sends the outputs and possible errors back to the Notebook interface. Thus, in general, the kernel has no notion of a Notebook. A notebook is persistent (it's a file), whereas a kernel may be closed at the end of an interactive session and it is therefore not persistent. When a notebook is re-opened, it needs to be re-executed.
In general, no more than one Notebook interface can be connected to a given kernel. However, several IPython consoles can be connected to a given kernel.
To create a new notebook, click on the New button, and select Notebook (Python 3). A new browser tab opens and shows the Notebook interface as follows:
A new notebook
Here are the main components of the interface, from top to bottom:
.ipynb
file.There are two main types of cells: Markdown cells and code cells, and they are described as follows:
You can change the type of a cell by first clicking on a cell to select it, and then choosing the cell's type in the toolbar's dropdown menu showing Markdown or Code.
Here is a screenshot of a Markdown cell:
A Markdown cell
The top panel shows the cell in edit mode, while the bottom one shows it in render mode. The edit mode lets you edit the text, while the render mode lets you display the rendered cell. We will explain the differences between these modes in greater detail in the following section.
Here is a screenshot of a complex code cell:
Structure of a code cell
This code cell contains several parts, as follows:
The Notebook implements a modal interface similar to some text editors such as vim. Mastering this interface may represent a small learning curve for some users.
Keyboard shortcuts are available in the Notebook interface. Type h
to show them. We review here the most common ones (for Windows and Linux; shortcuts for OS X may be slightly different).
Here are a few keyboard shortcuts that are always available when a cell is selected:
In the edit mode, you can type code as usual, and you have access to the following keyboard shortcuts:
In the command mode, keystrokes are bound to cell operations. Don't write code in command mode or unexpected things will happen! For example, typing dd
in command mode will delete the selected cell! Here are some keyboard shortcuts available in command mode:
Spending some time learning these shortcuts is highly recommended.
Here are a few references:
Change the font size
Change margin width
Change background colour