-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
index.tsx
147 lines (125 loc) · 3.29 KB
/
index.tsx
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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
Widget
} from '@phosphor/widgets';
import {
IRenderMime
} from '@jupyterlab/rendermime-interfaces';
import '../style/index.css';
namespace Private {
declare function require(moduleName: string): string;
/**
* Is plotly.js being loaded?.
*/
export
let loadingPlotly = false;
/**
* Load plotly.js browser script.
*/
export
function loadPlotly(): void {
loadingPlotly = true;
const script = document.createElement('script');
script.type = 'text/javascript';
script.text = require('raw-loader!plotly.js/dist/plotly.min.js');
document.head.appendChild(script);
}
}
/**
* The CSS class to add to the Plotly Widget.
*/
const CSS_CLASS = 'jp-RenderedPlotly';
/**
* The CSS class for a Plotly icon.
*/
const CSS_ICON_CLASS = 'jp-MaterialIcon jp-PlotlyIcon';
/**
* The MIME type for Plotly.
* The version of this follows the major version of Plotly.
*/
export
const MIME_TYPE = 'application/vnd.plotly.v1+json';
interface PlotlySpec {
data: Plotly.Data,
layout: Plotly.Layout
}
export
class RenderedPlotly extends Widget implements IRenderMime.IRenderer {
/**
* Create a new widget for rendering Plotly.
*/
constructor(options: IRenderMime.IRendererOptions) {
super();
this.addClass(CSS_CLASS);
this._mimeType = options.mimeType;
if (!Private.loadingPlotly) Private.loadPlotly();
}
/**
* Render Plotly into this widget's node.
*/
renderModel(model: IRenderMime.IMimeModel): Promise<void> {
const { data, layout } = model.data[this._mimeType] as any|PlotlySpec;
// const metadata = model.metadata[this._mimeType] as any || {};
return Plotly.newPlot(this.node, data, layout)
.then(() => Plotly.Plots.resize(this.node))
// .then(plot =>
// Plotly.toImage(plot, {
// format: 'png',
// width: this._width,
// height: this._height
// }))
// .then(() => {
// const data = url.split(',')[1];
// });
}
/**
* A message handler invoked on a `'resize'` message.
*/
protected onResize(msg: Widget.ResizeMessage) {
Plotly.redraw(this.node)
.then(() => {
Plotly.Plots.resize(this.node);
});
// .then(plot =>
// Plotly.toImage(plot, {
// format: 'png',
// width: this._width,
// height: this._height
// }))
// .then(url => {
// const data = url.split(',')[1];
// });
}
private _mimeType: string;
}
/**
* A mime renderer factory for Plotly data.
*/
export
const rendererFactory: IRenderMime.IRendererFactory = {
safe: true,
mimeTypes: [MIME_TYPE],
createRenderer: options => new RenderedPlotly(options)
};
const extensions: IRenderMime.IExtension | IRenderMime.IExtension[] = [
{
id: '@jupyterlab/plotly-extension:factory',
rendererFactory,
rank: 0,
dataType: 'json',
fileTypes: [{
name: 'plotly',
mimeTypes: [MIME_TYPE],
extensions: ['.plotly', '.plotly.json'],
iconClass: CSS_ICON_CLASS
}],
documentWidgetFactoryOptions: {
name: 'Plotly',
primaryFileType: 'plotly',
fileTypes: ['plotly', 'json'],
defaultFor: ['plotly']
}
}
];
export default extensions;