From a108b9f2b7c41c642ee25b0c8695467bbd463ce2 Mon Sep 17 00:00:00 2001 From: Lukas Geiger Date: Sat, 8 Jul 2017 22:27:08 +0200 Subject: [PATCH] Make init_notebook_mode() optional Since it isn't required for frontends with firstclass plotly support like nteract and Hydrogen --- plotly/offline/offline.py | 36 +++++++++---------- .../test_offline/test_offline.py | 10 +++--- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/plotly/offline/offline.py b/plotly/offline/offline.py index e8fa27b2020..6ab50476448 100644 --- a/plotly/offline/offline.py +++ b/plotly/offline/offline.py @@ -322,15 +322,6 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly', iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}], image='png') ``` """ - if not __PLOTLY_OFFLINE_INITIALIZED: - raise PlotlyError('\n'.join([ - 'Plotly Offline mode has not been initialized in this notebook. ' - 'Run: ', - '', - 'import plotly', - 'plotly.offline.init_notebook_mode() ' - '# run at the start of every ipython notebook', - ])) if not ipython: raise ImportError('`iplot` can only run inside an IPython Notebook.') @@ -338,10 +329,6 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly', config.setdefault('showLink', show_link) config.setdefault('linkText', link_text) - plot_html, plotdivid, width, height = _plot_html( - figure_or_data, config, validate, '100%', 525, True - ) - figure = tools.return_figure_from_figure_or_data(figure_or_data, validate) # Though it can add quite a bit to the display-bundle size, we include @@ -358,14 +345,27 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly', if frames: fig['frames'] = frames - display_bundle = { - 'application/vnd.plotly.v1+json': fig, - 'text/html': plot_html, - 'text/vnd.plotly.v1+html': plot_html - } + display_bundle = {'application/vnd.plotly.v1+json': fig} + + if __PLOTLY_OFFLINE_INITIALIZED: + plot_html, plotdivid, width, height = _plot_html( + figure_or_data, config, validate, '100%', 525, True + ) + display_bundle['text/html'] = plot_html + display_bundle['text/vnd.plotly.v1+html'] = plot_html + ipython_display.display(display_bundle, raw=True) if image: + if not __PLOTLY_OFFLINE_INITIALIZED: + raise PlotlyError('\n'.join([ + 'Plotly Offline mode has not been initialized in this notebook. ' + 'Run: ', + '', + 'import plotly', + 'plotly.offline.init_notebook_mode() ' + '# run at the start of every ipython notebook', + ])) if image not in __IMAGE_FORMATS: raise ValueError('The image parameter must be one of the following' ': {}'.format(__IMAGE_FORMATS) diff --git a/plotly/tests/test_optional/test_offline/test_offline.py b/plotly/tests/test_optional/test_offline/test_offline.py index 17dd1af2bdd..ebb2f538051 100644 --- a/plotly/tests/test_optional/test_offline/test_offline.py +++ b/plotly/tests/test_optional/test_offline/test_offline.py @@ -29,16 +29,19 @@ class PlotlyOfflineTestCase(TestCase): def setUp(self): pass - @raises(plotly.exceptions.PlotlyError) - def test_iplot_doesnt_work_before_you_call_init_notebook_mode(self): + def test_iplot_works_without_init_notebook_mode(self): plotly.offline.iplot([{}]) + @raises(plotly.exceptions.PlotlyError) + def test_iplot_doesnt_work_before_you_call_init_notebook_mode_when_requesting_download(self): + plotly.offline.iplot([{}], image='png') + def test_iplot_works_after_you_call_init_notebook_mode(self): plotly.offline.init_notebook_mode() plotly.offline.iplot([{}]) @attr('matplotlib') - def test_iplot_mpl_works_after_you_call_init_notebook_mode(self): + def test_iplot_mpl_works(self): # Generate matplotlib plot for tests fig = plt.figure() @@ -46,7 +49,6 @@ def test_iplot_mpl_works_after_you_call_init_notebook_mode(self): y = [100, 200, 300] plt.plot(x, y, "o") - plotly.offline.init_notebook_mode() plotly.offline.iplot_mpl(fig)