1515from requests .compat import json as _json
1616
1717import plotly
18- from plotly import tools , utils
18+ from plotly import optional_imports , tools , utils
1919from plotly .exceptions import PlotlyError
2020
21- try :
22- import IPython
23- from IPython .display import HTML , display
24- _ipython_imported = True
25- except ImportError :
26- _ipython_imported = False
27-
28- try :
29- import matplotlib
30- _matplotlib_imported = True
31- except ImportError :
32- _matplotlib_imported = False
21+ ipython = optional_imports .get_module ('IPython' )
22+ ipython_display = optional_imports .get_module ('IPython.display' )
23+ matplotlib = optional_imports .get_module ('matplotlib' )
3324
3425__PLOTLY_OFFLINE_INITIALIZED = False
3526
@@ -112,7 +103,7 @@ def init_notebook_mode(connected=False):
112103 your notebook, resulting in much larger notebook sizes compared to the case
113104 where `connected=True`.
114105 """
115- if not _ipython_imported :
106+ if not ipython :
116107 raise ImportError ('`iplot` can only run inside an IPython Notebook.' )
117108
118109 global __PLOTLY_OFFLINE_INITIALIZED
@@ -149,7 +140,7 @@ def init_notebook_mode(connected=False):
149140 '</script>'
150141 '' ).format (script = get_plotlyjs ())
151142
152- display (HTML (script_inject ))
143+ ipython_display . display (ipython_display . HTML (script_inject ))
153144 __PLOTLY_OFFLINE_INITIALIZED = True
154145
155146
@@ -333,7 +324,7 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
333324 'plotly.offline.init_notebook_mode() '
334325 '# run at the start of every ipython notebook' ,
335326 ]))
336- if not tools . _ipython_imported :
327+ if not ipython :
337328 raise ImportError ('`iplot` can only run inside an IPython Notebook.' )
338329
339330 config = {}
@@ -344,7 +335,7 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
344335 figure_or_data , config , validate , '100%' , 525 , True
345336 )
346337
347- display (HTML (plot_html ))
338+ ipython_display . display (ipython_display . HTML (plot_html ))
348339
349340 if image :
350341 if image not in __IMAGE_FORMATS :
@@ -360,7 +351,7 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
360351 # allow time for the plot to draw
361352 time .sleep (1 )
362353 # inject code to download an image of the plot
363- display (HTML (script ))
354+ ipython_display . display (ipython_display . HTML (script ))
364355
365356
366357def plot (figure_or_data , show_link = True , link_text = 'Export to plot.ly' ,
@@ -690,7 +681,7 @@ def enable_mpl_offline(resize=False, strip_style=False,
690681 """
691682 init_notebook_mode ()
692683
693- ip = IPython .core .getipython .get_ipython ()
684+ ip = ipython .core .getipython .get_ipython ()
694685 formatter = ip .display_formatter .formatters ['text/html' ]
695686 formatter .for_type (matplotlib .figure .Figure ,
696687 lambda fig : iplot_mpl (fig , resize , strip_style , verbose ,
0 commit comments