-
Notifications
You must be signed in to change notification settings - Fork 9
/
widget.ts
88 lines (69 loc) · 2.23 KB
/
widget.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
import {HTMLBox, HTMLBoxView} from "@bokehjs/models/layouts/html_box"
import {Document} from "@bokehjs/document"
import {MessageSentEvent} from "@bokehjs/document/events"
import * as p from "@bokehjs/core/properties"
import {isString} from "@bokehjs/core/util/types"
import {require_loader} from "./loader"
import {WidgetManager, ModelBundle} from "./manager"
const widget_managers: WeakMap<Document, WidgetManager> = new WeakMap()
export class IPyWidgetView extends HTMLBoxView {
model: IPyWidget
private rendered: boolean = false
render(): void {
super.render()
if (!this.rendered) {
this._render().then(() => {
this.rendered = true
this.invalidate_layout()
this.notify_finished()
})
}
}
has_finished(): boolean {
return this.rendered && super.has_finished()
}
async _render(): Promise<void> {
const manager = widget_managers.get(this.model.document!)!
await manager.render(this.model.bundle, this.el)
}
}
export namespace IPyWidget {
export type Attrs = p.AttrsOf<Props>
export type Props = HTMLBox.Props & {
bundle: p.Property<ModelBundle>
}
}
export interface IPyWidget extends IPyWidget.Attrs {}
export class IPyWidget extends HTMLBox {
properties: IPyWidget.Props
constructor(attrs?: Partial<IPyWidget.Attrs>) {
super(attrs)
}
static __name__ = "IPyWidget"
static __module__ = "ipywidgets_bokeh.widget"
static init_IPyWidget(): void {
this.prototype.default_view = IPyWidgetView
this.define<IPyWidget.Props>({
bundle: [ p.Any ],
})
}
protected _doc_attached(): void {
const doc = this.document!
if (!widget_managers.has(doc)) {
const manager = new WidgetManager({loader: require_loader})
widget_managers.set(doc, manager)
manager.bk_open((data: string | ArrayBuffer): void => {
const event = new MessageSentEvent(doc, "ipywidgets_bokeh", data)
doc._trigger_on_change(event)
})
doc.on_message("ipywidgets_bokeh", (data: unknown) => {
if (isString(data) || data instanceof ArrayBuffer) {
manager.bk_recv(data)
} else {
console.error(`expected a string or ArrayBuffer, got ${typeof data}`)
}
})
}
}
}
IPyWidget.init_IPyWidget()