-
-
Notifications
You must be signed in to change notification settings - Fork 551
/
ipywidgets.ts
90 lines (88 loc) · 2.85 KB
/
ipywidgets.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
import { ofMessageType, JupyterMessage } from "@nteract/messaging";
import { commOpenAction, appendOutput } from "@nteract/actions";
import * as selectors from "@nteract/selectors";
import {
ContentRef,
RemoteKernelProps,
LocalKernelProps,
DocumentRecordProps,
EmptyModelRecordProps,
FileModelRecordProps,
DirectoryModelRecordProps
} from "@nteract/types";
import { of } from "rxjs";
import { filter, switchMap } from "rxjs/operators";
import { RecordOf } from "immutable";
/**
* Listen for comm_open messages from the kernel that are associated
* with models that will not be rendered on the page.
*
* Note: this is not an ideal solution but we need to do this
* so that we can keep the WidgetManager contextualized to the
* WidgetDisplay as opposed to at the top-level.
*/
export const ipywidgetsModel$ = (
kernel: LocalKernelProps | RemoteKernelProps,
model:
| RecordOf<DocumentRecordProps>
| RecordOf<EmptyModelRecordProps>
| RecordOf<FileModelRecordProps>
| RecordOf<DirectoryModelRecordProps>
| null,
contentRef: ContentRef
) =>
kernel.channels.pipe(
ofMessageType("comm_open"),
filter((msg: JupyterMessage) => {
if (
msg.content.data &&
msg.content.data.state &&
msg.content.data.state._model_name === "LinkModel"
) {
return true;
}
return false;
}),
switchMap((msg: JupyterMessage) => {
return of(
commOpenAction(msg),
/**
* If the content we are running under is a notebook,
* then append a mock output for the linkModel to the
* notebook.
*/
model && model.type === "notebook"
? appendOutput({
/**
* Append the output to the currently focused cell.
*
* Ideally, we would append the output to the cell
* that the output was generated in. However, we
* don't currently do any associated between the source
* of execution and the follow-on actions.
*/
id:
selectors.notebook.cellFocused(model as RecordOf<
DocumentRecordProps
>) ||
selectors.notebook
.cellOrder(model as RecordOf<DocumentRecordProps>)
.first(),
contentRef,
output: {
output_type: "display_data",
data: {
"application/vnd.jupyter.widget-view+json": {
model_id: msg.content.comm_id,
version_major: 2,
version_minor: 0
}
},
metadata: {},
transient: {}
}
})
: null
);
})
);