forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
outputmodel.ts
357 lines (318 loc) · 8.27 KB
/
outputmodel.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
/*-----------------------------------------------------------------------------
| Copyright (c) Jupyter Development Team.
| Distributed under the terms of the Modified BSD License.
|----------------------------------------------------------------------------*/
import {
JSONExt, JSONObject, JSONValue, ReadonlyJSONObject
} from '@phosphor/coreutils';
import {
ISignal, Signal
} from '@phosphor/signaling';
import {
nbformat
} from '@jupyterlab/coreutils';
import {
IObservableJSON, ObservableJSON
} from '@jupyterlab/observables';
import {
IRenderMime
} from '@jupyterlab/rendermime-interfaces';
import {
MimeModel
} from './mimemodel';
/**
* The interface for an output model.
*/
export
interface IOutputModel extends IRenderMime.IMimeModel {
/**
* A signal emitted when the output model changes.
*/
readonly changed: ISignal<this, void>;
/**
* The output type.
*/
readonly type: string;
/**
* The execution count of the model.
*/
readonly executionCount: nbformat.ExecutionCount;
/**
* Whether the output is trusted.
*/
trusted: boolean;
/**
* Dispose of the resources used by the output model.
*/
dispose(): void;
/**
* Serialize the model to JSON.
*/
toJSON(): nbformat.IOutput;
}
/**
* The namespace for IOutputModel sub-interfaces.
*/
export
namespace IOutputModel {
/**
* The options used to create a notebook output model.
*/
export
interface IOptions {
/**
* The raw output value.
*/
value: nbformat.IOutput;
/**
* Whether the output is trusted. The default is false.
*/
trusted?: boolean;
}
}
/**
* The default implementation of a notebook output model.
*/
export
class OutputModel implements IOutputModel {
/**
* Construct a new output model.
*/
constructor(options: IOutputModel.IOptions) {
let { data, metadata, trusted } = Private.getBundleOptions(options);
this._data = new ObservableJSON({ values: data as JSONObject });
this._rawData = data;
this._metadata = new ObservableJSON({ values: metadata as JSONObject });
this._rawMetadata = metadata;
this.trusted = trusted;
// Make a copy of the data.
let value = options.value;
for (let key in value) {
// Ignore data and metadata that were stripped.
switch (key) {
case 'data':
case 'metadata':
break;
default:
this._raw[key] = Private.extract(value, key);
}
}
this.type = value.output_type;
if (nbformat.isExecuteResult(value)) {
this.executionCount = value.execution_count;
} else {
this.executionCount = null;
}
}
/**
* A signal emitted when the output model changes.
*/
get changed(): ISignal<this, void> {
return this._changed;
}
/**
* The output type.
*/
readonly type: string;
/**
* The execution count.
*/
readonly executionCount: nbformat.ExecutionCount;
/**
* Whether the model is trusted.
*/
readonly trusted: boolean;
/**
* Dispose of the resources used by the output model.
*/
dispose(): void {
this._data.dispose();
this._metadata.dispose();
Signal.clearData(this);
}
/**
* The data associated with the model.
*/
get data(): ReadonlyJSONObject {
return this._rawData;
}
/**
* The metadata associated with the model.
*/
get metadata(): ReadonlyJSONObject {
return this._rawMetadata;
}
/**
* Set the data associated with the model.
*
* #### Notes
* Depending on the implementation of the mime model,
* this call may or may not have deferred effects,
*/
setData(options: IRenderMime.IMimeModel.ISetDataOptions): void {
if (options.data) {
this._updateObservable(this._data, options.data);
this._rawData = options.data;
}
if (options.metadata) {
this._updateObservable(this._metadata, options.metadata);
this._rawMetadata = options.metadata;
}
this._changed.emit(void 0);
}
/**
* Serialize the model to JSON.
*/
toJSON(): nbformat.IOutput {
let output: JSONValue = {};
for (let key in this._raw) {
output[key] = Private.extract(this._raw, key);
}
switch (this.type) {
case 'display_data':
case 'execute_result':
case 'update_display_data':
output['data'] = this.data as JSONObject;
output['metadata'] = this.metadata as JSONObject;
break;
default:
break;
}
// Remove transient data.
delete output['transient'];
return output as nbformat.IOutput;
}
/**
* Update an observable JSON object using a readonly JSON object.
*/
private _updateObservable(observable: IObservableJSON, data: ReadonlyJSONObject) {
let oldKeys = observable.keys();
let newKeys = Object.keys(data);
// Handle removed keys.
for (let key of oldKeys) {
if (newKeys.indexOf(key) === -1) {
observable.delete(key);
}
}
// Handle changed data.
for (let key of newKeys) {
let oldValue = observable.get(key);
let newValue = data[key];
if (oldValue !== newValue) {
observable.set(key, newValue as JSONValue);
}
}
}
private _changed = new Signal<this, void>(this);
private _raw: JSONObject = {};
private _rawMetadata: ReadonlyJSONObject;
private _rawData: ReadonlyJSONObject;
private _data: IObservableJSON;
private _metadata: IObservableJSON;
}
/**
* The namespace for OutputModel statics.
*/
export
namespace OutputModel {
/**
* Get the data for an output.
*
* @params output - A kernel output message payload.
*
* @returns - The data for the payload.
*/
export
function getData(output: nbformat.IOutput): JSONObject {
return Private.getData(output);
}
/**
* Get the metadata from an output message.
*
* @params output - A kernel output message payload.
*
* @returns - The metadata for the payload.
*/
export
function getMetadata(output: nbformat.IOutput): JSONObject {
return Private.getMetadata(output);
}
}
/**
* The namespace for module private data.
*/
namespace Private {
/**
* Get the data from a notebook output.
*/
export
function getData(output: nbformat.IOutput): JSONObject {
let bundle: nbformat.IMimeBundle = {};
if (nbformat.isExecuteResult(output) || nbformat.isDisplayData(output) || nbformat.isDisplayUpdate(output)) {
bundle = (output as nbformat.IExecuteResult).data;
} else if (nbformat.isStream(output)) {
if (output.name === 'stderr') {
bundle['application/vnd.jupyter.stderr'] = output.text;
} else {
bundle['application/vnd.jupyter.stdout'] = output.text;
}
} else if (nbformat.isError(output)) {
let traceback = output.traceback.join('\n');
bundle['application/vnd.jupyter.stderr'] = (
traceback || `${output.ename}: ${output.evalue}`
);
}
return convertBundle(bundle);
}
/**
* Get the metadata from an output message.
*/
export
function getMetadata(output: nbformat.IOutput): JSONObject {
let value: JSONObject = Object.create(null);
if (nbformat.isExecuteResult(output) || nbformat.isDisplayData(output)) {
for (let key in output.metadata) {
value[key] = extract(output.metadata, key);
}
}
return value;
}
/**
* Get the bundle options given output model options.
*/
export
function getBundleOptions(options: IOutputModel.IOptions): MimeModel.IOptions {
let data = getData(options.value);
let metadata = getMetadata(options.value);
let trusted = !!options.trusted;
return { data, metadata, trusted };
}
/**
* Extract a value from a JSONObject.
*/
export
function extract(value: JSONObject, key: string): JSONValue {
let item = value[key];
if (JSONExt.isPrimitive(item)) {
return item;
}
return JSON.parse(JSON.stringify(item));
}
/**
* Convert a mime bundle to mime data.
*/
function convertBundle(bundle: nbformat.IMimeBundle): JSONObject {
let map: JSONObject = Object.create(null);
for (let mimeType in bundle) {
let item = bundle[mimeType];
// Convert multi-line strings to strings.
if (JSONExt.isArray(item)) {
item = (item as string[]).join('\n');
} else if (!JSONExt.isPrimitive(item)) {
item = JSON.parse(JSON.stringify(item));
}
map[mimeType] = item;
}
return map;
}
}