Jupyter Notebook Extension Basics

There are several methods of extending the functionality of Jupyter Notebooks. Here are four of them:

  • Kernels
  • IPython kernel extensions
  • Notebook extensions
  • Notebook server extensions

For the purposes of this article, I will be focusing on the third item, Notebook Extensions. However, let's take a moment and talk about the other three so that you are aware of how they can affect your Notebook.

Kernels

The Kernel is basically the language run-time used. The default is Python via the IPython kernel. You can extend your Jupyter Notebook to use other languages besides Python. Check out the following URL for more information:

I won't be covering the installation of other kernels in this article as each kernel has different installation instructions. The URL above should be used as it has hyperlinks to the most up-to-date information on this topic.

You can also implement your own kernel if you want to. Here's a great primer on the topic:

IPython Kernel Extensions

The IPython Kernel extension is just a Python module that can be used to modify the interactive shell environment. In Jupyter's case, these extensions would modify how code cells behave. You can use this type of extension to register new magics, define variables and modify the user's namespace. You can use the following three magics for managing IPython extensions:

  • %load_ext
  • %reload_ext
  • %unload_ext

See the IPython documentation for full details on these magics.

Notebook Server Extensions

Jupyter Notebook also has the concept of "server extensions". A server extension is a Python module that loads when the Notebook's web server application starts. The current method of loading this type of extension is via Jupyter's configuration system, which we talked about in Chapter 3. You will need to specify which extensions to load in the configuration file or via the command line interface.

If you add a new extension while Jupyter Notebook is running, you will have to restart the Notebook process to activate the new extension.

Notebook Extensions

The type of extension that we care the most about in this chapter is the Notebook Extension. A Notebook extension (or nbextensions) are JavaScript modules that you can load on most of the views in your Notebook's frontend. They can access the page's DOM and the Jupyter JavaScript API which allows the extension to modify the user experience and interface. This type of extension is exclusive to the Notebook frontend.

Let's learn how you might install a Notebook Extension. The manual method of installing a Jupyter Notebook extension would look something like this, assuming you have already downloaded / pip installed the package that contains the extension:

jupyter nbextension install EXTENSION
jupyter nbextension enable EXTENSION

Note that you would replace EXTENSION with the name of the extension you are intending to install.

Another method that seems to have gained a lot of backing for managing Notebook extensions is by using the Jupyter NbExtensions Configurator. You can check out the project here.

This package is not part of the Jupyter Project, but it is quite helpful. You can use pip or conda to install the Configurator. Here's how to do it with pip:

pip install jupyter_nbextensions_configurator

If you use conda as your Python package manager, then you would install the Configurator like this:

conda install -c conda-forge jupyter_nbextensions_configurator

The Configurator is a Jupyter server extension and must be enabled. You will need to run the following command in your terminal before starting Jupyter Notebook (or you can restart the server):

jupyter nbextensions_configurator enable --user

When I ran this command, I got the following output:

Enabling: jupyter_nbextensions_configurator
- Writing config: /Users/michael/.jupyter
    - Validating...
      jupyter_nbextensions_configurator 0.4.0 OK
Enabling notebook nbextension nbextensions_configurator/config_menu/main...
Enabling tree nbextension nbextensions_configurator/tree_tab/main...

After starting up Jupyter Notebook, just click on the Nbextensions tab and you should see something like the following:

Since we don't have any extensions downloaded and installed, the Configurator looks kind of barren and can't really do all that much for us right now. Let's get some extensions to try!

If you were to search for Jupyter Notebook extensions, you would find the **jupyter_contrib_nbextensions** pretty quickly. It is a collection of Notebook extensions provided by the Jupyter community. You can read more about the extensions that are included in this package at either of the following links:

To install this set of extensions, you can once again use either pip or conda. Here is the pip command you will need:

pip install jupyter_contrib_nbextensions

And here is the conda command:

conda install -c conda-forge jupyter_contrib_nbextensions

Once you have this package downloaded and installed, you will need to use a Jupyter to install the javascript and css files to the right location so that the Notebook can access them. Here is the command you should run:

jupyter contrib nbextension install --user

Now that you have the new extensions installed, you can use the Configurator tool we installed earlier to easily enable or disable them:

As you can see in the screenshot above, there is now a LOT of extensions to play around with. The Configurator is really handy for figuring out which extensions you have installed and enabled. You can also enable and disable Notebook extensions by hand in the terminal using jupyter nbextension enable EXTENSION or jupyter nbextension disable EXTENSION respectively, but I personally find the Configurator easier to use.


Wrapping Up

In this article we learned about the different types of extensions you can use with Jupyter. The one we cared the most about were the Jupyter Notebook extensions. We then went on to learn the basics of installing extensions. We also learned how to enable and disable extensions using the terminal and also via the Jupyter NbExtensions Configurator package.


Related Reading

Copyright © 2024 Mouse Vs Python | Powered by Pythonlibrary