@@ -48,6 +48,30 @@ def get_plotlyjs():
48
48
return plotlyjs
49
49
50
50
51
+ def load_plotlyjs ():
52
+ """
53
+ Initialize plotly.js in the browser if it hasn't been loaded into the DOM
54
+ yet. This is an idempotent method and can and should be called from any
55
+ offline methods that require plotly.js to be loaded into the notebook dom.
56
+ """
57
+ script_inject = (
58
+ ''
59
+ '<script type=\' text/javascript\' >'
60
+ 'if(!window.Plotly){{'
61
+ 'define(\' plotly\' , function(require, exports, module) {{'
62
+ '{script}'
63
+ '}});'
64
+ 'require([\' plotly\' ], function(Plotly) {{'
65
+ 'console.log(Plotly);'
66
+ 'window.Plotly = Plotly;'
67
+ '}});'
68
+ '}}'
69
+ '</script>'
70
+ '' ).format (script = get_plotlyjs ())
71
+
72
+ display (HTML (script_inject ))
73
+
74
+
51
75
def init_notebook_mode ():
52
76
"""
53
77
Initialize Plotly Offline mode in an IPython Notebook.
@@ -171,25 +195,13 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
171
195
172
196
Example:
173
197
```
174
- from plotly.offline import init_notebook_mode, iplot
175
- init_notebook_mode()
198
+ from plotly.offline import, iplot
176
199
177
200
iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}])
178
201
```
179
202
"""
180
- if not __PLOTLY_OFFLINE_INITIALIZED :
181
- raise PlotlyError ('\n ' .join ([
182
- 'Plotly Offline mode has not been initialized in this notebook. '
183
- 'Run: ' ,
184
- '' ,
185
- 'import plotly' ,
186
- 'plotly.offline.init_notebook_mode() '
187
- '# run at the start of every ipython notebook' ,
188
- ]))
189
- if not tools ._ipython_imported :
190
- raise ImportError ('`iplot` can only run inside an IPython Notebook.' )
191
203
192
- from IPython . display import HTML , display
204
+ load_plotlyjs ()
193
205
194
206
plot_html , plotdivid , width , height = _plot_html (
195
207
figure_or_data , show_link , link_text , validate ,
@@ -434,6 +446,8 @@ def iplot_mpl(mpl_fig, resize=False, strip_style=False,
434
446
iplot_mpl(fig)
435
447
```
436
448
"""
449
+ load_plotlyjs ()
450
+
437
451
plotly_plot = tools .mpl_to_plotly (mpl_fig , resize , strip_style , verbose )
438
452
return iplot (plotly_plot , show_link , link_text , validate )
439
453
@@ -467,8 +481,8 @@ def enable_mpl_offline(resize=False, strip_style=False,
467
481
fig
468
482
```
469
483
"""
470
- if not __PLOTLY_OFFLINE_INITIALIZED :
471
- init_notebook_mode ()
484
+ load_plotlyjs ()
485
+
472
486
ip = IPython .core .getipython .get_ipython ()
473
487
formatter = ip .display_formatter .formatters ['text/html' ]
474
488
formatter .for_type (matplotlib .figure .Figure ,
0 commit comments