-
-
Notifications
You must be signed in to change notification settings - Fork 552
/
Copy pathv3.ts
260 lines (232 loc) Β· 6.51 KB
/
v3.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
import {
fromJS as immutableFromJS,
List as ImmutableList,
Map as ImmutableMap,
RecordOf
} from "immutable";
import {
CellId,
createFrozenMediaBundle,
demultiline,
JSONObject,
MediaBundle,
MultiLineString
} from "./primitives";
import { makeNotebookRecord } from "./notebook";
import {
CodeCellParams,
ImmutableCell,
ImmutableCodeCell,
ImmutableMarkdownCell,
ImmutableRawCell,
makeCodeCell,
makeMarkdownCell,
makeRawCell,
MarkdownCellParams,
RawCellParams
} from "./cells";
import {
ImmutableOutput,
makeDisplayData,
makeErrorOutput,
makeExecuteResult,
makeStreamOutput
} from "./outputs";
import { appendCell, CellStructure } from "./structures";
import { MarkdownCell, RawCell } from "./v4";
const VALID_MIMETYPES = {
text: "text/plain",
latex: "text/latex",
png: "image/png",
jpeg: "image/jpeg",
svg: "image/svg+xml",
html: "text/html",
javascript: "application/x-javascript",
json: "application/javascript",
pdf: "application/pdf"
};
type MimeTypeKey = keyof typeof VALID_MIMETYPES;
type MimePayload = { [P in MimeTypeKey]?: MultiLineString };
interface MimeOutput<T extends string = string> extends MimePayload {
output_type: T;
prompt_number?: number;
metadata: object;
}
export interface ExecuteResult extends MimeOutput<"pyout"> {}
export interface DisplayData extends MimeOutput<"display_data"> {}
export interface ErrorOutput {
output_type: "error" | "pyerr";
ename: string;
evalue: string;
traceback: string[];
}
export interface StreamOutput {
output_type: "stream";
stream: string;
text: MultiLineString;
}
export type Output = ExecuteResult | DisplayData | StreamOutput | ErrorOutput;
export interface HeadingCell {
cell_type: "heading";
metadata: JSONObject;
source: MultiLineString;
level: number;
}
export interface CodeCell {
cell_type: "code";
language: string;
collapsed: boolean;
metadata: JSONObject;
input: MultiLineString;
prompt_number: number;
outputs: Output[];
}
export type Cell = RawCell | MarkdownCell | HeadingCell | CodeCell;
export interface Worksheet {
cells: Cell[];
metadata: object;
}
export interface NotebookV3 {
worksheets: Worksheet[];
metadata: object;
nbformat: 3;
nbformat_minor: number;
}
function createImmutableMarkdownCell(
cell: MarkdownCell
): ImmutableMarkdownCell {
return makeMarkdownCell({
cell_type: cell.cell_type,
source: demultiline(cell.source),
metadata: immutableFromJS(cell.metadata)
});
}
/**
* Handle the old v3 version of the media
*/
function createImmutableMediaBundle(output: MimeOutput): Readonly<MediaBundle> {
const mediaBundle: { [key: string]: MultiLineString | undefined } = {};
for (const key of Object.keys(output)) {
// v3 had non-media types for rich media
if (key in VALID_MIMETYPES) {
mediaBundle[VALID_MIMETYPES[key as MimeTypeKey]] =
output[key as keyof MimePayload];
}
}
return createFrozenMediaBundle(mediaBundle);
}
function createImmutableOutput(output: Output): ImmutableOutput {
switch (output.output_type) {
case "pyout":
return makeExecuteResult({
execution_count: output.prompt_number,
// Note strangeness with v4 API
data: createImmutableMediaBundle(output),
metadata: immutableFromJS(output.metadata)
});
case "display_data":
return makeDisplayData({
data: createImmutableMediaBundle(output),
metadata: immutableFromJS(output.metadata)
});
case "stream":
// Default to stdout in all cases unless it's stderr
const name = output.stream === "stderr" ? "stderr" : "stdout";
return makeStreamOutput({
name,
text: demultiline(output.text)
});
case "pyerr":
return makeErrorOutput({
ename: output.ename,
evalue: output.evalue,
traceback: ImmutableList(output.traceback)
});
default:
throw new TypeError(`Output type ${output.output_type} not recognized`);
}
}
function createImmutableCodeCell(cell: CodeCell): ImmutableCodeCell {
return makeCodeCell({
cell_type: cell.cell_type,
source: demultiline(cell.input),
outputs: ImmutableList(cell.outputs.map(createImmutableOutput)),
execution_count: cell.prompt_number,
metadata: immutableFromJS(cell.metadata)
});
}
function createImmutableRawCell(cell: RawCell): ImmutableRawCell {
return makeRawCell({
cell_type: cell.cell_type,
source: demultiline(cell.source),
metadata: immutableFromJS(cell.metadata)
});
}
function createImmutableHeadingCell(cell: HeadingCell): ImmutableMarkdownCell {
// v3 heading cells are just markdown cells in v4+
return makeMarkdownCell({
cell_type: "markdown",
source: Array.isArray(cell.source)
? demultiline(
cell.source.map(line =>
Array(cell.level)
.join("#")
.concat(" ")
.concat(line)
)
)
: cell.source,
metadata: immutableFromJS(cell.metadata)
});
}
function createImmutableCell(cell: Cell): any {
switch (cell.cell_type) {
case "markdown":
return createImmutableMarkdownCell(cell);
case "code":
return createImmutableCodeCell(cell);
case "raw":
return createImmutableRawCell(cell);
case "heading":
return createImmutableHeadingCell(cell);
default:
throw new TypeError(`Cell type ${(cell as any).cell_type} unknown`);
}
}
export function fromJS(notebook: NotebookV3): any {
if (!isNotebookV3(notebook)) {
notebook = notebook as any;
throw new TypeError(
`Notebook is not a valid v3 notebook. v3 notebooks must be of form 3.x
It lists nbformat v${notebook.nbformat}.${notebook.nbformat_minor}`
);
}
const starterCellStructure: CellStructure = {
cellOrder: ImmutableList<CellId>().asMutable(),
cellMap: ImmutableMap<CellId, ImmutableCell>().asMutable()
};
const cellStructure = ([] as CellStructure[]).concat.apply(
[],
notebook.worksheets.map(worksheet =>
worksheet.cells.reduce(
(cellStruct, cell) => appendCell(cellStruct, createImmutableCell(cell)),
starterCellStructure
)
)
)[0];
return makeNotebookRecord({
cellOrder: cellStructure.cellOrder.asImmutable(),
cellMap: cellStructure.cellMap.asImmutable(),
nbformat_minor: notebook.nbformat_minor,
nbformat: 4,
metadata: immutableFromJS(notebook.metadata)
});
}
export function isNotebookV3(value: any): value is NotebookV3 {
return (
value &&
typeof value === "object" &&
value.nbformat === 3 &&
value.nbformat_minor >= 0
);
}