-
Notifications
You must be signed in to change notification settings - Fork 22
/
outputWidgets.js
109 lines (94 loc) · 2.54 KB
/
outputWidgets.js
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
import * as outputBase from '@jupyter-widgets/output'
import { OutputAreaModel, OutputArea } from '@jupyterlab/outputarea'
import {
RenderMimeRegistry,
standardRendererFactories,
} from '@jupyterlab/rendermime'
import { Panel } from '@phosphor/widgets'
const RENDER_MIME = new RenderMimeRegistry({
initialFactories: standardRendererFactories,
})
export class OutputModel extends outputBase.OutputModel {
defaults() {
const defaults = super.defaults()
defaults['msg_id'] = ''
return defaults
}
initialize(attributes, options) {
super.initialize(attributes, options)
// The output area model is trusted since widgets are only rendered in
// trusted contexts.
this._outputs = new OutputAreaModel({ trusted: true })
this.listenTo(this, 'change:msg_id', this.reset_msg_id)
this.reset_msg_id()
}
reset_msg_id() {
if (this._msgHook) {
this._msgHook.dispose()
}
this._msgHook = null
let kernel = this.widget_manager.kernel
let msgId = this.get('msg_id')
if (msgId && kernel) {
this._msgHook = kernel.registerMessageHook(this.get('msg_id'), msg => {
this.add(msg)
return false
})
}
}
add(msg) {
let msgType = msg.header.msg_type
switch (msgType) {
case 'execute_result':
case 'display_data':
case 'stream':
case 'error':
let model = msg.content
model.output_type = msgType
this._outputs.add(model)
break
case 'clear_output':
this.clear_output(msg.content.wait)
break
default:
break
}
}
clear_output(wait = false) {
this._outputs.clear(wait)
}
get outputs() {
return this._outputs
}
}
export class OutputView extends outputBase.OutputView {
_createElement(tagName) {
this.pWidget = new Panel()
return this.pWidget.node
}
_setElement(el) {
if (this.el || el !== this.pWidget.node) {
// Boxes don't allow setting the element beyond the initial creation.
throw new Error('Cannot reset the DOM element.')
}
this.el = this.pWidget.node
}
/**
* Called when view is rendered.
*/
render() {
this._outputView = new OutputArea({
rendermime: RENDER_MIME,
contentFactory: OutputArea.defaultContentFactory,
model: this.model.outputs,
})
this.pWidget.insertWidget(0, this._outputView)
this.pWidget.addClass('jupyter-widgets')
this.pWidget.addClass('widget-output')
this.update() // Set defaults.
}
remove() {
this._outputView.dispose()
return super.remove()
}
}