forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
212 lines (183 loc) · 5.35 KB
/
index.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
/*-----------------------------------------------------------------------------
| Copyright (c) Jupyter Development Team.
| Distributed under the terms of the Modified BSD License.
|----------------------------------------------------------------------------*/
// tslint:disable-next-line
/// <reference path="../../../node_modules/@types/webpack-env/index.d.ts"/>
import { JSONObject } from '@phosphor/coreutils';
import { Widget } from '@phosphor/widgets';
import { IRenderMime } from '@jupyterlab/rendermime-interfaces';
import * as VegaModuleType from 'vega-embed';
import '../style/index.css';
/**
* The CSS class to add to the Vega and Vega-Lite widget.
*/
const VEGA_COMMON_CLASS = 'jp-RenderedVegaCommon4';
/**
* The CSS class to add to the Vega.
*/
const VEGA_CLASS = 'jp-RenderedVega4';
/**
* The CSS class to add to the Vega-Lite.
*/
const VEGALITE_CLASS = 'jp-RenderedVegaLite2';
/**
* The MIME type for Vega.
*
* #### Notes
* The version of this follows the major version of Vega.
*/
export const VEGA_MIME_TYPE = 'application/vnd.vega.v4+json';
/**
* The MIME type for Vega-Lite.
*
* #### Notes
* The version of this follows the major version of Vega-Lite.
*/
export const VEGALITE_MIME_TYPE = 'application/vnd.vegalite.v2+json';
/**
* A widget for rendering Vega or Vega-Lite data, for usage with rendermime.
*/
export class RenderedVega extends Widget implements IRenderMime.IRenderer {
private _result: VegaModuleType.Result;
/**
* Create a new widget for rendering Vega/Vega-Lite.
*/
constructor(options: IRenderMime.IRendererOptions) {
super();
this._mimeType = options.mimeType;
this._resolver = options.resolver;
this.addClass(VEGA_COMMON_CLASS);
this.addClass(
this._mimeType === VEGA_MIME_TYPE ? VEGA_CLASS : VEGALITE_CLASS
);
}
/**
* Render Vega/Vega-Lite into this widget's node.
*/
async renderModel(model: IRenderMime.IMimeModel): Promise<void> {
const spec = model.data[this._mimeType] as JSONObject;
const metadata = model.metadata[this._mimeType] as {
embed_options?: VegaModuleType.EmbedOptions;
};
const embedOptions =
metadata && metadata.embed_options ? metadata.embed_options : {};
const mode: VegaModuleType.Mode =
this._mimeType === VEGA_MIME_TYPE ? 'vega' : 'vega-lite';
const vega =
Private.vega != null ? Private.vega : await Private.ensureVega();
const path = await this._resolver.resolveUrl('');
const baseURL = await this._resolver.getDownloadUrl(path);
const el = document.createElement('div');
// clear the output before attaching a chart
this.node.innerHTML = '';
this.node.appendChild(el);
this._result = await vega.default(el, spec, {
actions: true,
defaultStyle: true,
...embedOptions,
mode,
loader: {
baseURL,
http: { credentials: 'same-origin' }
}
});
if (model.data['image/png']) {
return;
}
// Add png representation of vega chart to output
const imageURL = await this._result.view.toImageURL('png');
model.setData({
data: { ...model.data, 'image/png': imageURL.split(',')[1] }
});
}
dispose(): void {
if (this._result) {
this._result.view.finalize();
}
super.dispose();
}
private _mimeType: string;
private _resolver: IRenderMime.IResolver;
}
/**
* A mime renderer factory for vega data.
*/
export const rendererFactory: IRenderMime.IRendererFactory = {
safe: true,
mimeTypes: [VEGA_MIME_TYPE, VEGALITE_MIME_TYPE],
createRenderer: options => new RenderedVega(options)
};
const extension: IRenderMime.IExtension = {
id: '@jupyterlab/vega-extension:factory',
rendererFactory,
rank: 50, // prefer over vega 2 extension
dataType: 'json',
documentWidgetFactoryOptions: [
{
name: 'Vega',
primaryFileType: 'vega4',
fileTypes: ['vega4', 'json'],
defaultFor: ['vega4']
},
{
name: 'Vega-Lite',
primaryFileType: 'vega-lite2',
fileTypes: ['vega-lite2', 'json'],
defaultFor: ['vega-lite2']
}
],
fileTypes: [
{
mimeTypes: [VEGA_MIME_TYPE],
name: 'vega4',
extensions: ['.vg', '.vg.json', '.vega'],
iconClass: 'jp-MaterialIcon jp-VegaIcon'
},
{
mimeTypes: [VEGALITE_MIME_TYPE],
name: 'vega-lite2',
extensions: ['.vl', '.vl.json', '.vegalite'],
iconClass: 'jp-MaterialIcon jp-VegaIcon'
}
]
};
export default extension;
/**
* A namespace for private module data.
*/
namespace Private {
/**
* A cached reference to the vega library.
*/
export let vega: typeof VegaModuleType;
/**
* A Promise for the initial load of vega.
*/
export let vegaReady: Promise<typeof VegaModuleType>;
/**
* Lazy-load and cache the vega-embed library
*/
export function ensureVega(): Promise<typeof VegaModuleType> {
if (vegaReady) {
return vegaReady;
}
vegaReady = new Promise((resolve, reject) => {
require.ensure(
['vega-embed'],
// see https://webpack.js.org/api/module-methods/#require-ensure
// this argument MUST be named `require` for the WebPack parser
require => {
vega = require('vega-embed') as typeof VegaModuleType;
resolve(vega);
},
(error: any) => {
console.error(error);
reject();
},
'vega'
);
});
return vegaReady;
}
}