This repository has been archived by the owner on Jun 21, 2021. It is now read-only.
/
utils.ts
253 lines (214 loc) · 5.61 KB
/
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
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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
// tslint:disable-next-line
/// <reference path="./typings/json-to-html/json-to-html.d.ts"/>
import json2html = require('json-to-html');
import { simulate } from 'simulate-event';
import { ServiceManager } from '@jupyterlab/services';
import { ClientSession } from '@jupyterlab/apputils';
import { nbformat } from '@jupyterlab/coreutils';
import { UUID } from '@phosphor/coreutils';
import {
TextModelFactory,
DocumentRegistry,
Context
} from '@jupyterlab/docregistry';
import { INotebookModel, NotebookModelFactory } from '@jupyterlab/notebook';
import {
IRenderMime,
RenderMimeRegistry,
RenderedHTML,
standardRendererFactories
} from '@jupyterlab/rendermime';
/**
* Return a promise that resolves in the given milliseconds with the given value.
*/
export function sleep<T>(milliseconds: number = 0, value?: T): Promise<T> {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(value);
}, milliseconds);
});
}
/**
* Get a copy of the default rendermime instance.
*/
export function defaultRenderMime(): RenderMimeRegistry {
return Private.rendermime.clone();
}
/**
* Create a client session object.
*/
export async function createClientSession(
options: Partial<ClientSession.IOptions> = {}
): Promise<ClientSession> {
const manager = options.manager || Private.manager.sessions;
await manager.ready;
return new ClientSession({
manager,
path: options.path || UUID.uuid4(),
name: options.name,
type: options.type,
kernelPreference: options.kernelPreference || {
shouldStart: true,
canStart: true,
name: manager.specs.default
}
});
}
/**
* Create a context for a file.
*/
export function createFileContext(
path?: string,
manager?: ServiceManager.IManager
): Context<DocumentRegistry.IModel> {
const factory = Private.textFactory;
manager = manager || Private.manager;
path = path || UUID.uuid4() + '.txt';
return new Context({ manager, factory, path });
}
/**
* Create a context for a notebook.
*/
export async function createNotebookContext(
path?: string,
manager?: ServiceManager.IManager
): Promise<Context<INotebookModel>> {
const factory = Private.notebookFactory;
manager = manager || Private.manager;
path = path || UUID.uuid4() + '.ipynb';
await manager.ready;
return new Context({
manager,
factory,
path,
kernelPreference: { name: manager.specs.default }
});
}
/**
* Wait for a dialog to be attached to an element.
*/
export function waitForDialog(
host?: HTMLElement,
timeout?: number
): Promise<void> {
return new Promise<void>((resolve, reject) => {
let counter = 0;
const interval = 25;
const limit = Math.floor((timeout || 250) / interval);
const seek = () => {
if (++counter === limit) {
reject(new Error('Dialog not found'));
return;
}
if ((host || document.body).getElementsByClassName('jp-Dialog')[0]) {
resolve(undefined);
return;
}
setTimeout(seek, interval);
};
seek();
});
}
/**
* Accept a dialog after it is attached by accepting the default button.
*/
export async function acceptDialog(
host?: HTMLElement,
timeout?: number
): Promise<void> {
host = host || document.body;
await waitForDialog(host, timeout);
const node = host.getElementsByClassName('jp-Dialog')[0];
if (node) {
simulate(node as HTMLElement, 'keydown', { keyCode: 13 });
}
}
/**
* Dismiss a dialog after it is attached.
*
* #### Notes
* This promise will always resolve successfully.
*/
export async function dismissDialog(
host?: HTMLElement,
timeout?: number
): Promise<void> {
host = host || document.body;
try {
await waitForDialog(host, timeout);
} catch (error) {
return; // Ignore calls to dismiss the dialog if there is no dialog.
}
const node = host.getElementsByClassName('jp-Dialog')[0];
if (node) {
simulate(node as HTMLElement, 'keydown', { keyCode: 27 });
}
}
/**
* A namespace for private data.
*/
namespace Private {
export const manager = new ServiceManager();
export const textFactory = new TextModelFactory();
export const notebookFactory = new NotebookModelFactory({});
class JSONRenderer extends RenderedHTML {
mimeType = 'text/html';
renderModel(model: IRenderMime.IMimeModel): Promise<void> {
let source = model.data['application/json'];
model.setData({ data: { 'text/html': json2html(source) } });
return super.renderModel(model);
}
}
const jsonRendererFactory = {
mimeTypes: ['application/json'],
safe: true,
createRenderer(
options: IRenderMime.IRendererOptions
): IRenderMime.IRenderer {
return new JSONRenderer(options);
}
};
export const rendermime = new RenderMimeRegistry({
initialFactories: standardRendererFactories
});
rendermime.addFactory(jsonRendererFactory, 10);
}
/**
* 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']
}
];