This is a sample section from Learning IPython for Interactive Computing and Data Visualization, second edition.
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:
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
INFO (The Qt console): 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:
INFO: At the time of writing, the following browsers are officially supported: Chrome 13 and greater; Safari 5 and greater; 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.
TIP (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.
INFO (Notebook and notebook): We follow the convention to use the term of 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 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 console 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:
Here are the main components of the interface, from top to bottom:
Codelets you change the type of a cell.
There are two main types of cells: Markdown cells and code cells.
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
Here is a screenshot of 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 below.
Here is a screenshot of a complex code cell:
This code cell contains several parts:
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 Mac OS X may be slightly different).
Here are a few keyboard shortcuts that are always available when a cell is selected:
Enter: run the cell
Enter: run the cell and select the cell below
Enter: run the cell and insert a new cell below
s: save the notebook
In edit mode, you can type code as usual, and you have access to the following keyboard shortcuts:
Esc: switch to command mode
-: split the cell
In 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:
Enter: switch to edit mode
k: select the previous cell
j: select the next cell
m: change the cell type to code cell / Markdown cell
b: insert a new cell above / below the current cell
v: cut / copy / paste the current cell
dd: delete the current cell
z: undo the last delete operation
=: merge the cell below
h: display the help menu with the list of keyboard shorcuts
Spending some time learning these shortcuts is highly recommended.
Here are a few references: