1 | # JupyterLab Services
|
2 |
|
3 | Javascript client for the Jupyter services REST APIs
|
4 |
|
5 | [API Docs](https://jupyterlab.readthedocs.io/en/stable/api/)
|
6 |
|
7 | [REST API Docs](https://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter-server/jupyter_server/main/jupyter_server/services/api/api.yaml)
|
8 |
|
9 | Note: All functions and classes using the REST API allow a `serverSettings`
|
10 | parameter to configure requests.
|
11 |
|
12 | Requests are made using the `fetch` API, which is available in modern browsers or in Node 18+.
|
13 |
|
14 | ## Package Install
|
15 |
|
16 | **Prerequisites**
|
17 |
|
18 | - [node](http://nodejs.org/)
|
19 | - Python, e.g. via [conda](https://www.anaconda.com/download)
|
20 |
|
21 | ```bash
|
22 | npm install --save @jupyterlab/services
|
23 | conda install jupyter-server # jupyter-server 2.0+ is recommended
|
24 | # or `pip install jupyter-server`
|
25 | ```
|
26 |
|
27 | ## Source Build
|
28 |
|
29 | **Prerequisites**
|
30 |
|
31 | See the [building instructions for JupyterLab](../../CONTRIBUTING.md), which
|
32 | will build this module as part of the build process.
|
33 |
|
34 | **Rebuild**
|
35 |
|
36 | ```bash
|
37 | yarn run clean
|
38 | yarn run build
|
39 | ```
|
40 |
|
41 | ## Run Tests
|
42 |
|
43 | Follow the source build instructions first.
|
44 |
|
45 | ```bash
|
46 | yarn run test
|
47 | ```
|
48 |
|
49 | ## Build Docs
|
50 |
|
51 | Follow the source build instructions first.
|
52 |
|
53 | ```bash
|
54 | yarn run docs
|
55 | ```
|
56 |
|
57 | Navigate to `docs/index.html`.
|
58 |
|
59 | ## Supported Runtimes
|
60 |
|
61 | The runtime versions which should work are listed below. Earlier versions may
|
62 | also work, but come with no guarantees.
|
63 |
|
64 | - Node 10
|
65 | - Firefox 52+
|
66 | - Chrome 55+
|
67 |
|
68 | Note: "requirejs" may need be included in a global context for `Comm` targets
|
69 | using the a `target_module` (in the classic Notebook).
|
70 | This can be as a `<script>` tag in the browser or by using the `requirejs`
|
71 | package in node (`npm install requirejs` and setting
|
72 | `global.requirejs = require('requirejs');`).
|
73 |
|
74 | ## Starting the Jupyter Server
|
75 |
|
76 | Follow the package install instructions first.
|
77 |
|
78 | The library requires a running Jupyter Server, launched as:
|
79 |
|
80 | ```bash
|
81 | jupyter server
|
82 | ```
|
83 |
|
84 | or
|
85 |
|
86 | ```bash
|
87 | jupyter lab
|
88 | ```
|
89 |
|
90 | ## Bundling for the Browser
|
91 |
|
92 | Follow the package install instructions first.
|
93 |
|
94 | See `examples/browser` for an example of using Webpack to bundle the library.
|
95 |
|
96 | ## Usage from Node.js
|
97 |
|
98 | Follow the package install instructions first.
|
99 |
|
100 | See `examples/node` for an example of using an ES5 node script.
|
101 |
|
102 | ## Usage Examples
|
103 |
|
104 | **Note:** This package is compiled to ES2018 JavaScript syntax from
|
105 | TypeScript. Here are some examples of using parts of this package. See the
|
106 | other `examples` subdirectories for more examples.
|
107 |
|
108 | - [Comms](./examples/browser/src/comm.ts)
|
109 | - [Config](./examples/browser/src/config.ts)
|
110 | - [Contents](./examples/browser/src/contents.ts)
|
111 | - [Kernel](./examples/browser/src/kernel.ts)
|
112 | - [Session](./examples/browser/src/session.ts)
|
113 | - [Terminal](./examples/browser/src/terminal.ts)
|
114 |
|
115 | ## Overview
|
116 |
|
117 | This package introduces a number of concepts, such as session context, etc.
|
118 | Here we give a brief overview of some of the top-level concepts in this
|
119 | package.
|
120 |
|
121 | ### Clients
|
122 |
|
123 | A _client_ is a single entity connected to a kernel. Since kernel messages
|
124 | include the client id, it is easy for a client to filter kernel messages for
|
125 | just messages between it and the kernel. In JupyterLab, different activities
|
126 | (such as a console and a notebook) are usually considered separate clients when
|
127 | connected to the same kernel.
|
128 |
|
129 | ### Kernel specs
|
130 |
|
131 | A _kernel spec_ is the data about an available kernel on the system. We can
|
132 | retrieve a current list of kernel specs from the server.
|
133 |
|
134 | ### Kernels
|
135 |
|
136 | A _kernel_ represents a running process on the server that implements the
|
137 | Jupyter kernel messaging protocol.
|
138 |
|
139 | #### Kernel model
|
140 |
|
141 | A _kernel model_ mirrors the server kernel models, and represents a single
|
142 | running kernel on the server. A kernel can be created, restarted, shut down,
|
143 | etc., through calls to the server. A kernel model's lifecycle mirrors the
|
144 | server kernel model's lifecycle, and it will be disposed when the server
|
145 | kernel is shut down.
|
146 |
|
147 | #### Kernel connection
|
148 |
|
149 | A _kernel connection_ represents a single client connecting to a kernel over a
|
150 | websocket. Typically only one kernel connection handles comms for any given
|
151 | kernel. The kernel connection is disposed when the client no longer has a need
|
152 | for the connection. Disposing a kernel connection does not cause the kernel to
|
153 | shut down. However, if a kernel is shut down, (eventually) all of its kernel
|
154 | connections should be disposed if they were initiated from a kernel manager.
|
155 | If the kernel connections were instantiated outside of a manager, you are
|
156 | responsible for cleaning them up.
|
157 |
|
158 | A kernel connection has a number of signals, such as kernel status, kernel
|
159 | connection status, etc.
|
160 |
|
161 | #### Kernel manager
|
162 |
|
163 | A _kernel manager_ is an object that maintains a list of kernel models by
|
164 | regular polling. The kernel manager can instantiate a kernel connection and
|
165 | will manage its lifecycle (e.g., when the kernel is shut down, the connections
|
166 | will be disposed). The manager provides some minimal bookkeeping around
|
167 | kernels and their connections. Generally, it is easiest to interact with
|
168 | kernels on a server through a manager.
|
169 |
|
170 | ### Sessions
|
171 |
|
172 | A _session_ is a mapping on the server from an identifying string (the
|
173 | session's `path`) to a kernel. A session has a few other pieces of information
|
174 | to allow for easy categorization and searching of sessions.
|
175 |
|
176 | The primary usecase of a session is to enable persisting a connection to a
|
177 | kernel. For example, a notebook viewer may start a session with session path
|
178 | of the notebook's file path. When a browser is refreshed, the notebook viewer
|
179 | can connect to the same kernel by asking the server for the session
|
180 | corresponding with the notebook file path.
|
181 |
|
182 | #### Session model
|
183 |
|
184 | A _session model_ mirrors a server session. The session models can be
|
185 | refreshed from the server, created, changed (including creating a new session
|
186 | kernel), and shut down (which implies that the kernel will be shut down). A
|
187 | session model's lifecycle mirrors the server session's lifecycle, and it will
|
188 | be disposed when the server session is shut down.
|
189 |
|
190 | #### Session connection
|
191 |
|
192 | A _session connection_ represents a single client connected to a session's
|
193 | kernel. A session's kernel connection can change and may be null to signify no
|
194 | current kernel connection. A session connection owns the kernel connection,
|
195 | meaning the kernel connection is created and disposed by the session
|
196 | connection as needed. The session connection proxies signals from the kernel
|
197 | connection for convenience (e.g., you can listen to the session's status
|
198 | signal to get status changes for whatever the current kernel is, without
|
199 | having to disconnect and reconnect your signal handlers every time the session
|
200 | kernel changes). The session connection can be disposed when the client no
|
201 | longer is connected to that session's kernel, and disposal will not cause the
|
202 | session model to be deleted.
|
203 |
|
204 | #### Session manager
|
205 |
|
206 | A _session manager_ is an object that maintains a list of session models by
|
207 | regular polling. The session manager can instantiate a session connection and
|
208 | will manage its lifecycle (e.g., when the session is shut down, the connections
|
209 | will be disposed). The manager provides some minimal bookkeeping around
|
210 | sessions and their connections. Generally, it is easiest to interact with
|
211 | sessions on a server through a manager.
|
212 |
|
213 | ### Session Context
|
214 |
|
215 | A _session context_ is an object which has the same lifecycle as the client.
|
216 | The session context owns a session connection (which may be null if the client
|
217 | is not currently associated with a session). The session context proxies the
|
218 | current session connection's signals for convenience. The session context
|
219 | primarily serves as a stable object for a client to keep track of the current
|
220 | session connection. The session context also contains some convenience
|
221 | functionality, such as preferences for whether a kernel should be started and
|
222 | a user-friendly kernel name and status.
|
223 |
|
224 | Here is a diagram that provides an overview of the different components:
|
225 |
|
226 | ![architecture-diagram](./architecture.png)
|
227 |
|
228 | _The diagram can be edited on [diagrams.net](https://diagrams.net) by importing the [source](./architecture.xml)_.
|