Debian Codegraph @

docs/kernels.rst

raw
1 .. _kernels:
2
3 ==========================
4 Making kernels for Jupyter
5 ==========================
6
7 A 'kernel' is a program that runs and introspects the user's code. IPython
8 includes a kernel for Python code, and people have written kernels for
9 `several other languages <https://github.com/jupyter/jupyter/wiki/Jupyter-kernels>`_.
10
11 At kernel startup, Jupyter passes the kernel a connection file. This specifies
12 how to set up communications with the frontend.
13
14 There are three options for writing a kernel:
15
16 1. You can reuse the IPython kernel machinery to handle the communications, and
17 just describe how to execute your code. This is much simpler if the target
18 language can be driven from Python. See :doc:`wrapperkernels` for details.
19 2. You can implement the kernel machinery in your target language. This is more
20 work initially, but the people using your kernel might be more likely to
21 contribute to it if it's in the language they know.
22 3. You can use the `xeus <https://github.com/jupyter-xeus/xeus>`_ library that is
23 a C++ implementation of the Jupyter kernel protocol. Kernel authors only need to
24 implement the language-specific logic in their implementation
25 (execute code, auto-completion...). This is the simplest
26 solution if your target language can be driven from C or C++: e.g. if it has
27 a C-API like most scripting languages. Check out the
28 `xeus documentation <https://xeus.readthedocs.io/>`_ for more details.
29 Examples of kernels based on xeus include:
30
31 - `xeus-cling <https://github.com/jupyter-xeus/xeus-cling>`_
32 - `xeus-python <https://github.com/jupyter-xeus/xeus-python>`_
33 - `JuniperKernel <https://github.com/JuniperKernel/JuniperKernel>`_
34
35 Connection files
36 ================
37
38 Your kernel will be given the path to a connection file when it starts (see
39 :ref:`kernelspecs` for how to specify the command line arguments for your kernel).
40 This file, which is accessible only to the current user, will contain a JSON
41 dictionary looking something like this::
42
43 {
44 "control_port": 50160,
45 "shell_port": 57503,
46 "transport": "tcp",
47 "signature_scheme": "hmac-sha256",
48 "stdin_port": 52597,
49 "hb_port": 42540,
50 "ip": "127.0.0.1",
51 "iopub_port": 40885,
52 "key": "a0436f6c-1916-498b-8eb9-e81ab9368e84"
53 }
54
55 The ``transport``, ``ip`` and five ``_port`` fields specify five ports which the
56 kernel should bind to using `ZeroMQ <http://zeromq.org/>`_. For instance, the
57 address of the shell socket in the example above would be::
58
59 tcp://127.0.0.1:57503
60
61 New ports are chosen at random for each kernel started.
62
63 ``signature_scheme`` and ``key`` are used to cryptographically sign messages, so
64 that other users on the system can't send code to run in this kernel. See
65 :ref:`wire_protocol` for the details of how this signature is calculated.
66
67 Handling messages
68 =================
69
70 After reading the connection file and binding to the necessary sockets, the
71 kernel should go into an event loop, listening on the hb (heartbeat), control
72 and shell sockets.
73
74 :ref:`Heartbeat <kernel_heartbeat>` messages should be echoed back immediately
75 on the same socket - the frontend uses this to check that the kernel is still
76 alive.
77
78 Messages on the control and shell sockets should be parsed, and their signature
79 validated. See :ref:`wire_protocol` for how to do this.
80
81 The kernel will send messages on the iopub socket to display output, and on the
82 stdin socket to prompt the user for textual input.
83
84 .. seealso::
85
86 :doc:`messaging`
87 Details of the different sockets and the messages that come over them
88
89 `Creating Language Kernels for IPython <http://andrew.gibiansky.com/blog/ipython/ipython-kernels/>`_
90 A blog post by the author of `IHaskell <https://github.com/gibiansky/IHaskell>`_,
91 a Haskell kernel
92
93 `simple_kernel <https://github.com/dsblank/simple_kernel>`_
94 A simple example implementation of the kernel machinery in Python
95
96
97 .. _kernelspecs:
98
99 Kernel specs
100 ============
101
102 A kernel identifies itself to IPython by creating a directory, the name of which
103 is used as an identifier for the kernel. These may be created in a number of
104 locations:
105
106 +--------+--------------------------------------------+-----------------------------------+
107 | | Unix | Windows |
108 +========+============================================+===================================+
109 | System | ``/usr/share/jupyter/kernels`` | ``%PROGRAMDATA%\jupyter\kernels`` |
110 | | | |
111 | | ``/usr/local/share/jupyter/kernels`` | |
112 +--------+--------------------------------------------+-----------------------------------+
113 | Env | ``{sys.prefix}/share/jupyter/kernels`` |
114 +--------+--------------------------------------------+-----------------------------------+
115 | User | ``~/.local/share/jupyter/kernels`` (Linux) | ``%APPDATA%\jupyter\kernels`` |
116 | | | |
117 | | ``~/Library/Jupyter/kernels`` (Mac) | |
118 +--------+--------------------------------------------+-----------------------------------+
119
120 The user location takes priority over the system locations, and the case of the
121 names is ignored, so selecting kernels works the same way whether or not the
122 filesystem is case sensitive.
123 Since kernelspecs show up in URLs and other places,
124 a kernelspec is required to have a simple name, only containing ASCII letters,
125 ASCII numbers, and the simple separators: ``-`` hyphen, ``.`` period, ``_``
126 underscore.
127
128 Other locations may also be searched if the :envvar:`JUPYTER_PATH` environment
129 variable is set.
130
131 Inside the kernel directory, three types of files are presently used:
132 ``kernel.json``, ``kernel.js``, and logo image files. Currently, no other
133 files are used, but this may change in the future.
134
135 Inside the directory, the most important file is *kernel.json*. This should be a
136 JSON serialised dictionary containing the following keys and values:
137
138 - **argv**: A list of command line arguments used to start the kernel. The text
139 ``{connection_file}`` in any argument will be replaced with the path to the
140 connection file.
141 - **display_name**: The kernel's name as it should be displayed in the UI.
142 Unlike the kernel name used in the API, this can contain arbitrary unicode
143 characters.
144 - **language**: The name of the language of the kernel.
145 When loading notebooks, if no matching kernelspec key (may differ across machines)
146 is found, a kernel with a matching ``language`` will be used.
147 This allows a notebook written on any Python or Julia kernel to be properly associated
148 with the user's Python or Julia kernel, even if they aren't listed under the
149 same name as the author's.
150 - **interrupt_mode** (optional): May be either ``signal`` or ``message`` and
151 specifies how a client is supposed to interrupt cell execution on this kernel,
152 either by sending an interrupt ``signal`` via the operating system's
153 signalling facilities (e.g. ``SIGINT`` on POSIX systems), or by sending an
154 ``interrupt_request`` message on the control channel (see
155 :ref:`msging_interrupt`). If this is not specified
156 the client will default to ``signal`` mode.
157 - **env** (optional): A dictionary of environment variables to set for the kernel.
158 These will be added to the current environment variables before the kernel is
159 started. Existing environment variables can be referenced using ``${<ENV_VAR>}`` and
160 will be substituted with the corresponding value. Administrators should note that use
161 of ``${<ENV_VAR>}`` can expose sensitive variables and should use only in controlled
162 circumstances.
163 - **metadata** (optional): A dictionary of additional attributes about this
164 kernel; used by clients to aid in kernel selection. Metadata added
165 here should be namespaced for the tool reading and writing that metadata.
166
167 For example, the kernel.json file for IPython looks like this::
168
169 {
170 "argv": ["python3", "-m", "IPython.kernel",
171 "-f", "{connection_file}"],
172 "display_name": "Python 3",
173 "language": "python"
174 }
175
176 To see the available kernel specs, run::
177
178 jupyter kernelspec list
179
180 To start the terminal console or the Qt console with a specific kernel::
181
182 jupyter console --kernel bash
183 jupyter qtconsole --kernel bash
184
185 The notebook offers you the available kernels in a dropdown menu from the 'New'
186 button.
187
188
189 .. _packaging-kernels:
190
191 Packaging
192 =========
193
194 To release your kernel as a Python package, we recommend following the pattern
195 used in the `echo_kernel`_, which uses the `hatch`_ build backend and
196 a build file that creates the kernel directory with the ``kernel.json`` and
197 kernel icons, which is included as ``shared-data``, ending up in the
198 ``share/jupyter/kernels/`` folder in the user's installed environment.
199 See `pyproject.toml`_ and `hatch_build.py`_ for more details.
200
201 .. _hatch: https://hatch.pypa.io/latest/
202 .. _pyproject.toml: https://github.com/jupyter/echo_kernel/blob/main/pyproject.toml
203 .. _hatch_build.py: https://github.com/jupyter/echo_kernel/blob/main/hatch_build.py
204 .. _echo_kernel: https://github.com/jupyter/echo_kernel