This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
notebooks.ts
256 lines (233 loc) · 7.13 KB
/
notebooks.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
import {
JupyterFrontEnd,
JupyterFrontEndPlugin
} from "@jupyterlab/application";
import { ICellModel, isCodeCellModel } from "@jupyterlab/cells";
import {
DataTypeNoArgs,
DataTypeStringArg,
nestedDataType,
Registry,
resolveDataType,
TypedConverter,
createConverter
} from "@jupyterlab/dataregistry";
import { IOutputModel } from "@jupyterlab/rendermime";
import { ReadonlyJSONObject, ReadonlyJSONValue } from "@phosphor/coreutils";
import { defer, Observable, of } from "rxjs";
import { map, switchMap } from "rxjs/operators";
import {
observableListToObservable,
outputAreaModelToObservable
} from "./observables";
import { IRegistry } from "./registry";
import { notebookContextDataType } from "./documents";
/**
* This defines a nested data type for notebooks, so that a notebook
* `file:///notebook.ipynb` will have children `file:///notebook.ipynb#/cells/0/outputs/0`,etc
*/
// 'file:///{path}.ipynb/#/cells/{cellid}/outputs/{outputid}/data/{mimetype}'
const notebookCellsDataType = new DataTypeNoArgs<Observable<Array<ICellModel>>>(
"application/x.jupyterlab.notebook-cells"
);
/**
* Convert from notebook context to a list of cells within the model
*/
const notebookContextToCells = createConverter(
{ from: notebookContextDataType, to: notebookCellsDataType },
({ data }) =>
data.pipe(
switchMap(context => observableListToObservable(context.model.cells))
)
);
const notebookCellsToNested = createConverter(
{ from: notebookCellsDataType, to: nestedDataType },
({ url, data }) =>
data.pipe(
map(
cells =>
new Set(
cells.map((_, i) => {
url = new URL(url.toString());
url.hash = `/cells/${i}`;
return url.toString();
})
)
)
)
);
const cellModelDataType = new DataTypeNoArgs<Observable<ICellModel>>(
"application/x.jupyterlab.cell-model"
);
/**
* Converts from a URL like `file:///notebook.ipynb#/cells/0` to the cell model.
*/
function createResolveCellModelConverter(
registry: Registry
): TypedConverter<typeof resolveDataType, typeof cellModelDataType> {
return createConverter(
{ from: resolveDataType, to: cellModelDataType },
({ url }) => {
const result = url.hash.match(/^[#][/]cells[/](\d+)$/);
if (
url.protocol !== "file:" ||
!url.pathname.endsWith(".ipynb") ||
!result
) {
return null;
}
const cellID = Number(result[1]);
// Create the original notebook URL and get the cells from it
url.hash = "";
const notebookURL = url.toString();
return defer(() =>
notebookCellsDataType
.getDataset(registry.getURL(notebookURL))!
.pipe(map(cells => cells[cellID]))
);
}
);
}
const outputsDataType = new DataTypeNoArgs<Observable<Array<IOutputModel>>>(
"application/x.jupyterlab.outputs"
);
/**
* Convert from a cell model to a list of output models. If the cell is not a code cell, the list is empty.
*/
const cellToOutputs = createConverter(
{ from: cellModelDataType, to: outputsDataType },
({ data }) =>
data.pipe(
switchMap(cellModel => {
if (isCodeCellModel(cellModel)) {
return outputAreaModelToObservable(cellModel.outputs);
}
return of([]);
})
)
);
/**
* Converts from a list of outputs to the resulting URLs
*/
const outputsToNested = createConverter(
{ from: outputsDataType, to: nestedDataType },
({ url, data }) =>
data.pipe(
map(outputs => new Set(outputs.map((_, i) => `${url}/outputs/${i}`)))
)
);
// The data in the mimebundle of an output cell
const mimeBundleDataType = new DataTypeNoArgs<Observable<ReadonlyJSONObject>>(
"application/x.jupyterlab.mime-bundle"
);
/**
* Register a conversion from a URL like `file:///notebook.ipynb#/cells/0/output/0` to
* the mimebundle for the output.
*/
function createOutputConverter(
registry: Registry
): TypedConverter<typeof resolveDataType, typeof mimeBundleDataType> {
return createConverter(
{ from: resolveDataType, to: mimeBundleDataType },
({ url }) => {
const result = url.hash.match(/^[#]([/]cells[/]\d+)[/]outputs[/](\d+)$/);
if (
url.protocol !== "file:" ||
!url.pathname.endsWith(".ipynb") ||
!result
) {
return null;
}
const cellHash = result[1];
const outputID = Number(result[2]);
// Create the original output URL and get the cells from it
url.hash = cellHash;
const cellURL = url.toString();
return defer(() =>
outputsDataType
.getDataset(registry.getURL(cellURL))!
.pipe(map(outputs => outputs[outputID].data))
);
}
);
}
// The data for a certain mimetype in an output.
const mimeDataDataType = new DataTypeStringArg<Observable<ReadonlyJSONValue>>(
"application/x.jupyterlab.mimedata",
"mimeType"
);
/**
* Converts from a URL with a mimebundle, which is the output, to one with the mimeType
* added.
*/
const mimeBundleNested = createConverter(
{ from: mimeBundleDataType, to: nestedDataType },
({ url, data }) =>
data.pipe(
map(
mimeData =>
new Set(
Object.keys(mimeData).map(mimeType => `${url}/data/${mimeType}`)
)
)
)
);
/**
* Create a conversion a mimebundle data `file:///notebook.ipynb#/cells/0/output/0/data/text/csv` to
* that mimedata.
*/
function createMimeDataConverter(
registry: Registry
): TypedConverter<typeof resolveDataType, typeof mimeDataDataType> {
return createConverter(
{ from: resolveDataType, to: mimeDataDataType },
({ url }) => {
const result = decodeURIComponent(url.hash).match(
/^[#]([/]cells[/]\d+[/]outputs[/]\d+)[/]data[/](.*)$/
);
if (
url.protocol !== "file:" ||
!url.pathname.endsWith(".ipynb") ||
!result
) {
return null;
}
const [, outputHash, type] = result;
// Create the original output URL and get the cells from it
url.hash = outputHash;
const outputURL = url.toString();
const data = defer(() =>
mimeBundleDataType
.getDataset(registry.getURL(outputURL))!
.pipe(map(mimeBundle => mimeBundle[type]))
);
return { type, data };
}
);
}
/**
* Create the right mime type for for mime bundle data.
*/
const mimeBundleDataConverter = createConverter<
Observable<ReadonlyJSONValue>,
Observable<ReadonlyJSONValue>,
string,
string
>({ from: mimeDataDataType }, ({ type, data }) => ({ type, data }));
function activate(app: JupyterFrontEnd, registry: Registry) {
registry.addConverter(notebookContextToCells);
registry.addConverter(notebookCellsToNested);
registry.addConverter(createResolveCellModelConverter(registry));
registry.addConverter(cellToOutputs);
registry.addConverter(outputsToNested);
registry.addConverter(createOutputConverter(registry));
registry.addConverter(mimeBundleNested);
registry.addConverter(createMimeDataConverter(registry));
registry.addConverter(mimeBundleDataConverter);
}
export default {
id: "@jupyterlab/dataregistry-extension:notebooks",
requires: [IRegistry],
activate,
autoStart: true
} as JupyterFrontEndPlugin<void>;