1414
1515import plotly
1616from plotly import tools , utils
17+ from plotly .exceptions import PlotlyError
1718
1819try :
1920 import IPython
2829except ImportError :
2930 _matplotlib_imported = False
3031
32+ __PLOTLY_OFFLINE_INITIALIZED = False
3133
3234def download_plotlyjs (download_url ):
3335 warnings .warn ('''
@@ -50,16 +52,11 @@ def init_notebook_mode():
5052 yet. This is an idempotent method and can and should be called from any
5153 offline methods that require plotly.js to be loaded into the notebook dom.
5254 """
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-
6055 if not _ipython_imported :
6156 raise ImportError ('`iplot` can only run inside an IPython Notebook.' )
6257
58+ global __PLOTLY_OFFLINE_INITIALIZED
59+ # Inject plotly.js into the output cell
6360 script_inject = (
6461 ''
6562 '<script type=\' text/javascript\' >'
@@ -68,14 +65,14 @@ def init_notebook_mode():
6865 '{script}'
6966 '}});'
7067 'require([\' plotly\' ], function(Plotly) {{'
71- 'console.log(Plotly);'
7268 'window.Plotly = Plotly;'
7369 '}});'
7470 '}}'
7571 '</script>'
7672 '' ).format (script = get_plotlyjs ())
7773
7874 display (HTML (script_inject ))
75+ __PLOTLY_OFFLINE_INITIALIZED = True
7976
8077
8178def _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',
177174
178175 Example:
179176 ```
180- from plotly.offline import iplot
181-
177+ from plotly.offline import init_notebook_mode, iplot
178+ init_notebook_mode()
182179 iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}])
183180 ```
184181 """
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.' )
187193
188194 plot_html , plotdivid , width , height = _plot_html (
189195 figure_or_data , show_link , link_text , validate ,
@@ -415,19 +421,18 @@ def iplot_mpl(mpl_fig, resize=False, strip_style=False,
415421
416422 Example:
417423 ```
418- from plotly.offline import iplot_mpl
424+ from plotly.offline import init_notebook_mode, iplot_mpl
419425 import matplotlib.pyplot as plt
420426
421427 fig = plt.figure()
422428 x = [10, 15, 20, 25, 30]
423429 y = [100, 250, 200, 150, 300]
424430 plt.plot(x, y, "o")
425431
432+ init_notebook_mode()
426433 iplot_mpl(fig)
427434 ```
428435 """
429- init_notebook_mode ()
430-
431436 plotly_plot = tools .mpl_to_plotly (mpl_fig , resize , strip_style , verbose )
432437 return iplot (plotly_plot , show_link , link_text , validate )
433438
0 commit comments