@jupyterlab/inspector 4.0.0-alpha.19 → 4.0.0-alpha.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +18 -17
- package/src/handler.ts +250 -0
- package/src/index.ts +11 -0
- package/src/inspector.ts +167 -0
- package/src/kernelconnector.ts +74 -0
- package/src/tokens.ts +79 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jupyterlab/inspector",
|
|
3
|
-
"version": "4.0.0-alpha.
|
|
3
|
+
"version": "4.0.0-alpha.20",
|
|
4
4
|
"description": "JupyterLab - Code Inspector",
|
|
5
5
|
"homepage": "https://github.com/jupyterlab/jupyterlab",
|
|
6
6
|
"bugs": {
|
|
@@ -27,7 +27,8 @@
|
|
|
27
27
|
"lib/*.js.map",
|
|
28
28
|
"lib/*.js",
|
|
29
29
|
"style/*.css",
|
|
30
|
-
"style/index.js"
|
|
30
|
+
"style/index.js",
|
|
31
|
+
"src/**/*.{ts,tsx}"
|
|
31
32
|
],
|
|
32
33
|
"scripts": {
|
|
33
34
|
"build": "tsc -b",
|
|
@@ -41,25 +42,25 @@
|
|
|
41
42
|
"watch": "tsc -b --watch"
|
|
42
43
|
},
|
|
43
44
|
"dependencies": {
|
|
44
|
-
"@jupyterlab/apputils": "^4.0.0-alpha.
|
|
45
|
-
"@jupyterlab/codeeditor": "^4.0.0-alpha.
|
|
46
|
-
"@jupyterlab/coreutils": "^6.0.0-alpha.
|
|
47
|
-
"@jupyterlab/rendermime": "^4.0.0-alpha.
|
|
48
|
-
"@jupyterlab/services": "^7.0.0-alpha.
|
|
49
|
-
"@jupyterlab/statedb": "^4.0.0-alpha.
|
|
50
|
-
"@jupyterlab/translation": "^4.0.0-alpha.
|
|
51
|
-
"@lumino/coreutils": "^2.0.0-
|
|
52
|
-
"@lumino/disposable": "^2.0.0-
|
|
53
|
-
"@lumino/polling": "^2.0.0-
|
|
54
|
-
"@lumino/signaling": "^2.0.0-
|
|
55
|
-
"@lumino/widgets": "^2.0.0-
|
|
45
|
+
"@jupyterlab/apputils": "^4.0.0-alpha.20",
|
|
46
|
+
"@jupyterlab/codeeditor": "^4.0.0-alpha.20",
|
|
47
|
+
"@jupyterlab/coreutils": "^6.0.0-alpha.20",
|
|
48
|
+
"@jupyterlab/rendermime": "^4.0.0-alpha.20",
|
|
49
|
+
"@jupyterlab/services": "^7.0.0-alpha.20",
|
|
50
|
+
"@jupyterlab/statedb": "^4.0.0-alpha.20",
|
|
51
|
+
"@jupyterlab/translation": "^4.0.0-alpha.20",
|
|
52
|
+
"@lumino/coreutils": "^2.0.0-rc.0",
|
|
53
|
+
"@lumino/disposable": "^2.0.0-rc.0",
|
|
54
|
+
"@lumino/polling": "^2.0.0-rc.0",
|
|
55
|
+
"@lumino/signaling": "^2.0.0-rc.0",
|
|
56
|
+
"@lumino/widgets": "^2.0.0-rc.0"
|
|
56
57
|
},
|
|
57
58
|
"devDependencies": {
|
|
58
|
-
"@jupyterlab/testing": "^4.0.0-alpha.
|
|
59
|
+
"@jupyterlab/testing": "^4.0.0-alpha.20",
|
|
59
60
|
"@types/jest": "^29.2.0",
|
|
60
61
|
"rimraf": "~3.0.0",
|
|
61
|
-
"typedoc": "~0.
|
|
62
|
-
"typescript": "~
|
|
62
|
+
"typedoc": "~0.23.25",
|
|
63
|
+
"typescript": "~5.0.0-beta"
|
|
63
64
|
},
|
|
64
65
|
"publishConfig": {
|
|
65
66
|
"access": "public"
|
package/src/handler.ts
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
// Copyright (c) Jupyter Development Team.
|
|
2
|
+
// Distributed under the terms of the Modified BSD License.
|
|
3
|
+
|
|
4
|
+
import { CodeEditor } from '@jupyterlab/codeeditor';
|
|
5
|
+
import { Text } from '@jupyterlab/coreutils';
|
|
6
|
+
import { IRenderMimeRegistry, MimeModel } from '@jupyterlab/rendermime';
|
|
7
|
+
import { IDataConnector } from '@jupyterlab/statedb';
|
|
8
|
+
import { JSONExt, ReadonlyJSONObject } from '@lumino/coreutils';
|
|
9
|
+
import { IDisposable } from '@lumino/disposable';
|
|
10
|
+
import { Debouncer } from '@lumino/polling';
|
|
11
|
+
import { ISignal, Signal } from '@lumino/signaling';
|
|
12
|
+
import { IInspector } from './tokens';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* An object that handles code inspection.
|
|
16
|
+
*/
|
|
17
|
+
export class InspectionHandler implements IDisposable, IInspector.IInspectable {
|
|
18
|
+
/**
|
|
19
|
+
* Construct a new inspection handler for a widget.
|
|
20
|
+
*/
|
|
21
|
+
constructor(options: InspectionHandler.IOptions) {
|
|
22
|
+
this._connector = options.connector;
|
|
23
|
+
this._rendermime = options.rendermime;
|
|
24
|
+
this._debouncer = new Debouncer(this.onEditorChange.bind(this), 250);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A signal emitted when the inspector should clear all items.
|
|
29
|
+
*/
|
|
30
|
+
get cleared(): ISignal<InspectionHandler, void> {
|
|
31
|
+
return this._cleared;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A signal emitted when the handler is disposed.
|
|
36
|
+
*/
|
|
37
|
+
get disposed(): ISignal<InspectionHandler, void> {
|
|
38
|
+
return this._disposed;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* A signal emitted when an inspector value is generated.
|
|
43
|
+
*/
|
|
44
|
+
get inspected(): ISignal<InspectionHandler, IInspector.IInspectorUpdate> {
|
|
45
|
+
return this._inspected;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The editor widget used by the inspection handler.
|
|
50
|
+
*/
|
|
51
|
+
get editor(): CodeEditor.IEditor | null {
|
|
52
|
+
return this._editor;
|
|
53
|
+
}
|
|
54
|
+
set editor(newValue: CodeEditor.IEditor | null) {
|
|
55
|
+
if (newValue === this._editor) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
// Remove all of our listeners.
|
|
59
|
+
Signal.disconnectReceiver(this);
|
|
60
|
+
|
|
61
|
+
const editor = (this._editor = newValue);
|
|
62
|
+
if (editor) {
|
|
63
|
+
// Clear the inspector in preparation for a new editor.
|
|
64
|
+
this._cleared.emit(void 0);
|
|
65
|
+
// Call onEditorChange to cover the case where the user changes
|
|
66
|
+
// the active cell
|
|
67
|
+
this.onEditorChange();
|
|
68
|
+
editor.model.selections.changed.connect(this._onChange, this);
|
|
69
|
+
editor.model.sharedModel.changed.connect(this._onChange, this);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Indicates whether the handler makes API inspection requests or stands by.
|
|
75
|
+
*
|
|
76
|
+
* #### Notes
|
|
77
|
+
* The use case for this attribute is to limit the API traffic when no
|
|
78
|
+
* inspector is visible.
|
|
79
|
+
*/
|
|
80
|
+
get standby(): boolean {
|
|
81
|
+
return this._standby;
|
|
82
|
+
}
|
|
83
|
+
set standby(value: boolean) {
|
|
84
|
+
this._standby = value;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Get whether the inspection handler is disposed.
|
|
89
|
+
*
|
|
90
|
+
* #### Notes
|
|
91
|
+
* This is a read-only property.
|
|
92
|
+
*/
|
|
93
|
+
get isDisposed(): boolean {
|
|
94
|
+
return this._isDisposed;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Dispose of the resources used by the handler.
|
|
99
|
+
*/
|
|
100
|
+
dispose(): void {
|
|
101
|
+
if (this.isDisposed) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
this._isDisposed = true;
|
|
105
|
+
this._debouncer.dispose();
|
|
106
|
+
this._disposed.emit(void 0);
|
|
107
|
+
Signal.clearData(this);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Handle a text changed signal from an editor.
|
|
112
|
+
*
|
|
113
|
+
* #### Notes
|
|
114
|
+
* Update the hints inspector based on a text change.
|
|
115
|
+
*/
|
|
116
|
+
onEditorChange(customText?: string): void {
|
|
117
|
+
// If the handler is in standby mode, bail.
|
|
118
|
+
if (this._standby) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const editor = this.editor;
|
|
123
|
+
|
|
124
|
+
if (!editor) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
const text = customText ? customText : editor.model.sharedModel.getSource();
|
|
128
|
+
const position = editor.getCursorPosition();
|
|
129
|
+
const offset = Text.jsIndexToCharIndex(editor.getOffsetAt(position), text);
|
|
130
|
+
const update: IInspector.IInspectorUpdate = { content: null };
|
|
131
|
+
|
|
132
|
+
const pending = ++this._pending;
|
|
133
|
+
|
|
134
|
+
void this._connector
|
|
135
|
+
.fetch({ offset, text })
|
|
136
|
+
.then(reply => {
|
|
137
|
+
// If handler has been disposed or a newer request is pending, bail.
|
|
138
|
+
if (!reply || this.isDisposed || pending !== this._pending) {
|
|
139
|
+
this._lastInspectedReply = null;
|
|
140
|
+
this._inspected.emit(update);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const { data } = reply;
|
|
145
|
+
|
|
146
|
+
// Do not update if there would be no change.
|
|
147
|
+
if (
|
|
148
|
+
this._lastInspectedReply &&
|
|
149
|
+
JSONExt.deepEqual(this._lastInspectedReply, data)
|
|
150
|
+
) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const mimeType = this._rendermime.preferredMimeType(data);
|
|
155
|
+
if (mimeType) {
|
|
156
|
+
const widget = this._rendermime.createRenderer(mimeType);
|
|
157
|
+
const model = new MimeModel({ data });
|
|
158
|
+
|
|
159
|
+
void widget.renderModel(model);
|
|
160
|
+
update.content = widget;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
this._lastInspectedReply = reply.data;
|
|
164
|
+
this._inspected.emit(update);
|
|
165
|
+
})
|
|
166
|
+
.catch(reason => {
|
|
167
|
+
// Since almost all failures are benign, fail silently.
|
|
168
|
+
this._lastInspectedReply = null;
|
|
169
|
+
this._inspected.emit(update);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Handle changes to the editor state, debouncing.
|
|
175
|
+
*/
|
|
176
|
+
private _onChange(): void {
|
|
177
|
+
void this._debouncer.invoke();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private _cleared = new Signal<InspectionHandler, void>(this);
|
|
181
|
+
private _connector: IDataConnector<
|
|
182
|
+
InspectionHandler.IReply,
|
|
183
|
+
void,
|
|
184
|
+
InspectionHandler.IRequest
|
|
185
|
+
>;
|
|
186
|
+
private _disposed = new Signal<this, void>(this);
|
|
187
|
+
private _editor: CodeEditor.IEditor | null = null;
|
|
188
|
+
private _inspected = new Signal<this, IInspector.IInspectorUpdate>(this);
|
|
189
|
+
private _isDisposed = false;
|
|
190
|
+
private _pending = 0;
|
|
191
|
+
private _rendermime: IRenderMimeRegistry;
|
|
192
|
+
private _standby = true;
|
|
193
|
+
private _debouncer: Debouncer;
|
|
194
|
+
private _lastInspectedReply: InspectionHandler.IReply['data'] | null = null;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* A namespace for inspection handler statics.
|
|
199
|
+
*/
|
|
200
|
+
export namespace InspectionHandler {
|
|
201
|
+
/**
|
|
202
|
+
* The instantiation options for an inspection handler.
|
|
203
|
+
*/
|
|
204
|
+
export interface IOptions {
|
|
205
|
+
/**
|
|
206
|
+
* The connector used to make inspection requests.
|
|
207
|
+
*
|
|
208
|
+
* #### Notes
|
|
209
|
+
* The only method of this connector that will ever be called is `fetch`, so
|
|
210
|
+
* it is acceptable for the other methods to be simple functions that return
|
|
211
|
+
* rejected promises.
|
|
212
|
+
*/
|
|
213
|
+
connector: IDataConnector<IReply, void, IRequest>;
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* The mime renderer for the inspection handler.
|
|
217
|
+
*/
|
|
218
|
+
rendermime: IRenderMimeRegistry;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* A reply to an inspection request.
|
|
223
|
+
*/
|
|
224
|
+
export interface IReply {
|
|
225
|
+
/**
|
|
226
|
+
* The MIME bundle data returned from an inspection request.
|
|
227
|
+
*/
|
|
228
|
+
data: ReadonlyJSONObject;
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Any metadata that accompanies the MIME bundle returning from a request.
|
|
232
|
+
*/
|
|
233
|
+
metadata: ReadonlyJSONObject;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* The details of an inspection request.
|
|
238
|
+
*/
|
|
239
|
+
export interface IRequest {
|
|
240
|
+
/**
|
|
241
|
+
* The cursor offset position within the text being inspected.
|
|
242
|
+
*/
|
|
243
|
+
offset: number;
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* The text being inspected.
|
|
247
|
+
*/
|
|
248
|
+
text: string;
|
|
249
|
+
}
|
|
250
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Copyright (c) Jupyter Development Team.
|
|
2
|
+
// Distributed under the terms of the Modified BSD License.
|
|
3
|
+
/**
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
* @module inspector
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export * from './handler';
|
|
9
|
+
export * from './inspector';
|
|
10
|
+
export * from './kernelconnector';
|
|
11
|
+
export * from './tokens';
|
package/src/inspector.ts
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
// Copyright (c) Jupyter Development Team.
|
|
2
|
+
// Distributed under the terms of the Modified BSD License.
|
|
3
|
+
|
|
4
|
+
import { Printing } from '@jupyterlab/apputils';
|
|
5
|
+
import {
|
|
6
|
+
ITranslator,
|
|
7
|
+
nullTranslator,
|
|
8
|
+
TranslationBundle
|
|
9
|
+
} from '@jupyterlab/translation';
|
|
10
|
+
import { Panel, PanelLayout, Widget } from '@lumino/widgets';
|
|
11
|
+
import { IInspector } from './tokens';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The class name added to inspector panels.
|
|
15
|
+
*/
|
|
16
|
+
const PANEL_CLASS = 'jp-Inspector';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The class name added to inspector content.
|
|
20
|
+
*/
|
|
21
|
+
const CONTENT_CLASS = 'jp-Inspector-content';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The class name added to default inspector content.
|
|
25
|
+
*/
|
|
26
|
+
const DEFAULT_CONTENT_CLASS = 'jp-Inspector-default-content';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A panel which contains a set of inspectors.
|
|
30
|
+
*/
|
|
31
|
+
export class InspectorPanel
|
|
32
|
+
extends Panel
|
|
33
|
+
implements IInspector, Printing.IPrintable
|
|
34
|
+
{
|
|
35
|
+
/**
|
|
36
|
+
* Construct an inspector.
|
|
37
|
+
*/
|
|
38
|
+
constructor(options: InspectorPanel.IOptions = {}) {
|
|
39
|
+
super();
|
|
40
|
+
this.translator = options.translator || nullTranslator;
|
|
41
|
+
this._trans = this.translator.load('jupyterlab');
|
|
42
|
+
|
|
43
|
+
if (options.initialContent instanceof Widget) {
|
|
44
|
+
this._content = options.initialContent;
|
|
45
|
+
} else if (typeof options.initialContent === 'string') {
|
|
46
|
+
this._content = InspectorPanel._generateContentWidget(
|
|
47
|
+
`<p>${options.initialContent}</p>`
|
|
48
|
+
);
|
|
49
|
+
} else {
|
|
50
|
+
this._content = InspectorPanel._generateContentWidget(
|
|
51
|
+
'<p>' +
|
|
52
|
+
this._trans.__('Click on a function to see documentation.') +
|
|
53
|
+
'</p>'
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
this.addClass(PANEL_CLASS);
|
|
58
|
+
(this.layout as PanelLayout).addWidget(this._content);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Print in iframe
|
|
63
|
+
*/
|
|
64
|
+
[Printing.symbol]() {
|
|
65
|
+
return (): Promise<void> => Printing.printWidget(this);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The source of events the inspector panel listens for.
|
|
70
|
+
*/
|
|
71
|
+
get source(): IInspector.IInspectable | null {
|
|
72
|
+
return this._source;
|
|
73
|
+
}
|
|
74
|
+
set source(source: IInspector.IInspectable | null) {
|
|
75
|
+
if (this._source === source) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Disconnect old signal handler.
|
|
80
|
+
if (this._source) {
|
|
81
|
+
this._source.standby = true;
|
|
82
|
+
this._source.inspected.disconnect(this.onInspectorUpdate, this);
|
|
83
|
+
this._source.disposed.disconnect(this.onSourceDisposed, this);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Reject a source that is already disposed.
|
|
87
|
+
if (source && source.isDisposed) {
|
|
88
|
+
source = null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Update source.
|
|
92
|
+
this._source = source;
|
|
93
|
+
|
|
94
|
+
// Connect new signal handler.
|
|
95
|
+
if (this._source) {
|
|
96
|
+
this._source.standby = false;
|
|
97
|
+
this._source.inspected.connect(this.onInspectorUpdate, this);
|
|
98
|
+
this._source.disposed.connect(this.onSourceDisposed, this);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Dispose of the resources held by the widget.
|
|
104
|
+
*/
|
|
105
|
+
dispose(): void {
|
|
106
|
+
if (this.isDisposed) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
this.source = null;
|
|
110
|
+
super.dispose();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Handle inspector update signals.
|
|
115
|
+
*/
|
|
116
|
+
protected onInspectorUpdate(
|
|
117
|
+
sender: any,
|
|
118
|
+
args: IInspector.IInspectorUpdate
|
|
119
|
+
): void {
|
|
120
|
+
const { content } = args;
|
|
121
|
+
|
|
122
|
+
// Update the content of the inspector widget.
|
|
123
|
+
if (!content || content === this._content) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
this._content.dispose();
|
|
127
|
+
|
|
128
|
+
this._content = content;
|
|
129
|
+
content.addClass(CONTENT_CLASS);
|
|
130
|
+
(this.layout as PanelLayout).addWidget(content);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Handle source disposed signals.
|
|
135
|
+
*/
|
|
136
|
+
protected onSourceDisposed(sender: any, args: void): void {
|
|
137
|
+
this.source = null;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Generate content widget from string
|
|
142
|
+
*/
|
|
143
|
+
private static _generateContentWidget(message: string): Widget {
|
|
144
|
+
const widget = new Widget();
|
|
145
|
+
widget.node.innerHTML = message;
|
|
146
|
+
widget.addClass(CONTENT_CLASS);
|
|
147
|
+
widget.addClass(DEFAULT_CONTENT_CLASS);
|
|
148
|
+
|
|
149
|
+
return widget;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
protected translator: ITranslator;
|
|
153
|
+
private _trans: TranslationBundle;
|
|
154
|
+
private _content: Widget;
|
|
155
|
+
private _source: IInspector.IInspectable | null = null;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export namespace InspectorPanel {
|
|
159
|
+
export interface IOptions {
|
|
160
|
+
initialContent?: Widget | string | undefined;
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* The application language translator.
|
|
164
|
+
*/
|
|
165
|
+
translator?: ITranslator;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// Copyright (c) Jupyter Development Team.
|
|
2
|
+
// Distributed under the terms of the Modified BSD License.
|
|
3
|
+
|
|
4
|
+
import { ISessionContext } from '@jupyterlab/apputils';
|
|
5
|
+
import { KernelMessage } from '@jupyterlab/services';
|
|
6
|
+
import { DataConnector } from '@jupyterlab/statedb';
|
|
7
|
+
import { InspectionHandler } from './handler';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The default connector for making inspection requests from the Jupyter API.
|
|
11
|
+
*/
|
|
12
|
+
export class KernelConnector extends DataConnector<
|
|
13
|
+
InspectionHandler.IReply,
|
|
14
|
+
void,
|
|
15
|
+
InspectionHandler.IRequest
|
|
16
|
+
> {
|
|
17
|
+
/**
|
|
18
|
+
* Create a new kernel connector for inspection requests.
|
|
19
|
+
*
|
|
20
|
+
* @param options - The instantiation options for the kernel connector.
|
|
21
|
+
*/
|
|
22
|
+
constructor(options: KernelConnector.IOptions) {
|
|
23
|
+
super();
|
|
24
|
+
this._sessionContext = options.sessionContext;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Fetch inspection requests.
|
|
29
|
+
*
|
|
30
|
+
* @param request - The inspection request text and details.
|
|
31
|
+
*/
|
|
32
|
+
fetch(
|
|
33
|
+
request: InspectionHandler.IRequest
|
|
34
|
+
): Promise<InspectionHandler.IReply> {
|
|
35
|
+
const kernel = this._sessionContext.session?.kernel;
|
|
36
|
+
|
|
37
|
+
if (!kernel) {
|
|
38
|
+
return Promise.reject(new Error('Inspection fetch requires a kernel.'));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const contents: KernelMessage.IInspectRequestMsg['content'] = {
|
|
42
|
+
code: request.text,
|
|
43
|
+
cursor_pos: request.offset,
|
|
44
|
+
detail_level: 1
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
return kernel.requestInspect(contents).then(msg => {
|
|
48
|
+
const response = msg.content;
|
|
49
|
+
|
|
50
|
+
if (response.status !== 'ok' || !response.found) {
|
|
51
|
+
throw new Error('Inspection fetch failed to return successfully.');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return { data: response.data, metadata: response.metadata };
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private _sessionContext: ISessionContext;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A namespace for kernel connector statics.
|
|
63
|
+
*/
|
|
64
|
+
export namespace KernelConnector {
|
|
65
|
+
/**
|
|
66
|
+
* The instantiation options for an inspection handler.
|
|
67
|
+
*/
|
|
68
|
+
export interface IOptions {
|
|
69
|
+
/**
|
|
70
|
+
* The session context used to make API requests to the kernel.
|
|
71
|
+
*/
|
|
72
|
+
sessionContext: ISessionContext;
|
|
73
|
+
}
|
|
74
|
+
}
|
package/src/tokens.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// Copyright (c) Jupyter Development Team.
|
|
2
|
+
// Distributed under the terms of the Modified BSD License.
|
|
3
|
+
|
|
4
|
+
import { Token } from '@lumino/coreutils';
|
|
5
|
+
import { ISignal } from '@lumino/signaling';
|
|
6
|
+
import { Widget } from '@lumino/widgets';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The inspector panel token.
|
|
10
|
+
*/
|
|
11
|
+
export const IInspector = new Token<IInspector>(
|
|
12
|
+
'@jupyterlab/inspector:IInspector'
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* An interface for an inspector.
|
|
17
|
+
*/
|
|
18
|
+
export interface IInspector {
|
|
19
|
+
/**
|
|
20
|
+
* The source of events the inspector listens for.
|
|
21
|
+
*/
|
|
22
|
+
source: IInspector.IInspectable | null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A namespace for inspector interfaces.
|
|
27
|
+
*/
|
|
28
|
+
export namespace IInspector {
|
|
29
|
+
/**
|
|
30
|
+
* The definition of an inspectable source.
|
|
31
|
+
*/
|
|
32
|
+
export interface IInspectable {
|
|
33
|
+
/**
|
|
34
|
+
* A signal emitted when the inspector should clear all items.
|
|
35
|
+
*/
|
|
36
|
+
cleared: ISignal<any, void>;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* A signal emitted when the inspectable is disposed.
|
|
40
|
+
*/
|
|
41
|
+
disposed: ISignal<any, void>;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A signal emitted when an inspector value is generated.
|
|
45
|
+
*/
|
|
46
|
+
inspected: ISignal<any, IInspectorUpdate>;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Test whether the inspectable has been disposed.
|
|
50
|
+
*/
|
|
51
|
+
isDisposed: boolean;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Indicates whether the inspectable source emits signals.
|
|
55
|
+
*
|
|
56
|
+
* #### Notes
|
|
57
|
+
* The use case for this attribute is to limit the API traffic when no
|
|
58
|
+
* inspector is visible. It can be modified by the consumer of the source.
|
|
59
|
+
*/
|
|
60
|
+
standby: boolean;
|
|
61
|
+
/**
|
|
62
|
+
* Handle a text changed signal from an editor.
|
|
63
|
+
*
|
|
64
|
+
* #### Notes
|
|
65
|
+
* Update the hints inspector based on a text change.
|
|
66
|
+
*/
|
|
67
|
+
onEditorChange(customText?: string): void;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* An update value for code inspectors.
|
|
72
|
+
*/
|
|
73
|
+
export interface IInspectorUpdate {
|
|
74
|
+
/**
|
|
75
|
+
* The content being sent to the inspector for display.
|
|
76
|
+
*/
|
|
77
|
+
content: Widget | null;
|
|
78
|
+
}
|
|
79
|
+
}
|