Create plugin#

In addition to the existing notebook extensions, other plugins can also be added. The directory in which jupyter_contrib_nbextensions/nbextensions is located can be found with pip show:

$ pipenv run pip show jupyter_contrib_nbextensions
Name: jupyter-contrib-nbextensions
Version: 0.5.1
Summary: A collection of Jupyter nbextensions.
Home-page: https://github.com/ipython-contrib/jupyter_contrib_nbextensions.git
Author: ipython-contrib and jupyter-contrib developers
Author-email: jupytercontrib@gmail.com
License: BSD
Location: /Users/veit/.local/share/virtualenvs/jupyter-tutorial--q5BvmfG/lib/python3.7/site-packages
Requires: lxml, jupyter-contrib-core, nbconvert, jupyter-latex-envs, jupyter-core, pyyaml, jupyter-nbextensions-configurator, notebook, traitlets, jupyter-highlight-selected-word, tornado, ipython-genutils
Required-by:

This directory contains the individual notebook extensions, e.g. with the following structure:

$ tree
.
├── main.js
├── main.yaml
└── readme.md
main.js

contains the actual logic of the extension, e.g .:

define([
    'require',
    'base/js/namespace',
], function (
    requirejs
    $,
    Jupyter,
) {
    "use strict";

    // define default values for config parameters
    var params = {
        my_config_value : 100
    };

    var initialize = function () {
        $.extend(true, params, Jupyter.notebook.config.myextension);

        $('<link/>')
            .attr({
                rel: 'stylesheet',
                type: 'text/css',
                href: requirejs.toUrl('./myextension.css')
            })
            .appendTo('head');
    };

    var load_ipython_extension = function () {
        return Jupyter.notebook.config.loaded.then(initialize);
    };

    return {
        load_ipython_extension : load_ipython_extension
    };
});
main.yaml

yaml file that describes the extension for the Jupyter Extensions Configurator.

Type: Jupyter Notebook Extension
Compatibility: 3.x, 4.x, 5.x, 6.x
Name: My notebook extensions
Main: main.js
Link: README.md
Description: |
  My notebook extension helps with the use of Jupyter notebooks.
Parameters:
- none

More information about the options supported by the configurator can be found on GitHub: jupyter_nbextensions_configurator.

readme.md

Markdown file that describes the extension and how it can be used. This is also displayed in the Nbextensions tab.

Setup Jupyter Notebook Extension#

This is an extension that fixes some problems when working with notebooks that Joel Grus presented at JupyterCon 2018: I Don’t Like Notebooks:

  • it asks you to name the notebook

  • it creates a template to improve the documentation

  • it imports and configures frequently used libraries

Installation#

  1. Find out where the notebook extensions are installed:

    $ pipenv run pip show jupyter_contrib_nbextensions
    Name: jupyter-contrib-nbextensions
    Version: 0.5.1
    Summary: A collection of Jupyter nbextensions.
    Home-page: https://github.com/ipython-contrib/jupyter_contrib_nbextensions.git
    Author: ipython-contrib and jupyter-contrib developers
    Author-email: jupytercontrib@gmail.com
    License: BSD
    Location: /Users/veit/.local/share/virtualenvs/jupyter-tutorial--q5BvmfG/lib/python3.7/site-packages
    Requires: lxml, jupyter-contrib-core, nbconvert, jupyter-latex-envs, jupyter-core, pyyaml, jupyter-nbextensions-configurator, notebook, traitlets, jupyter-highlight-selected-word, tornado, ipython-genutils
    Required-by:
    
  2. Download the Setup directory in jupyter_contrib_nbextensions/nbextensions/.

  3. Install the extension with

    $ pipenv run jupyter contrib nbextensions install --user
    
    [I 10:54:46 InstallContribNbextensionsApp] Installing /Users/veit/.local/share/virtualenvs/jupyter-tutorial--q5BvmfG/lib/python3.7/site-packages/jupyter_contrib_nbextensions/nbextensions/setup -> setup
    [I 10:54:46 InstallContribNbextensionsApp] Making directory: /Users/veit/Library/Jupyter/nbextensions/setup/
    [I 10:54:46 InstallContribNbextensionsApp] Copying: /Users/veit/.local/share/virtualenvs/jupyter-tutorial--q5BvmfG/lib/python3.7/site-packages/jupyter_contrib_nbextensions/nbextensions/setup/setup.yaml -> /Users/veit/Library/Jupyter/nbextensions/setup/setup.yaml
    [I 10:54:46 InstallContribNbextensionsApp] Copying: /Users/veit/.local/share/virtualenvs/jupyter-tutorial--q5BvmfG/lib/python3.7/site-packages/jupyter_contrib_nbextensions/nbextensions/setup/README.md -> /Users/veit/Library/Jupyter/nbextensions/setup/README.md
    [I 10:54:46 InstallContribNbextensionsApp] Copying: /Users/veit/.local/share/virtualenvs/jupyter-tutorial--q5BvmfG/lib/python3.7/site-packages/jupyter_contrib_nbextensions/nbextensions/setup/main.js -> /Users/veit/Library/Jupyter/nbextensions/setup/main.js
    [I 10:54:46 InstallContribNbextensionsApp] - Validating: OK
    
    
  4. Activate the Setup extension in Nbextensions.

Finally you can create a new notebook, which then has the following structure: setup.ipynb.