本页尚未翻译,以下为英文原文。
#9.8. Jupyter kernel integration
本页内容
- 9.8.1. Kernel
- Input
- Notebook cells and REPL cells
- 9.8.2. Local native kernel: operation
- Socket channels
- Message format
- Supported messages
- Execution
- 9.8.3. kernelspec installation
- 9.8.4. Interrupt / shutdown
- Interrupt
- Shutdown (kernel restart)
- 9.8.5. Code completion
- 9.8.6. inspect_request: help
- 9.8.7. Code sharing between REPL and Jupyter
- Differences
- 9.8.8. Security / isolation
- 9.8.9. Next
This section defines the Nabi kernel of the Jupyter surface.
#9.8.1. Kernel
Jupyter runs the Nabi kernel as a separate process and communicates over the ZeroMQ-based Jupyter wire protocol. The kernel announces protocol_version as "5.3" (the value jupyter_client expects; works with clients 5.3 and later). In this form all OS resources, i.e. files, network and external processes, are available.
The kernel runs on the same cell session as the terminal REPL and follows the rules of 9.7.0: each cell runs exactly once, bindings and items carry over to later cells, and a failed cell leaves no trace in the bindings. The kernel's engine is the same JIT session as the REPL: a top-level await and it work as there (9.7.1, 9.7.7).
#Input
A cell reads stdin through the client. When the execute_request has allow_stdin: true, each read the runtime makes of stdin sends an input_request (prompt empty, password false) on the stdin channel, to the client of that request; what the cell wrote before the read, such as a prompt it printed, has been sent as stream first. The value of the input_reply, with a newline added, is what the read returns. Without allow_stdin, or for a silent request, a read is end of input. An interrupt while the cell waits for the reply ends the cell (9.8.4).
#Notebook cells and REPL cells
A notebook cell is split into several REPL cells by the same rules the terminal REPL uses to split input, and they run in order. As in IPython, only the last value is displayed as the result. If a later part of a notebook cell fails, the effects of the earlier parts (the preceding REPL cells) remain.
The REPL cells take their number in runtime locations (<repl cell N>, 9.7.5) from the execution count: a notebook cell that is one REPL cell is N, one that splits into several numbers them N.1, N.2 and so on. A request that does not advance the count (silent, or store_history: false) numbers its REPL cells on after the last number under the current count.
#9.8.2. Local native kernel: operation
When the user selects "Nabi" as the notebook's kernel, Jupyter runs the command of the kernelspec (9.8.3) as a separate process.
nabi jupyter --connection-file <path> # resolves dependencies; also accepts build options such as --features
The kernel reads the ZMQ ports and key from the connection file, then binds and listens on its five sockets. Once all five accept connections, it writes the line nabi jupyter: ready to its stderr, before output capture starts. Only the tcp transport is supported (no ipc), and CURVE encryption is not used. Messages are HMAC-signed with the connection file's key.
The kernel loads the package it starts in (the current directory or the nearest one above it with src/main.nabi or src/lib.nabi) as nabi repl . does (9.7.10): a library package is a dependency with use <name>::*; as the first cell; a program package is loaded and run. This happens after output capture starts; what it prints is sent with the output of the first executed cell. Outside a package the session starts empty.
#Socket channels
| Channel | Purpose |
|---|---|
shell | Requests (execute, kernel_info, complete, is_complete, etc.) |
iopub | Output and status broadcast (status, stream, execute_result, etc.) |
stdin | input_request / input_reply for a cell that reads stdin (9.8.1) |
control | interrupt, shutdown |
heartbeat | Sends back whatever it receives (echo) |
#Message format
Each message is serialized as six or more ZMQ frames.
[b"u-u-i-d", # zmq identity(ies)
b"<IDS|MSG>", # delimiter
b"baddad42", # HMAC signature (with key)
b"{header}", # header JSON
b"{parent_header}", # parent header JSON
b"{metadata}", # metadata JSON
b"{content}", # content JSON
]
The detailed format follows the Jupyter wire protocol standard. Every request is wrapped in iopub status: busy and status: idle.
#Supported messages
| Request | Reply |
|---|---|
kernel_info_request | language_info with name nabi, mimetype text/x-nabi, file_extension .nabi, codemirror_mode and pygments_lexer rust (highlighting until front ends know Nabi); help_links to the documentation (https://docs.nabi-lang.org/latest/) and this REPL chapter; banner the REPL's toolchain line (9.7.8); protocol_version "5.3" |
execute_request | See "Execution" below |
is_complete_request | complete / incomplete decided by the REPL continuation rules; indent is four spaces per bracket still open |
complete_request | The REPL's completion candidates (9.8.5) |
inspect_request | found: true with text/plain help for the name at the cursor (9.8.6), else false |
history_request | The cells executed by this kernel |
comm_info_request | Empty list |
shutdown_request | On shell or control. Exits after replying |
interrupt_request | On control (9.8.4) |
#Execution
On receiving an execute_request, the kernel sends:
- iopub
execute_input(code and execution count). - The cell's output as it is written: what a running cell writes to stdout / stderr is sent as iopub
stream(stdout/stderr) as the kernel reads it. The two are separate pipes, so chunks of one keep their order and chunks of both come in the order they are read. Output written outside a run (diagnostics,:commands) is sent before the next run or when the cell finishes. - If there is a value, iopub
execute_result:text/plainholding the same output as the REPL (9.7.7). A value whose type implementsRichDisplay(stdlib 2.10) is shown throughdisplayinstead: iopubdisplay_datawhosedatais itsmime_bundle()(image/svg+xml,image/png,text/html, ...), and noexecute_result. - On failure, iopub
error:evalueis the first line of the stderr not yet sent (the diagnostics of a cell that does not compile, or the report of how a run ended),tracebackits lines. What the cell itself wrote to stderr, such as a panic message, has been streamed before. - shell
execute_reply(includingexecution_count, with success / failure status).
Output MIME types are text/plain and those of a RichDisplay bundle. display(x) sends display_data mid-cell, in its place among the cell's streamed output: it writes a framed bundle to stdout (the kernel sets NABI_JUPYTER for the cells it runs), which the kernel takes out of the text. Updating a display in place (update_display_data) is not supported.
#9.8.3. kernelspec installation
Jupyter only recognizes kernels installed in a kernelspec directory.
nabi jupyter install [--env <name>]
nabi jupyter with no arguments is a usage error (exit code 2) that says what to run instead: Jupyter starts the kernel itself; to add it, run `nabi jupyter install`.
The kernelspec is kernels/nabi/kernel.json under the directory below. Installed with --env <name>, it is kernels/nabi-<name>/, with display name nabi (<name>) and command nabi --env <name> jupyter ....
$JUPYTER_DATA_DIRif set~/Library/Jupyteron macOS%APPDATA%\jupyteron Windows- Otherwise
$XDG_DATA_HOME/jupyter, or~/.local/share/jupyterif unset
Next to it are logo-32x32.png, logo-64x64.png and logo-svg.svg, the Nabi logo front ends show for the kernel. kernel.json runs the nabi proxy (9.10.6) and records the NABI_HOME and PATH at installation time in env.
{
"argv": ["/home/u/.nabi/bin/nabi", "jupyter", "--connection-file", "{connection_file}"],
"display_name": "nabi",
"language": "nabi",
"interrupt_mode": "message",
"env": { "NABI_HOME": "/home/u/.nabi", "PATH": "<PATH at install time>" }
}
Once installed, the Nabi kernel is usable from Jupyter, VS Code, nteract and others.
#9.8.4. Interrupt / shutdown
#Interrupt
Since interrupt_mode is "message", an interrupt arrives as an interrupt_request on the control socket. The running cell is terminated and the session continues from the state before that cell (as for a failed cell, 9.7.0). A cell waiting for an input_reply ends as well. A cell still being compiled cannot be interrupted.
#Shutdown (kernel restart)
After replying to a shutdown_request on the shell or control socket, the kernel cleans up the session and exits the process. On restart, Jupyter launches it again automatically.
#9.8.5. Code completion
complete_request matches the fragment before the cursor against the candidates of the terminal REPL's Tab completion (9.7.8): the session's bindings and items, the core library's names, the names the session's use items import, and keywords; members after . and items after ::. It replies with { matches, cursor_start, cursor_end }.
#9.8.6. inspect_request: help
inspect_request (Shift+Tab) replies with found: true and, as text/plain, the name at the cursor as name: type, its signature and doc comments (/// / //! of Lexical structure). For an unknown name it is found: false.
#9.8.7. Code sharing between REPL and Jupyter
Code written in the REPL can be moved to Jupyter cells unchanged, and vice versa, because both follow the same cell session and the same semantics (9.7.0).
#Differences
The two surfaces differ in a few respects, summarized below.
| Aspect | REPL | Jupyter |
|---|---|---|
| Input | One line / one expression | One cell (multiple lines) |
| Output | text only (display(x) prints its text/plain) | text, and the MIME types of RichDisplay |
| stdin | The terminal; empty when piped | input_request with allow_stdin |
| State | Discarded on exit | Cells kept in the notebook file |
#9.8.8. Security / isolation
The kernel runs arbitrary code with the user's privileges. No isolation is performed.
#9.8.9. Next
The following Debug / diagnostic tools defines gdb-friendly features, profiling, --emit and more.
Found a mistake? Edit this page on GitHub. Last updated