@@ -7,8 +7,6 @@ import { Message } from '@lumino/messaging';
7
7
8
8
import { IRenderMime } from '@jupyterlab/rendermime-interfaces' ;
9
9
10
- import Plotly from 'plotly.js/dist/plotly.min' ;
11
-
12
10
import '../style/index.css' ;
13
11
14
12
/**
@@ -27,12 +25,12 @@ const CSS_ICON_CLASS = 'jp-MaterialIcon jp-PlotlyIcon';
27
25
*/
28
26
export const MIME_TYPE = 'application/vnd.plotly.v1+json' ;
29
27
30
- interface IPlotlySpec {
31
- data : Plotly . Data ;
32
- layout : Plotly . Layout ;
33
- frames ?: Plotly . Frame [ ] ;
28
+ interface PlotlyHTMLElement extends HTMLElement {
29
+ on ( event : 'plotly_webglcontextlost' , callback : ( ) => void ) : void ;
34
30
}
35
31
32
+ type Frame = { [ key : string ] : any } ;
33
+
36
34
export class RenderedPlotly extends Widget implements IRenderMime . IRenderer {
37
35
/**
38
36
* Create a new widget for rendering Plotly.
@@ -48,8 +46,11 @@ export class RenderedPlotly extends Widget implements IRenderMime.IRenderer {
48
46
this . node . appendChild ( this . _img_el ) ;
49
47
50
48
// Install image hover callback
51
- this . _img_el . addEventListener ( 'mouseenter' , event => {
52
- this . createGraph ( this . _model ) ;
49
+ import ( /* webpackChunkName: 'plotly'*/ 'plotly.js/dist/plotly' ) . then ( Plotly => {
50
+
51
+ this . _img_el . addEventListener ( 'mouseenter' , event => {
52
+ this . createGraph ( this . _model , Plotly ) ;
53
+ } )
53
54
} )
54
55
}
55
56
@@ -74,7 +75,9 @@ export class RenderedPlotly extends Widget implements IRenderMime.IRenderer {
74
75
return Promise . resolve ( ) ;
75
76
} else {
76
77
// Create a new graph
77
- return this . createGraph ( model ) ;
78
+ return import ( /* webpackChunkName: 'plotly'*/ 'plotly.js/dist/plotly' ) . then ( Plotly => {
79
+ this . createGraph ( model , Plotly ) ;
80
+ } )
78
81
}
79
82
}
80
83
@@ -122,10 +125,11 @@ export class RenderedPlotly extends Widget implements IRenderMime.IRenderer {
122
125
}
123
126
}
124
127
125
- private createGraph ( model : IRenderMime . IMimeModel ) {
128
+ private createGraph ( model : IRenderMime . IMimeModel , Plotly : any ) {
126
129
const { data, layout, frames, config } = model . data [ this . _mimeType ] as
127
130
| any
128
- | IPlotlySpec ;
131
+ | { data : Plotly . Data ; layout : Plotly . Layout ; frames ?: Frame [ ] ; }
132
+ | { data : Plotly . Data ; layout : Plotly . Layout ; frames ?: Plotly . Frame [ ] ; }
129
133
130
134
return Plotly . react ( this . node , data , layout , config ) . then ( plot => {
131
135
this . showGraph ( ) ;
@@ -153,7 +157,7 @@ export class RenderedPlotly extends Widget implements IRenderMime.IRenderer {
153
157
}
154
158
155
159
// Handle webgl context lost events
156
- ( < Plotly . PlotlyHTMLElement > ( this . node ) ) . on ( 'plotly_webglcontextlost' , ( ) => {
160
+ ( < PlotlyHTMLElement > ( this . node ) ) . on ( 'plotly_webglcontextlost' , ( ) => {
157
161
const png_data = < string > model . data [ 'image/png' ] ;
158
162
if ( png_data !== undefined && png_data !== null ) {
159
163
// We have PNG data, use it
@@ -183,9 +187,11 @@ export class RenderedPlotly extends Widget implements IRenderMime.IRenderer {
183
187
*/
184
188
protected onUpdateRequest ( msg : Message ) : void {
185
189
if ( this . isVisible && this . hasGraphElement ( ) ) {
186
- Plotly . redraw ( this . node ) . then ( ( ) => {
187
- Plotly . Plots . resize ( this . node ) ;
188
- } ) ;
190
+ import ( /* webpackChunkName: 'plotly'*/ 'plotly.js/dist/plotly' ) . then ( Plotly => {
191
+ Plotly . redraw ( this . node ) . then ( ( ) => {
192
+ Plotly . Plots . resize ( this . node ) ;
193
+ } ) ;
194
+ } )
189
195
}
190
196
}
191
197
@@ -226,4 +232,4 @@ const extensions: IRenderMime.IExtension | IRenderMime.IExtension[] = [
226
232
}
227
233
] ;
228
234
229
- export default extensions ;
235
+ export default extensions ;
0 commit comments