Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning IPython for Interactive Computing and Data Visualization, Second Edition

You're reading from  Learning IPython for Interactive Computing and Data Visualization, Second Edition

Product type Book
Published in Oct 2015
Publisher
ISBN-13 9781783986989
Pages 200 pages
Edition 1st Edition
Languages
Author (1):
Cyrille Rossant Cyrille Rossant
Profile icon Cyrille Rossant

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:

In [1]: from IPython.core.magic import register_cell_magic

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:

In [2]: @register_cell_magic
        def cpp(line, cell):
            """Compile, execute C++ code, and return the
            standard output."""
            # We first retrieve the current IPython interpreter
            # instance.
            ip = get_ipython()

            # We define the source and executable filenames.
            source_filename = '_temp.cpp'
  ...

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:

  • Writing a kernel from scratch for a new language by reimplementing the whole Jupyter messaging protocol.

  • Writing a wrapper kernel for a language that can be accessed from Python.

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:

In [1]...

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 [1]: %%javascript

        // This function allows us to add buttons
        // to the Notebook toolbar.
        IPython.toolbar.add_buttons_group([
        {

            // The button's label.
            'label': 'Renumber all code cells',

            // The button's icon.
            // See a list of Font-Awesome icons here:
            // http://fortawesome.github.io/Font-Awesome/icons/
            'icon': 'fa-list-ol',

            // The callback function called when the button...

Summary


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.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Learning IPython for Interactive Computing and Data Visualization, Second Edition
Published in: Oct 2015 Publisher: ISBN-13: 9781783986989
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}