forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
panel.ts
494 lines (426 loc) · 11.6 KB
/
panel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
Kernel, KernelMessage
} from '@jupyterlab/services';
import {
each
} from '@phosphor/algorithm';
import {
Token
} from '@phosphor/coreutils';
import {
Message
} from '@phosphor/messaging';
import {
ISignal, Signal
} from '@phosphor/signaling';
import {
PanelLayout, Widget
} from '@phosphor/widgets';
import {
Toolbar
} from '@jupyterlab/apputils';
import {
BaseCellWidget, CodeCellWidget
} from '@jupyterlab/cells';
import {
IEditorMimeTypeService, CodeEditor
} from '@jupyterlab/codeeditor';
import {
IChangedArgs
} from '@jupyterlab/coreutils';
import {
DocumentRegistry
} from '@jupyterlab/docregistry';
import {
OutputAreaWidget
} from '@jupyterlab/outputarea';
import {
RenderMime
} from '@jupyterlab/rendermime';
import {
INotebookModel
} from './model';
import {
Notebook
} from './widget';
/**
* The class name added to notebook panels.
*/
const NB_PANEL = 'jp-Notebook-panel';
/**
* The class name added to a dirty widget.
*/
const DIRTY_CLASS = 'jp-mod-dirty';
/**
* A widget that hosts a notebook toolbar and content area.
*
* #### Notes
* The widget keeps the document metadata in sync with the current
* kernel on the context.
*/
export
class NotebookPanel extends Widget {
/**
* Construct a new notebook panel.
*/
constructor(options: NotebookPanel.IOptions) {
super();
this.addClass(NB_PANEL);
this.rendermime = options.rendermime;
let layout = this.layout = new PanelLayout();
let factory = this.contentFactory = options.contentFactory;
let nbOptions = {
rendermime: this.rendermime,
languagePreference: options.languagePreference,
contentFactory: factory.notebookContentFactory,
mimeTypeService: options.mimeTypeService
};
let toolbar = factory.createToolbar();
this.notebook = factory.createNotebook(nbOptions);
layout.addWidget(toolbar);
layout.addWidget(this.notebook);
}
/**
* A signal emitted when the panel has been activated.
*/
get activated(): ISignal<this, void> {
return this._activated;
}
/**
* A signal emitted when the panel context changes.
*/
get contextChanged(): ISignal<this, void> {
return this._contextChanged;
}
/**
* A signal emitted when the kernel used by the panel changes.
*/
get kernelChanged(): ISignal<this, Kernel.IKernel> {
return this._kernelChanged;
}
/**
* The factory used by the widget.
*/
readonly contentFactory: NotebookPanel.IContentFactory;
/**
* The Rendermime instance used by the widget.
*/
readonly rendermime: RenderMime;
/**
* The notebook used by the widget.
*/
readonly notebook: Notebook;
/**
* Get the toolbar used by the widget.
*/
get toolbar(): Toolbar<Widget> {
return (this.layout as PanelLayout).widgets[0] as Toolbar<Widget>;
}
/**
* Get the current kernel used by the panel.
*/
get kernel(): Kernel.IKernel {
return this._context ? this._context.kernel : null;
}
/**
* The model for the widget.
*/
get model(): INotebookModel {
return this.notebook ? this.notebook.model : null;
}
/**
* The document context for the widget.
*
* #### Notes
* Changing the context also changes the model on the
* `content`.
*/
get context(): DocumentRegistry.IContext<INotebookModel> {
return this._context;
}
set context(newValue: DocumentRegistry.IContext<INotebookModel>) {
newValue = newValue || null;
if (newValue === this._context) {
return;
}
let oldValue = this._context;
this._context = newValue;
this.rendermime.resolver = newValue;
// Trigger private, protected, and public changes.
this._onContextChanged(oldValue, newValue);
this.onContextChanged(oldValue, newValue);
this._contextChanged.emit(void 0);
}
/**
* Dispose of the resources used by the widget.
*/
dispose(): void {
this._context = null;
this.notebook.dispose();
super.dispose();
}
/**
* Handle `'activate-request'` messages.
*/
protected onActivateRequest(msg: Message): void {
this.notebook.activate();
this._activated.emit(void 0);
}
/**
* Handle a change to the document context.
*
* #### Notes
* The default implementation is a no-op.
*/
protected onContextChanged(oldValue: DocumentRegistry.IContext<INotebookModel>, newValue: DocumentRegistry.IContext<INotebookModel>): void {
// This is a no-op.
}
/**
* Handle a change in the model state.
*/
protected onModelStateChanged(sender: INotebookModel, args: IChangedArgs<any>): void {
if (args.name === 'dirty') {
this._handleDirtyState();
}
}
/**
* Handle a change to the document path.
*/
protected onPathChanged(sender: DocumentRegistry.IContext<INotebookModel>, path: string): void {
this.title.label = path.split('/').pop();
}
/**
* Handle a change in the context.
*/
private _onContextChanged(oldValue: DocumentRegistry.IContext<INotebookModel>, newValue: DocumentRegistry.IContext<INotebookModel>): void {
if (oldValue) {
oldValue.kernelChanged.disconnect(this._onKernelChanged, this);
oldValue.pathChanged.disconnect(this.onPathChanged, this);
if (oldValue.model) {
oldValue.model.stateChanged.disconnect(this.onModelStateChanged, this);
}
}
if (!newValue) {
this._onKernelChanged(null, null);
return;
}
let context = newValue;
context.kernelChanged.connect(this._onKernelChanged, this);
let oldKernel = oldValue ? oldValue.kernel : null;
if (context.kernel !== oldKernel) {
this._onKernelChanged(this._context, this._context.kernel);
}
this.notebook.model = newValue.model;
this._handleDirtyState();
newValue.model.stateChanged.connect(this.onModelStateChanged, this);
// Clear the cells when the context is initially populated.
if (!newValue.isReady) {
newValue.ready.then(() => {
if (this.isDisposed) {
return;
}
let model = newValue.model;
// Clear the undo state of the cells.
if (model) {
model.cells.clearUndo();
each(this.notebook.widgets, widget => {
widget.editor.clearHistory();
});
}
});
}
// Handle the document title.
this.onPathChanged(context, context.path);
context.pathChanged.connect(this.onPathChanged, this);
}
/**
* Handle a change in the kernel by updating the document metadata.
*/
private _onKernelChanged(context: DocumentRegistry.IContext<INotebookModel>, kernel: Kernel.IKernel): void {
this._kernelChanged.emit(kernel);
if (!this.model || !kernel) {
return;
}
kernel.ready.then(() => {
if (this.model) {
this._updateLanguage(kernel.info.language_info);
}
});
this._updateSpec(kernel);
}
/**
* Update the kernel language.
*/
private _updateLanguage(language: KernelMessage.ILanguageInfo): void {
this.model.metadata.set('language_info', language);
}
/**
* Update the kernel spec.
*/
private _updateSpec(kernel: Kernel.IKernel): void {
kernel.getSpec().then(spec => {
if (this.isDisposed) {
return;
}
this.model.metadata.set('kernelspec', {
name: kernel.name,
display_name: spec.display_name,
language: spec.language
});
});
}
/**
* Handle the dirty state of the model.
*/
private _handleDirtyState(): void {
if (!this.model) {
return;
}
if (this.model.dirty) {
this.title.className += ` ${DIRTY_CLASS}`;
} else {
this.title.className = this.title.className.replace(DIRTY_CLASS, '');
}
}
private _context: DocumentRegistry.IContext<INotebookModel> = null;
private _activated = new Signal<this, void>(this);
private _contextChanged = new Signal<this, void>(this);
private _kernelChanged = new Signal<this, Kernel.IKernel>(this);
}
/**
* A namespace for `NotebookPanel` statics.
*/
export namespace NotebookPanel {
/**
* An options interface for NotebookPanels.
*/
export
interface IOptions {
/**
* The rendermime instance used by the panel.
*/
rendermime: RenderMime;
/**
* The language preference for the model.
*/
languagePreference?: string;
/**
* The content factory for the panel.
*/
contentFactory: IContentFactory;
/**
* The mimeType service.
*/
mimeTypeService: IEditorMimeTypeService;
}
/**
* A content factory interface for NotebookPanel.
*/
export
interface IContentFactory {
/**
* The editor factory.
*/
readonly editorFactory: CodeEditor.Factory;
/**
* The factory for notebook cell widget content.
*/
readonly notebookContentFactory: Notebook.IContentFactory;
/**
* Create a new content area for the panel.
*/
createNotebook(options: Notebook.IOptions): Notebook;
/**
* Create a new toolbar for the panel.
*/
createToolbar(): Toolbar<Widget>;
}
/**
* The default implementation of an `IContentFactory`.
*/
export
class ContentFactory implements IContentFactory {
/**
* Creates a new renderer.
*/
constructor(options: ContentFactory.IOptions) {
this.editorFactory = options.editorFactory;
this.notebookContentFactory = (options.notebookContentFactory ||
new Notebook.ContentFactory({
editorFactory: this.editorFactory,
outputAreaContentFactory: options.outputAreaContentFactory,
codeCellContentFactory: options.codeCellContentFactory,
rawCellContentFactory: options.rawCellContentFactory,
markdownCellContentFactory: options.markdownCellContentFactory
})
);
}
/**
* The editor factory.
*/
readonly editorFactory: CodeEditor.Factory;
/**
* The factory for notebook cell widget content.
*/
readonly notebookContentFactory: Notebook.IContentFactory;
/**
* Create a new content area for the panel.
*/
createNotebook(options: Notebook.IOptions): Notebook {
return new Notebook(options);
}
/**
* Create a new toolbar for the panel.
*/
createToolbar(): Toolbar<Widget> {
return new Toolbar();
}
}
/**
* The namespace for `ContentFactory`.
*/
export
namespace ContentFactory {
/**
* An initialization options for a notebook panel content factory.
*/
export
interface IOptions {
/**
* The editor factory.
*/
editorFactory: CodeEditor.Factory;
/**
* The factory for output area content.
*/
outputAreaContentFactory?: OutputAreaWidget.IContentFactory;
/**
* The factory for code cell widget content. If given, this will
* take precedence over the `outputAreaContentFactory`.
*/
codeCellContentFactory?: CodeCellWidget.IContentFactory;
/**
* The factory for raw cell widget content.
*/
rawCellContentFactory?: BaseCellWidget.IContentFactory;
/**
* The factory for markdown cell widget content.
*/
markdownCellContentFactory?: BaseCellWidget.IContentFactory;
/**
* The factory for notebook cell widget content. If given, this will
* take precedence over the the cell and output area factories.
*/
notebookContentFactory?: Notebook.IContentFactory;
}
}
/* tslint:disable */
/**
* The notebook renderer token.
*/
export
const IContentFactory = new Token<IContentFactory>('jupyter.services.notebook.content-factory');
/* tslint:enable */
}