File: extensions.rst

package info (click to toggle)
jupyter-server 2.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,068 kB
  • sloc: python: 21,064; makefile: 186; sh: 25; javascript: 14
file content (581 lines) | stat: -rw-r--r-- 20,622 bytes parent folder | download
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
.. _extensions:

=================
Server Extensions
=================

A Jupyter Server extension is typically a module or package that extends to Server’s REST API/endpoints—i.e. adds extra request handlers to Server’s Tornado Web Application.

For examples of jupyter server extensions, see the
:ref:`homepage <examples of jupyter server extensions>`.

To get started writing your own extension, see the simple examples in the `examples folder
<https://github.com/jupyter-server/jupyter_server/tree/main/examples/simple>`_ in the GitHub jupyter_server repository.


Authoring a basic server extension
==================================

The simplest way to write a Jupyter Server extension is to write an extension
module with a ``_load_jupyter_server_extension`` function. This function should
take a single argument, an instance of the ``ServerApp``.


.. code-block:: python

    def _load_jupyter_server_extension(serverapp: jupyter_server.serverapp.ServerApp):
        """
        This function is called when the extension is loaded.
        """
        pass


Adding extension endpoints
--------------------------

The easiest way to add endpoints and handle incoming requests is to subclass the ``JupyterHandler`` (which itself is a subclass of Tornado's ``RequestHandler``).

.. code-block:: python

    from jupyter_server.base.handlers import JupyterHandler
    import tornado


    class MyExtensionHandler(JupyterHandler):
        @tornado.web.authenticated
        def get(self):
            ...

        @tornado.web.authenticated
        def post(self):
            ...

.. note::
   It is best practice to wrap each handler method with the ``authenticated`` decorator to ensure that each request is authenticated by the server.

Then add this handler to Jupyter Server's Web Application through the ``_load_jupyter_server_extension`` function.

.. code-block:: python

    def _load_jupyter_server_extension(serverapp: jupyter_server.serverapp.ServerApp):
        """
        This function is called when the extension is loaded.
        """
        handlers = [("/myextension/hello", MyExtensionHandler)]
        serverapp.web_app.add_handlers(".*$", handlers)


Making an extension discoverable
--------------------------------

To make this extension discoverable to Jupyter Server, first define a
``_jupyter_server_extension_points()`` function at the root of the module/
package. This function returns metadata describing how to load the extension.
Usually, this requires a ``module`` key with the import path to the extension's
``_load_jupyter_server_extension`` function.

.. code-block:: python

    def _jupyter_server_extension_points():
        """
        Returns a list of dictionaries with metadata describing
        where to find the `_load_jupyter_server_extension` function.
        """
        return [{"module": "my_extension"}]

Second, add the extension to the ServerApp's ``jpserver_extensions`` trait. This can be manually added by users in their ``jupyter_server_config.py`` file,

.. code-block:: python

    c.ServerApp.jpserver_extensions = {"my_extension": True}

or loaded from a JSON file in the ``jupyter_server_config.d`` directory under
one of `Jupyter's paths`_. (See the `Distributing a server extension`_ section
for details on how to automatically enabled your extension when users install
it.)

.. code-block:: python

    {"ServerApp": {"jpserver_extensions": {"my_extension": true}}}


Authoring a configurable extension application
==============================================

Some extensions are full-fledged client applications that sit on top of the Jupyter Server. For example, `JupyterLab <https://jupyterlab.readthedocs.io/en/stable/>`_ is a server extension. It can be launched from the command line, configured by CLI or config files, and serves+loads static assets behind the server (i.e. html templates, Javascript, etc.)

Jupyter Server offers a convenient base class, ``ExtensionsApp``, that handles most of the boilerplate code for building such extensions.

Anatomy of an ``ExtensionApp``
------------------------------

An ExtensionApp:

    - has traits.
    - is configurable (from file or CLI)
    - has a name (see the ``name`` trait).
    - has an entrypoint, ``jupyter <name>``.
    - can serve static content from the ``/static/<name>/`` endpoint.
    - can add new endpoints to the Jupyter Server.

The basic structure of an ExtensionApp is shown below:

.. code-block:: python

    from jupyter_server.extension.application import ExtensionApp


    class MyExtensionApp(ExtensionApp):
        # -------------- Required traits --------------
        name = "myextension"
        default_url = "/myextension"
        load_other_extensions = True
        file_url_prefix = "/render"

        # --- ExtensionApp traits you can configure ---
        static_paths = [...]
        template_paths = [...]
        settings = {...}
        handlers = [...]

        # ----------- add custom traits below ---------
        ...

        def initialize_settings(self):
            ...
            # Update the self.settings trait to pass extra
            # settings to the underlying Tornado Web Application.
            self.settings.update({"<trait>": ...})

        def initialize_handlers(self):
            ...
            # Extend the self.handlers trait
            self.handlers.extend(...)

        def initialize_templates(self):
            ...
            # Change the jinja templating environment

        async def stop_extension(self):
            ...
            # Perform any required shut down steps


The ``ExtensionApp`` uses the following methods and properties to connect your
extension to the Jupyter server. You do not need to define a
``_load_jupyter_server_extension`` function for these apps. Instead, overwrite
the pieces below to add your custom settings, handlers and templates:

Methods

* ``initialize_settings()``: adds custom settings to the Tornado Web Application.
* ``initialize_handlers()``: appends handlers to the Tornado Web Application.
* ``initialize_templates()``: initialize the templating engine (e.g. jinja2) for your frontend.
* ``stop_extension()``: called on server shut down.

Properties

* ``name``: the name of the extension
* ``default_url``: the default URL for this extension—i.e. the landing page for this extension when launched from the CLI.
* ``load_other_extensions``: a boolean enabling/disabling other extensions when launching this extension directly.
* ``file_url_prefix``: the prefix URL added when opening a document directly from the command line. For example, classic Notebook uses ``/notebooks`` to open a document at http://localhost:8888/notebooks/path/to/notebook.ipynb.

``ExtensionApp`` request handlers
---------------------------------

``ExtensionApp`` Request Handlers have a few extra properties.

* ``config``: the ExtensionApp's config object.
* ``server_config``: the ServerApp's config object.
* ``name``: the name of the extension to which this handler is linked.
* ``static_url()``: a method that returns the url to static files (prefixed with ``/static/<name>``).

Jupyter Server provides a convenient mixin class for adding these properties to any ``JupyterHandler``. For example, the basic server extension handler in the section above becomes:

.. code-block:: python

    from jupyter_server.base.handlers import JupyterHandler
    from jupyter_server.extension.handler import ExtensionHandlerMixin
    import tornado


    class MyExtensionHandler(ExtensionHandlerMixin, JupyterHandler):
        @tornado.web.authenticated
        def get(self):
            ...

        @tornado.web.authenticated
        def post(self):
            ...


Jinja templating from frontend extensions
-----------------------------------------

Many Jupyter frontend applications use Jinja for basic HTML templating. Since this is common enough, Jupyter Server provides some extra mixin that integrate Jinja with Jupyter server extensions.

Use ``ExtensionAppJinjaMixin`` to automatically add a Jinja templating
environment to an ``ExtensionApp``. This adds a ``<name>_jinja2_env`` setting
to Tornado Web Server's settings that will be used by request handlers.

.. code-block:: python


    from jupyter_server.extension.application import ExtensionApp, ExtensionAppJinjaMixin


    class MyExtensionApp(ExtensionAppJinjaMixin, ExtensionApp):
        ...


Pair the example above with ``ExtensionHandlers`` that also inherit the
``ExtensionHandlerJinjaMixin`` mixin. This will automatically load HTML
templates from the Jinja templating environment created by the ``ExtensionApp``.


.. code-block:: python


    from jupyter_server.base.handlers import JupyterHandler
    from jupyter_server.extension.handler import (
        ExtensionHandlerMixin,
        ExtensionHandlerJinjaMixin,
    )
    import tornado


    class MyExtensionHandler(
        ExtensionHandlerMixin, ExtensionHandlerJinjaMixin, JupyterHandler
    ):
        @tornado.web.authenticated
        def get(self):
            ...

        @tornado.web.authenticated
        def post(self):
            ...


.. note:: The mixin classes in this example must come before the base classes, ``ExtensionApp`` and ``ExtensionHandler``.


Making an ``ExtensionApp`` discoverable
---------------------------------------

To make an ``ExtensionApp`` discoverable by Jupyter Server, add the ``app`` key+value pair to the ``_jupyter_server_extension_points()`` function example above:

.. code-block:: python

    from myextension import MyExtensionApp


    def _jupyter_server_extension_points():
        """
        Returns a list of dictionaries with metadata describing
        where to find the `_load_jupyter_server_extension` function.
        """
        return [{"module": "myextension", "app": MyExtensionApp}]


Launching an ``ExtensionApp``
-----------------------------

To launch the application, simply call the ``ExtensionApp``'s ``launch_instance`` method.

.. code-block:: python

    launch_instance = MyFrontend.launch_instance
    launch_instance()


To make your extension executable from anywhere on your system, point an entry-point at the ``launch_instance`` method in the extension's ``setup.py``:

.. code-block:: python

    from setuptools import setup


    setup(
        name="myfrontend",
        # ...
        entry_points={
            "console_scripts": ["jupyter-myextension = myextension:launch_instance"]
        },
    )

``ExtensionApp`` as a classic Notebook server extension
-------------------------------------------------------

An extension that extends ``ExtensionApp`` should still work with the old Tornado server from the classic Jupyter Notebook. The ``ExtensionApp`` class
provides a method, ``load_classic_server_extension``, that handles the extension initialization. Simply  define a ``load_jupyter_server_extension`` reference
pointing at the ``load_classic_server_extension`` method:

.. code-block:: python

    # This is typically defined in the root `__init__.py`
    # file of the extension package.
    load_jupyter_server_extension = MyExtensionApp.load_classic_server_extension


If the extension is enabled, the extension will be loaded when the server starts.


Distributing a server extension
===============================

Putting it all together, authors can distribute their extension following this steps:

1. Add a ``_jupyter_server_extension_points()`` function at the extension's root.
    This function should likely live in the ``__init__.py`` found at the root of the extension package. It will look something like this:

    .. code-block:: python

        # Found in the __init__.py of package


        def _jupyter_server_extension_points():
            return [{"module": "myextension.app", "app": MyExtensionApp}]

2. Create an extension by writing a ``_load_jupyter_server_extension()`` function or subclassing ``ExtensionApp``.
    This is where the extension logic will live (i.e. custom extension handlers, config, etc). See the sections above for more information on how to create an extension.

3. Add the following JSON config file to the extension package.
    The file should be named after the extension (e.g. ``myextension.json``)
    and saved in a subdirectory of the package with the prefix:
    ``jupyter-config/jupyter_server_config.d/``. The extension package will
    have a similar structure to this example:

    .. code-block::

        myextension
        ├── myextension/
        │   ├── __init__.py
        │   └── app.py
        ├── jupyter-config/
        │   └── jupyter_server_config.d/
        │       └── myextension.json
        └── setup.py

    The contents of the JSON file will tell Jupyter Server to load the extension when a user installs the package:

    .. code-block:: json

        {
            "ServerApp": {
                "jpserver_extensions": {
                    "myextension": true
                }
            }
        }

    When the extension is installed, this JSON file will be copied to the ``jupyter_server_config.d`` directory found in one of `Jupyter's paths`_.

    Users can toggle the enabling/disableing of extension using the command:

    .. code-block:: console

        jupyter server extension disable myextension

    which will change the boolean value in the JSON file above.

4. Create a ``setup.py`` that automatically enables the extension.
    Add a few extra lines the extension package's ``setup`` function

    .. code-block:: python

        from setuptools import setup

        setup(
            name="myextension",
            # ...
            include_package_data=True,
            data_files=[
                (
                    "etc/jupyter/jupyter_server_config.d",
                    ["jupyter-config/jupyter_server_config.d/myextension.json"],
                ),
            ],
        )




.. links

.. _`Jupyter's paths`: https://jupyter.readthedocs.io/en/latest/use/jupyter-directories.html


Migrating an extension to use Jupyter Server
============================================

If you're a developer of a `classic Notebook Server`_ extension, your extension
should be able to work with *both* the classic notebook server and
``jupyter_server``.

There are a few key steps to make this happen:

1. Point Jupyter Server to the ``load_jupyter_server_extension`` function with a new reference name.
    The ``load_jupyter_server_extension`` function was the key to loading a
    server extension in the classic Notebook Server. Jupyter Server expects the
    name of this function to be prefixed with an underscore—i.e.
    ``_load_jupyter_server_extension``. You can easily achieve this by adding a
    reference to the old function name with the new name in the same module.

    .. code-block:: python

        def load_jupyter_server_extension(nb_server_app):
            ...


        # Reference the old function name with the new function name.

        _load_jupyter_server_extension = load_jupyter_server_extension

2. Add new data files to your extension package that enable it with Jupyter Server.
    This new file can go next to your classic notebook server data files. Create a new sub-directory, ``jupyter_server_config.d``, and add a new ``.json`` file there:

    .. raw:: html

        <pre>
        myextension
        ├── myextension/
        │   ├── __init__.py
        │   └── app.py
        ├── jupyter-config/
        │   └── jupyter_notebook_config.d/
        │       └── myextension.json
        │   <b>└── jupyter_server_config.d/</b>
        │       <b>└── myextension.json</b>
        └── setup.py
        </pre>

    The new ``.json`` file should look something like this (you'll notice the changes in the configured class and trait names):

    .. code-block:: json

        {
            "ServerApp": {
                "jpserver_extensions": {
                    "myextension": true
                }
            }
        }

    Update your extension package's ``setup.py`` so that the data-files are moved into the jupyter configuration directories when users download the package.

    .. code-block:: python

        from setuptools import setup

        setup(
            name="myextension",
            # ...
            include_package_data=True,
            data_files=[
                (
                    "etc/jupyter/jupyter_server_config.d",
                    ["jupyter-config/jupyter_server_config.d/myextension.json"],
                ),
                (
                    "etc/jupyter/jupyter_notebook_config.d",
                    ["jupyter-config/jupyter_notebook_config.d/myextension.json"],
                ),
            ],
        )

3. (Optional) Point extension at the new favicon location.
    The favicons in the Jupyter Notebook have been moved to a new location in
    Jupyter Server. If your extension is using one of these icons, you'll want
    to add a set of redirect handlers this. (In ``ExtensionApp``, this is
    handled automatically).

    This usually means adding a chunk to your ``load_jupyter_server_extension`` function similar to this:

    .. code-block:: python

        def load_jupyter_server_extension(nb_server_app):
            web_app = nb_server_app.web_app
            host_pattern = ".*$"
            base_url = web_app.settings["base_url"]

            # Add custom extensions handler.
            custom_handlers = [
                # ...
            ]

            # Favicon redirects.
            favicon_redirects = [
                (
                    url_path_join(base_url, "/static/favicons/favicon.ico"),
                    RedirectHandler,
                    {
                        "url": url_path_join(
                            serverapp.base_url, "static/base/images/favicon.ico"
                        )
                    },
                ),
                (
                    url_path_join(base_url, "/static/favicons/favicon-busy-1.ico"),
                    RedirectHandler,
                    {
                        "url": url_path_join(
                            serverapp.base_url, "static/base/images/favicon-busy-1.ico"
                        )
                    },
                ),
                (
                    url_path_join(base_url, "/static/favicons/favicon-busy-2.ico"),
                    RedirectHandler,
                    {
                        "url": url_path_join(
                            serverapp.base_url, "static/base/images/favicon-busy-2.ico"
                        )
                    },
                ),
                (
                    url_path_join(base_url, "/static/favicons/favicon-busy-3.ico"),
                    RedirectHandler,
                    {
                        "url": url_path_join(
                            serverapp.base_url, "static/base/images/favicon-busy-3.ico"
                        )
                    },
                ),
                (
                    url_path_join(base_url, "/static/favicons/favicon-file.ico"),
                    RedirectHandler,
                    {
                        "url": url_path_join(
                            serverapp.base_url, "static/base/images/favicon-file.ico"
                        )
                    },
                ),
                (
                    url_path_join(base_url, "/static/favicons/favicon-notebook.ico"),
                    RedirectHandler,
                    {
                        "url": url_path_join(
                            serverapp.base_url, "static/base/images/favicon-notebook.ico"
                        )
                    },
                ),
                (
                    url_path_join(base_url, "/static/favicons/favicon-terminal.ico"),
                    RedirectHandler,
                    {
                        "url": url_path_join(
                            serverapp.base_url, "static/base/images/favicon-terminal.ico"
                        )
                    },
                ),
                (
                    url_path_join(base_url, "/static/logo/logo.png"),
                    RedirectHandler,
                    {"url": url_path_join(serverapp.base_url, "static/base/images/logo.png")},
                ),
            ]

            web_app.add_handlers(host_pattern, custom_handlers + favicon_redirects)


.. _`classic Notebook Server`: https://jupyter-notebook.readthedocs.io/en/v6.5.4/extending/handlers.html