Chapter 6. Customizing IPython
The Jupyter Notebook is a highly-customizable platform. You can configure many aspects of the software in your configuration files. You can also extend the backend (kernels) and the frontend (HTML-based Notebook). This allows you to create highly-personalized user experiences based on the Notebook.
In this chapter, we will cover the following topics:
Creating a custom magic command in an IPython extension
Writing a new Jupyter kernel
Displaying rich HTML elements in the Notebook
Customizing the Notebook interface with JavaScript
Creating a custom magic command in an IPython extension
IPython comes with a rich set of magic commands. You can get the complete list with the %lsmagic
command. IPython also allows you to create your own magic commands. In this section, we will create a new cell magic that compiles and executes C++ code in the Notebook.
We first import the register_cell_magic
function:
To create a new cell magic, we create a function that takes a line (containing possible options) and a cell's contents as its arguments, and we decorate it with @register_cell_magic
, as shown here:
Writing a new Jupyter kernel
Jupyter supports a wide variety of kernels written in many languages, including the most-frequently used IPython. The Notebook interface lets you choose the kernel for every notebook. This information is stored within each notebook file.
The jupyter kernelspec
command allows you to get information about the kernels. For example, jupyter kernelspec list
lists the installed kernels. Type jupyter kernelspec --help
for more information.
At the end of this section, you will find references with instructions to install various kernels such as IR, IJulia, or IHaskell. Here, we will detail how to create a custom kernel.
There are two methods to create a new kernel:
We will use the second, easier method in this section. Specifically, we will reuse the example from the last section to write a C++ wrapper...
Displaying rich HTML elements in the Notebook
The Jupyter Notebook application is based on HTML and runs in a web browser. This platform supports many kinds of rich content such as images, mathematical equations, interactive widgets, videos, and much more. Jupyter proposes several methods to leverage these capabilities.
In this section, we'll show how to display HTML, SVG, and JavaScript elements, notably with the Data-Driven Documents (D3) JavaScript visualization library.
Displaying SVG in the Notebook
Scalable
Vector Graphics (SVG) is an open XML-based file format describing vector graphics. Most modern web browsers support this format.
For displaying objects, IPython provides a simple API for representing rich content like SVG. In the following example, we'll define a Disc
class with a customizable radius and a color. When displaying a Disc
instance in the Notebook, an SVG representation of the disc will be shown.
Let's first define a function generating the SVG code for a disc:
Customizing the Notebook interface with JavaScript
The Notebook application exposes a JavaScript API that allows for a high level of customization. In this section, we will create a new button in the Notebook toolbar to renumber the cells.
Note
The JavaScript API is not stable and not well-documented. Although the example in this section has been tested with IPython 4.0, nothing guarantees that it will work in future versions without changes.
The commented JavaScript code belows adds a new Renumber button.
In this chapter, we covered several customization options of IPython and the Jupyter Notebook. The IPython Cookbook contains more details, notably on how to create entirely custom widgets in the Notebook.
With this book, you've learned the fundamentals of the platform: Python, IPython, and the Jupyter Notebook. You've seen how to analyze real-world datasets with pandas and NumPy, and how to create plots with matplotlib and seaborn. Finally, you've sampled a wide-range of the scientific Python ecosystem, including high-performance computing, interactive visualization, and interactive data analysis.
The IPython Cookbook, Packt Publishing, is the sequel of this book. In more than 500 pages and 100 recipes, it explores the topics addressed in this book in much greater detail. Also, it contains a wide range of examples illustrating advanced analyses in applied mathematics, statistics, machine learning, signal processing, networks, and many other domains.