From 91e74fddd4c9059ee05bdfd9221d9fde9d455252 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikl=C3=B3s=20Tusz?= Date: Wed, 18 May 2016 16:21:28 -0400 Subject: [PATCH 1/6] Offline: Add `load_plotlyjs` method to ensure it is always available --- plotly/offline/offline.py | 46 +++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/plotly/offline/offline.py b/plotly/offline/offline.py index bd50e2115ba..831fb8febe5 100644 --- a/plotly/offline/offline.py +++ b/plotly/offline/offline.py @@ -48,6 +48,30 @@ def get_plotlyjs(): return plotlyjs +def load_plotlyjs(): + """ + Initialize plotly.js in the browser if it hasn't been loaded into the DOM + yet. This is an idempotent method and can and should be called from any + offline methods that require plotly.js to be loaded into the notebook dom. + """ + script_inject = ( + '' + '' + '').format(script=get_plotlyjs()) + + display(HTML(script_inject)) + + def init_notebook_mode(): """ Initialize Plotly Offline mode in an IPython Notebook. @@ -171,25 +195,13 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly', Example: ``` - from plotly.offline import init_notebook_mode, iplot - init_notebook_mode() + from plotly.offline import, iplot iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}]) ``` """ - 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 tools._ipython_imported: - raise ImportError('`iplot` can only run inside an IPython Notebook.') - from IPython.display import HTML, display + load_plotlyjs() plot_html, plotdivid, width, height = _plot_html( figure_or_data, show_link, link_text, validate, @@ -434,6 +446,8 @@ def iplot_mpl(mpl_fig, resize=False, strip_style=False, iplot_mpl(fig) ``` """ + load_plotlyjs() + plotly_plot = tools.mpl_to_plotly(mpl_fig, resize, strip_style, verbose) return iplot(plotly_plot, show_link, link_text, validate) @@ -467,8 +481,8 @@ def enable_mpl_offline(resize=False, strip_style=False, fig ``` """ - if not __PLOTLY_OFFLINE_INITIALIZED: - init_notebook_mode() + load_plotlyjs() + ip = IPython.core.getipython.get_ipython() formatter = ip.display_formatter.formatters['text/html'] formatter.for_type(matplotlib.figure.Figure, From 2de17c3c15bd20c1ea4e922130272eeb624f1983 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikl=C3=B3s=20Tusz?= Date: Wed, 18 May 2016 16:22:11 -0400 Subject: [PATCH 2/6] Offline: Cleanup unused code --- plotly/offline/offline.py | 47 ++++++++++----------------------------- 1 file changed, 12 insertions(+), 35 deletions(-) diff --git a/plotly/offline/offline.py b/plotly/offline/offline.py index 831fb8febe5..87ead4a158c 100644 --- a/plotly/offline/offline.py +++ b/plotly/offline/offline.py @@ -14,23 +14,11 @@ import plotly from plotly import tools, utils -from plotly.exceptions import PlotlyError +import IPython +from IPython.display import HTML, display -try: - import IPython - _ipython_imported = True -except ImportError: - _ipython_imported = False - -try: - import matplotlib - _matplotlib_imported = True -except ImportError: - _matplotlib_imported = False - - -__PLOTLY_OFFLINE_INITIALIZED = False +import matplotlib def download_plotlyjs(download_url): @@ -79,21 +67,13 @@ def init_notebook_mode(): to load the necessary javascript files for creating Plotly graphs with plotly.offline.iplot. """ - if not tools._ipython_imported: - raise ImportError('`iplot` can only run inside an IPython Notebook.') - from IPython.display import HTML, display - - global __PLOTLY_OFFLINE_INITIALIZED - if not __PLOTLY_OFFLINE_INITIALIZED: - display(HTML("")) - __PLOTLY_OFFLINE_INITIALIZED = True + warnings.warn(''' + `init_notebook_mode` is deprecated and will be removed in the + next release. Notebook mode is now automatically initialized when + notebook methods are invoked, so it is no + longer necessary to manually initialize. + ''', DeprecationWarning) + pass def _plot_html(figure_or_data, show_link, link_text, @@ -433,11 +413,9 @@ def iplot_mpl(mpl_fig, resize=False, strip_style=False, Example: ``` - from plotly.offline import init_notebook_mode, iplot_mpl + from plotly.offline import iplot_mpl import matplotlib.pyplot as plt - init_notebook_mode() - fig = plt.figure() x = [10, 15, 20, 25, 30] y = [100, 250, 200, 150, 300] @@ -468,10 +446,9 @@ def enable_mpl_offline(resize=False, strip_style=False, Example: ``` - from plotly.offline import init_notebook_mode, enable_mpl_offline + from plotly.offline import enable_mpl_offline import matplotlib.pyplot as plt - init_notebook_mode() enable_mpl_offline() fig = plt.figure() From 593e84ca675ff71687af07762fbb18042c88d258 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikl=C3=B3s=20Tusz?= Date: Wed, 18 May 2016 17:04:16 -0400 Subject: [PATCH 3/6] Offline: Add back try/except block for IPython --- plotly/offline/offline.py | 48 ++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/plotly/offline/offline.py b/plotly/offline/offline.py index 87ead4a158c..dbcab123ad0 100644 --- a/plotly/offline/offline.py +++ b/plotly/offline/offline.py @@ -15,10 +15,18 @@ import plotly from plotly import tools, utils -import IPython -from IPython.display import HTML, display +try: + import IPython + from IPython.display import HTML, display + _ipython_imported = True +except ImportError: + _ipython_imported = False -import matplotlib +try: + import matplotlib + _matplotlib_imported = True +except ImportError: + _matplotlib_imported = False def download_plotlyjs(download_url): @@ -36,12 +44,22 @@ def get_plotlyjs(): return plotlyjs -def load_plotlyjs(): +def init_notebook_mode(): """ Initialize plotly.js in the browser if it hasn't been loaded into the DOM yet. This is an idempotent method and can and should be called from any offline methods that require plotly.js to be loaded into the notebook dom. """ + warnings.warn(''' + `init_notebook_mode` is deprecated and will be removed in the + next release. Notebook mode is now automatically initialized when + notebook methods are invoked, so it is no + longer necessary to manually initialize. + ''', DeprecationWarning) + + if not _ipython_imported: + raise ImportError('`iplot` can only run inside an IPython Notebook.') + script_inject = ( '' '