14
14
15
15
import plotly
16
16
from plotly import tools , utils
17
+ from plotly .exceptions import PlotlyError
17
18
18
19
try :
19
20
import IPython
28
29
except ImportError :
29
30
_matplotlib_imported = False
30
31
32
+ __PLOTLY_OFFLINE_INITIALIZED = False
31
33
32
34
def download_plotlyjs (download_url ):
33
35
warnings .warn ('''
@@ -50,16 +52,11 @@ def init_notebook_mode():
50
52
yet. This is an idempotent method and can and should be called from any
51
53
offline methods that require plotly.js to be loaded into the notebook dom.
52
54
"""
53
- warnings .warn ('''
54
- `init_notebook_mode` is deprecated and will be removed in the
55
- next release. Notebook mode is now automatically initialized when
56
- notebook methods are invoked, so it is no
57
- longer necessary to manually initialize.
58
- ''' , DeprecationWarning )
59
-
60
55
if not _ipython_imported :
61
56
raise ImportError ('`iplot` can only run inside an IPython Notebook.' )
62
57
58
+ global __PLOTLY_OFFLINE_INITIALIZED
59
+ # Inject plotly.js into the output cell
63
60
script_inject = (
64
61
''
65
62
'<script type=\' text/javascript\' >'
@@ -68,14 +65,14 @@ def init_notebook_mode():
68
65
'{script}'
69
66
'}});'
70
67
'require([\' plotly\' ], function(Plotly) {{'
71
- 'console.log(Plotly);'
72
68
'window.Plotly = Plotly;'
73
69
'}});'
74
70
'}}'
75
71
'</script>'
76
72
'' ).format (script = get_plotlyjs ())
77
73
78
74
display (HTML (script_inject ))
75
+ __PLOTLY_OFFLINE_INITIALIZED = True
79
76
80
77
81
78
def _plot_html (figure_or_data , show_link , link_text ,
@@ -177,13 +174,22 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
177
174
178
175
Example:
179
176
```
180
- from plotly.offline import iplot
181
-
177
+ from plotly.offline import init_notebook_mode, iplot
178
+ init_notebook_mode()
182
179
iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}])
183
180
```
184
181
"""
185
-
186
- init_notebook_mode ()
182
+ if not __PLOTLY_OFFLINE_INITIALIZED :
183
+ raise PlotlyError ('\n ' .join ([
184
+ 'Plotly Offline mode has not been initialized in this notebook. '
185
+ 'Run: ' ,
186
+ '' ,
187
+ 'import plotly' ,
188
+ 'plotly.offline.init_notebook_mode() '
189
+ '# run at the start of every ipython notebook' ,
190
+ ]))
191
+ if not tools ._ipython_imported :
192
+ raise ImportError ('`iplot` can only run inside an IPython Notebook.' )
187
193
188
194
plot_html , plotdivid , width , height = _plot_html (
189
195
figure_or_data , show_link , link_text , validate ,
@@ -415,19 +421,18 @@ def iplot_mpl(mpl_fig, resize=False, strip_style=False,
415
421
416
422
Example:
417
423
```
418
- from plotly.offline import iplot_mpl
424
+ from plotly.offline import init_notebook_mode, iplot_mpl
419
425
import matplotlib.pyplot as plt
420
426
421
427
fig = plt.figure()
422
428
x = [10, 15, 20, 25, 30]
423
429
y = [100, 250, 200, 150, 300]
424
430
plt.plot(x, y, "o")
425
431
432
+ init_notebook_mode()
426
433
iplot_mpl(fig)
427
434
```
428
435
"""
429
- init_notebook_mode ()
430
-
431
436
plotly_plot = tools .mpl_to_plotly (mpl_fig , resize , strip_style , verbose )
432
437
return iplot (plotly_plot , show_link , link_text , validate )
433
438
0 commit comments