UNPKG

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