Jupyter Notebook is a web application used to produce computational documents, commonly called “notebooks” which can contain human-readable text, executable code, charts, and more. When used together with Python and libraries like pandas and Matplotlib, Jupyter notebooks are an excellent tool we can use for presentations and data analysis.
In this article we see how to install the Jupyter Notebook App on Linux, and we learn to write our first notebook.
In this tutorial you will learn:
- How to install and run the Jupyter Notebook app on Linux
- How to write your first notebook
- How to save and export a notebook
|Category||Requirements, Conventions or Software Version Used|
|Other||Privileged access to your Linux system as root or via the
|Conventions||# – requires given linux-commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux-commands to be executed as a regular non-privileged user
There are two distinct applications/environments we can use to work with notebooks: Jupiter Notebook and JupiterLab. The former is the simpler and more streamlined of the two: it is what we will use in our examples. The latter is newer, more flexible and comes with an interface more similar to an IDE. Both work with the same notebook document format. We can install the Jupyter Notebook application directly with pip, as a pure Python package, or by using our favorite distribution package manager.
Installing Jupyter Notebook using pip
The cross-distribution way to install Jupyter Notebook is by using pip. This method has the advantage of providing the last available version of the package, and not requiring superuser privileges to work (actually, you should absolutely avoid using pip as root!). To install the app, we run:
$ pip install --user notebook
Installing Jupyter Notebook using distributions package managers
Jupyter Notebook is also available in the official repositories of the most used Linux distributions, therefore we can also install it using their native package managers. This installation method provides the best possible software integration, however, the packaged version of the application can be quite old, depending on the distribution we are running.
To install Jupyter Notebook on Fedora we run:
$ sudo dnf install python3-notebook
On Debian and Debian-based distributions such as Ubuntu, we can use the following command, instead:
$ sudo apt install jupyter-notebook
On Archlinux the “jupyter-notebook” package is available in the “Extra” repository. We can install it with pacman:
$ sudo pacman -S jupyter-notebook
Jupyter Notebook: the basics
Jupyter Notebook supports many programming languages (Python, C++, Julia and Ruby, etc.) via dedicated core processes known as “kernels”. Python kernel is called “ipykernel”: it is based on IPython (Interactive Python), an extended Python shell which provides advanced features such as code completion and introspection. Launching the Jupyter Notebook server is a matter of running a single command:
Jupiter Notebook uses a client-server architecture. The server listens on port
8888, and no authentication is implemented by default. In response to the command above, our favorite web browser will be launched and pointed to
http://localhost:8000/tree. The interface is basically a file manager:
Creating our first notebook
To create our first notebook, all we have to do is to click on the “New notebook” entry in the right-click context menu, then choose a kernel for the notebook. Since we plan to use Python, in this case we just confirm the default choice: “ipykernel”.
A notebook can be in command or edit mode. Command mode is the default; to switch to the “edit”, mode we can click inside a cell. Cells are the “basic” blocks of a notebook. When the notebook is in “edit” mode, cell borders become blue:
A cell content can be interpreted as Markdown, Code, or raw text. Code is the default; to change it, we can use the dedicated dropdown in the toolbar, or the appropriate shortcuts:
M switches the content to “Markdown”,
R switches to “Raw”:
Executing the content of a cell
To actually execute the content of a cell, we can either use the
Ctrl-Return shortcut, or click on “Run Selected Cell” in the toolbar. In the example below, we populated the cell with some Markdown code:
The content of the cell, once executed, produces the following result:
Adding, deleting and moving cells
To add, delete or move a cell up or down in our notebook, we can use the buttons on the top right of each cell:
We can, in order: create a duplicate of the cell, move the cell up or down, insert a new cell above or below, and delete the cell.
Using pandas in Jupyter notebooks
Although we can run any kind of Python code in Jupyter notebooks, they are mainly used for data analysis and presentations. In a previous article we learned the basis of the pandas library. Here is a trivial example of using it in the notebook:
The notebook contains 3 cells. In the first one we imported required libraries, in the second we created a pandas
DataFrame object with some “Lord of The Rings” characters data; in the the third, we created a plot based on the that object to represent the age of the included characters.
To execute all the cells we click on “Run -> Run All Cells” in the main menu. The numbers in square brackets at the left of each cell indicate the execution order:
Saving and exporting a notebook
To save a notebook in its native format (“ipynb”) we click on “File -> Save Notebook” or “File -> Save Notebook As”. Alternatively, we can use the corresponding keyboard shortcuts: “Ctrl+S” and “Ctrl+Shift+S”. To convert and export a notebook in one of the available formats, instead, we can click on “File -> Save and Export Notebook As”. The following formats are available:
- ReStructured Text
- Executable Script
- Reveal.js Slides
In this tutorial we saw how to install the Jupyter Notebook App on Linux and how to create a notebook using Python and the Ipython kernel. We learned the basic concepts behind a notebook, how to create and manage cells, and how to save or export a notebook in one of the many available formats. We barely scratched the surface of Jupyter Notebook here. To know more about the application you can take a look at the official quick start guide.