forked from jupyterlab/jupyterlab
/
notebook-utils.ts
165 lines (139 loc) · 3.88 KB
/
notebook-utils.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { editorServices } from '@jupyterlab/codemirror';
import { CodeEditorWrapper } from '@jupyterlab/codeeditor';
import { Clipboard } from '@jupyterlab/apputils';
import { nbformat } from '@jupyterlab/coreutils';
import { Context } from '@jupyterlab/docregistry';
import {
INotebookModel,
NotebookPanel,
Notebook,
NotebookModel,
StaticNotebook
} from '@jupyterlab/notebook';
import { RenderMimeRegistry } from '@jupyterlab/rendermime';
import { Cell, CodeCellModel } from '@jupyterlab/cells';
import { defaultRenderMime as localRendermime } from './rendermime';
/**
* Stub for the require() function.
*/
declare var require: any;
/**
* The default notebook content.
*/
// tslint:disable-next-line
export namespace NBTestUtils {
/**
* The default outputs used for testing.
*/
export const DEFAULT_OUTPUTS: nbformat.IOutput[] = [
{
name: 'stdout',
output_type: 'stream',
text: ['hello world\n', '0\n', '1\n', '2\n']
},
{
name: 'stderr',
output_type: 'stream',
text: ['output to stderr\n']
},
{
name: 'stderr',
output_type: 'stream',
text: ['output to stderr2\n']
},
{
output_type: 'execute_result',
execution_count: 1,
data: { 'text/plain': 'foo' },
metadata: {}
},
{
output_type: 'display_data',
data: { 'text/plain': 'hello, world' },
metadata: {}
},
{
output_type: 'error',
ename: 'foo',
evalue: 'bar',
traceback: ['fizz', 'buzz']
}
];
export const DEFAULT_CONTENT: nbformat.INotebookContent = require('../default.json') as nbformat.INotebookContent;
export const defaultEditorConfig = { ...StaticNotebook.defaultEditorConfig };
export const editorFactory = editorServices.factoryService.newInlineEditor.bind(
editorServices.factoryService
);
export const mimeTypeService = editorServices.mimeTypeService;
/**
* Get a copy of the default rendermime instance.
*/
export function defaultRenderMime(): RenderMimeRegistry {
return localRendermime();
}
export const clipboard = Clipboard.getInstance();
/**
* Create a base cell content factory.
*/
export function createBaseCellFactory(): Cell.IContentFactory {
return new Cell.ContentFactory({ editorFactory });
}
/**
* Create a new code cell content factory.
*/
export function createCodeCellFactory(): Cell.IContentFactory {
return new Cell.ContentFactory({ editorFactory });
}
/**
* Create a cell editor widget.
*/
export function createCellEditor(model?: CodeCellModel): CodeEditorWrapper {
return new CodeEditorWrapper({
model: model || new CodeCellModel({}),
factory: editorFactory
});
}
/**
* Create a default notebook content factory.
*/
export function createNotebookFactory(): Notebook.IContentFactory {
return new Notebook.ContentFactory({ editorFactory });
}
/**
* Create a default notebook panel content factory.
*/
export function createNotebookPanelFactory(): NotebookPanel.IContentFactory {
return new NotebookPanel.ContentFactory({ editorFactory });
}
/**
* Create a notebook widget.
*/
export function createNotebook(): Notebook {
return new Notebook({
rendermime: defaultRenderMime(),
contentFactory: createNotebookFactory(),
mimeTypeService
});
}
/**
* Create a notebook panel widget.
*/
export function createNotebookPanel(
context: Context<INotebookModel>
): NotebookPanel {
return new NotebookPanel({
content: createNotebook(),
context
});
}
/**
* Populate a notebook with default content.
*/
export function populateNotebook(notebook: Notebook): void {
let model = new NotebookModel();
model.fromJSON(DEFAULT_CONTENT);
notebook.model = model;
}
}