Skip to content

Commit 593e84c

Browse files
committed
Offline: Add back try/except block for IPython
1 parent 2de17c3 commit 593e84c

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

plotly/offline/offline.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,18 @@
1515
import plotly
1616
from plotly import tools, utils
1717

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
2024

21-
import matplotlib
25+
try:
26+
import matplotlib
27+
_matplotlib_imported = True
28+
except ImportError:
29+
_matplotlib_imported = False
2230

2331

2432
def download_plotlyjs(download_url):
@@ -36,12 +44,22 @@ def get_plotlyjs():
3644
return plotlyjs
3745

3846

39-
def load_plotlyjs():
47+
def init_notebook_mode():
4048
"""
4149
Initialize plotly.js in the browser if it hasn't been loaded into the DOM
4250
yet. This is an idempotent method and can and should be called from any
4351
offline methods that require plotly.js to be loaded into the notebook dom.
4452
"""
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+
4563
script_inject = (
4664
''
4765
'<script type=\'text/javascript\'>'
@@ -60,22 +78,6 @@ def load_plotlyjs():
6078
display(HTML(script_inject))
6179

6280

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-
7981
def _plot_html(figure_or_data, show_link, link_text,
8082
validate, default_width, default_height, global_requirejs):
8183

@@ -181,7 +183,7 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
181183
```
182184
"""
183185

184-
load_plotlyjs()
186+
init_notebook_mode()
185187

186188
plot_html, plotdivid, width, height = _plot_html(
187189
figure_or_data, show_link, link_text, validate,
@@ -424,7 +426,7 @@ def iplot_mpl(mpl_fig, resize=False, strip_style=False,
424426
iplot_mpl(fig)
425427
```
426428
"""
427-
load_plotlyjs()
429+
init_notebook_mode()
428430

429431
plotly_plot = tools.mpl_to_plotly(mpl_fig, resize, strip_style, verbose)
430432
return iplot(plotly_plot, show_link, link_text, validate)
@@ -458,7 +460,7 @@ def enable_mpl_offline(resize=False, strip_style=False,
458460
fig
459461
```
460462
"""
461-
load_plotlyjs()
463+
init_notebook_mode()
462464

463465
ip = IPython.core.getipython.get_ipython()
464466
formatter = ip.display_formatter.formatters['text/html']

0 commit comments

Comments
 (0)