File: Distributing%20Jupyter%20Extensions%20as%20Python%20Packages.rst

package info (click to toggle)
jupyter-notebook 4.2.3-4~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 7,804 kB
  • sloc: python: 8,698; makefile: 240; sh: 74
file content (245 lines) | stat: -rw-r--r-- 7,898 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245

`View the original notebook on nbviewer <http://nbviewer.jupyter.org/github/jupyter/notebook/blob/master/docs/source/examples/Notebook/Distributing%20Jupyter%20Extensions%20as%20Python%20Packages.ipynb>`__

Distributing Jupyter Extensions as Python Packages
==================================================

Overview
--------

How can the notebook be extended?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The Jupyter Notebook client and server application are both deeply
customizable. Their behavior can be extended by creating, respectively:

-  nbextension: a notebook extension

   -  a single JS file, or directory of JavaScript, Cascading
      StyleSheets, etc. that contain at minimum a JavaScript module
      packaged as an `AMD
      modules <https://en.wikipedia.org/wiki/Asynchronous_module_definition>`__
      that exports a function ``load_ipython_extension``

-  server extension: an importable Python module

   -  that implements ``load_jupyter_server_extension``

Why create a Python package for Jupyter extensions?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Since it is rare to have a server extension that does not have any
frontend components (an nbextension), for convenience and consistency,
all these client and server extensions with their assets can be packaged
and versioned together as a Python package with a few simple commands.
This makes installing the package of extensions easier and less
error-prone for the user.

Installation of Jupyter Extensions
----------------------------------

Install a Python package containing Jupyter Extensions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

There are several ways that you may get a Python package containing
Jupyter Extensions. Commonly, you will use a package manager for your
system:

.. code:: shell

    pip install helpful_package
    # or
    conda install helpful_package
    # or
    apt-get install helpful_package

    # where 'helpful_package' is a Python package containing one or more Jupyter Extensions

Enable a Server Extension
~~~~~~~~~~~~~~~~~~~~~~~~~

The simplest case would be to enable a server extension which has no
frontend components.

A ``pip`` user that wants their configuration stored in their home
directory would type the following command:

.. code:: shell

    jupyter serverextension enable --py helpful_package

Alternatively, a ``virtualenv`` or ``conda`` user can pass
``--sys-prefix`` which keeps their environment isolated and
reproducible. For example:

.. code:: shell

    # Make sure that your virtualenv or conda environment is activated
    [source] activate my-environment

    jupyter serverextension enable --py helpful_package --sys-prefix

Install the nbextension assets
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If a package also has an nbextension with frontend assets that must be
available (but not neccessarily enabled by default), install these
assets with the following command:

.. code:: shell

    jupyter nbextension install --py helpful_package # or --sys-prefix if using virtualenv or conda

Enable nbextension assets
~~~~~~~~~~~~~~~~~~~~~~~~~

If a package has assets that should be loaded every time a Jupyter app
(e.g. lab, notebook, dashboard, terminal) is loaded in the browser, the
following command can be used to enable the nbextension:

.. code:: shell

    jupyter nbextension enable --py helpful_package # or --sys-prefix if using virtualenv or conda

Did it work? Check by listing Jupyter Extensions.
-------------------------------------------------

After running one or more extension installation steps, you can list
what is presently known about nbextensions or server extension. The
following commands will list which extensions are available, whether
they are enabled, and other extension details:

.. code:: shell

    jupyter nbextension list
    jupyter serverextension list

Additional resources on creating and distributing packages
----------------------------------------------------------

    Of course, in addition to the files listed, there are number of
    other files one needs to build a proper package. Here are some good
    resources: - `The Hitchhiker's Guide to
    Packaging <http://the-hitchhikers-guide-to-packaging.readthedocs.org/en/latest/quickstart.html>`__
    - `Repository Structure and
    Python <http://www.kennethreitz.org/essays/repository-structure-and-python>`__
    by Kenneth Reitz

    How you distribute them, too, is important: - `Packaging and
    Distributing
    Projects <http://python-packaging-user-guide.readthedocs.org/en/latest/distributing/>`__
    - `conda: Building
    packages <http://conda.pydata.org/docs/building/build.html>`__

    Here are some tools to get you started: -
    `generator-nbextension <https://github.com/Anaconda-Server/generator-nbextension>`__

Example - Server extension
--------------------------

Creating a Python package with a server extension
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here is an example of a python module which contains a server extension
directly on itself. It has this directory structure:

::

    - setup.py
    - MANIFEST.in
    - my_module/
      - __init__.py

Defining the server extension
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This example shows that the server extension and its
``load_jupyter_server_extension`` function are defined in the
``__init__.py`` file.

``my_module/__init__.py``
^^^^^^^^^^^^^^^^^^^^^^^^^

.. code:: python

    def _jupyter_server_extension_paths():
        return [{
            "module": "my_module"
        }]


    def load_jupyter_server_extension(nbapp):
        nbapp.log.info("my module enabled!")

Install and enable the server extension
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Which a user can install with:

::

    jupyter serverextension enable --py my_module [--sys-prefix]

Example - Server extension and nbextension
------------------------------------------

Creating a Python package with a server extension and nbextension
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here is another server extension, with a front-end module. It assumes
this directory structure:

::

    - setup.py
    - MANIFEST.in
    - my_fancy_module/
      - __init__.py
      - static/
        index.js

Defining the server extension and nbextension
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This example again shows that the server extension and its
``load_jupyter_server_extension`` function are defined in the
``__init__.py`` file. This time, there is also a function
``_jupyter_nbextension_path`` for the nbextension.

``my_fancy_module/__init__.py``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code:: python

    def _jupyter_server_extension_paths():
        return [{
            "module": "my_fancy_module"
        }]

    # Jupyter Extension points
    def _jupyter_nbextension_paths():
        return [dict(
            section="notebook",
            # the path is relative to the `my_fancy_module` directory
            src="static",
            # directory in the `nbextension/` namespace
            dest="my_fancy_module",
            # _also_ in the `nbextension/` namespace
            require="my_fancy_module/index")]

    def load_jupyter_server_extension(nbapp):
        nbapp.log.info("my module enabled!")

Install and enable the server extension and nbextension
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The user can install and enable the extensions with the following set of
commands:

::

    jupyter nbextension install --py my_fancy_module [--sys-prefix|--user]
    jupyter nbextension enable --py my_fancy_module [--sys-prefix|--system]
    jupyter serverextension enable --py my_fancy_module [--sys-prefix|--system]

`View the original notebook on nbviewer <http://nbviewer.jupyter.org/github/jupyter/notebook/blob/master/docs/source/examples/Notebook/Distributing%20Jupyter%20Extensions%20as%20Python%20Packages.ipynb>`__