15
15
import plotly
16
16
from plotly import tools , utils
17
17
18
- import IPython
19
- from IPython .display import HTML , display
18
+ try :
19
+ import IPython
20
+ from IPython .display import HTML , display
21
+ _ipython_imported = True
22
+ except ImportError :
23
+ _ipython_imported = False
20
24
21
- import matplotlib
25
+ try :
26
+ import matplotlib
27
+ _matplotlib_imported = True
28
+ except ImportError :
29
+ _matplotlib_imported = False
22
30
23
31
24
32
def download_plotlyjs (download_url ):
@@ -36,12 +44,22 @@ def get_plotlyjs():
36
44
return plotlyjs
37
45
38
46
39
- def load_plotlyjs ():
47
+ def init_notebook_mode ():
40
48
"""
41
49
Initialize plotly.js in the browser if it hasn't been loaded into the DOM
42
50
yet. This is an idempotent method and can and should be called from any
43
51
offline methods that require plotly.js to be loaded into the notebook dom.
44
52
"""
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
+ if not _ipython_imported :
61
+ raise ImportError ('`iplot` can only run inside an IPython Notebook.' )
62
+
45
63
script_inject = (
46
64
''
47
65
'<script type=\' text/javascript\' >'
@@ -60,22 +78,6 @@ def load_plotlyjs():
60
78
display (HTML (script_inject ))
61
79
62
80
63
- def init_notebook_mode ():
64
- """
65
- Initialize Plotly Offline mode in an IPython Notebook.
66
- Run this function at the start of an IPython notebook
67
- to load the necessary javascript files for creating
68
- Plotly graphs with plotly.offline.iplot.
69
- """
70
- warnings .warn ('''
71
- `init_notebook_mode` is deprecated and will be removed in the
72
- next release. Notebook mode is now automatically initialized when
73
- notebook methods are invoked, so it is no
74
- longer necessary to manually initialize.
75
- ''' , DeprecationWarning )
76
- pass
77
-
78
-
79
81
def _plot_html (figure_or_data , show_link , link_text ,
80
82
validate , default_width , default_height , global_requirejs ):
81
83
@@ -181,7 +183,7 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
181
183
```
182
184
"""
183
185
184
- load_plotlyjs ()
186
+ init_notebook_mode ()
185
187
186
188
plot_html , plotdivid , width , height = _plot_html (
187
189
figure_or_data , show_link , link_text , validate ,
@@ -424,7 +426,7 @@ def iplot_mpl(mpl_fig, resize=False, strip_style=False,
424
426
iplot_mpl(fig)
425
427
```
426
428
"""
427
- load_plotlyjs ()
429
+ init_notebook_mode ()
428
430
429
431
plotly_plot = tools .mpl_to_plotly (mpl_fig , resize , strip_style , verbose )
430
432
return iplot (plotly_plot , show_link , link_text , validate )
@@ -458,7 +460,7 @@ def enable_mpl_offline(resize=False, strip_style=False,
458
460
fig
459
461
```
460
462
"""
461
- load_plotlyjs ()
463
+ init_notebook_mode ()
462
464
463
465
ip = IPython .core .getipython .get_ipython ()
464
466
formatter = ip .display_formatter .formatters ['text/html' ]
0 commit comments