Jupyter Extension DevelopmentΒΆ

This page assumes that you have already know the basic configuration for a Tornado application. If not, please read the section Web Development with Tornado and React.

To execute examples/jupyter_ext, you need to setup the Python project and install Jupyter serverextension and nbextension first.

Example setup:

$ cd example/jupyter_ext
$ pip install -e .
$ jupyter serverextension install jupyter_myext --user
$ jupyter nbextension install jupyter_myext --user

Here is the Jaffle configuration.

jaffle.hcl:

 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
kernel "py_kernel" {}

app "watchdog" {
  class  = "jaffle.app.watchdog.WatchdogApp"
  kernel = "py_kernel"

  options {
    handlers = [
      {
        patterns           = ["*.py"]
        ignore_patterns    = ["*/tests/*.py"]
        ignore_directories = true
        clear_cache        = ["jupyter_myext"]

        code_blocks = [
          "notebook.handle_watchdog_event({event})",
          "pytest.handle_watchdog_event({event})",
        ]
      },
      {
        patterns           = ["*/tests/test_*.py"]
        ignore_directories = true
        clear_cache        = ["jupyter_myext.tests"]

        code_blocks = [
          "pytest.handle_watchdog_event({event})",
        ]
      },
      {
        patterns           = ["*.js"]
        ignore_directories = true

        code_blocks = [
          "nbext_install.handle_watchdog_event({event})",
        ]
      },
    ]
  }
}

app "notebook" {
  class  = "jaffle.app.tornado.TornadoBridgeApp"
  kernel = "py_kernel"

  options {
    app_class = "notebook.notebookapp.NotebookApp"

    args = [
      "--port=9999",
      "--NotebookApp.token=''",
    ]

    clear_cache = []
  }

  start = "notebook.start()"
}

app "pytest" {
  class  = "jaffle.app.pytest.PyTestRunnerApp"
  kernel = "py_kernel"

  options {
    args = ["-s", "--color=yes"]

    auto_test = [
      "jupyter_myext/tests/test_*.py",
    ]

    auto_test_map {
      "jupyter_myext/**/*.py" = "jupyter_myext/tests/{}/test_{}.py"
    }

    clear_cache = []
  }
}

app "nbext_install" {
  class  = "jupyter_myext._devel.NBExtensionInstaller"
  kernel = "py_kernel"
}
  • L10-28: The handler configuration of pytest execution and Tornado restart, same as the example: Web Development with Tornado and React.
  • L29-36: The handler configuration to install nbextension on detecting .js file update.
  • L41-57: Launch Jupyter notebook server via TornadoBridgeApp with the main IO loop of the kernel process.
  • L78-81: The definition of an app that installs the nbextension.

Tip

This example uses NBExtensionInstaller to install the Jupyter nbextension. You can define a job that executes jupyter nbextension install --overwrite instead. If you do so, be sure to set pass_env = ["PATH"] in the kernel section if Jupyter is installed in a virtualenv.

Note

The source package of Jaffle contains example projects in examples directory. You can see the latest version of them here: https://github.com/yatsu/jaffle/tree/master/examples

A Jupyter extension example is here: https://github.com/yatsu/jaffle/tree/master/examples/jupyter_ext