From 03201442e040b806111b9713fdfa53fc94e1c98b Mon Sep 17 00:00:00 2001 From: Jon Mease Date: Tue, 5 Mar 2019 20:17:05 -0500 Subject: [PATCH 1/2] Make sure div exists before calling resize/newPlot functions --- plotly/offline/offline.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/plotly/offline/offline.py b/plotly/offline/offline.py index 01c08acf103..7964e07bf3b 100644 --- a/plotly/offline/offline.py +++ b/plotly/offline/offline.py @@ -102,7 +102,9 @@ def _build_resize_script(plotdivid, plotly_root='Plotly'): resize_script = ( '' ).format(plotly_root=plotly_root, id=plotdivid) return resize_script @@ -356,12 +358,14 @@ def _plot_html(figure_or_data, config, validate, default_width, animate = '' script = ''' + if (document.getElementById("{id}")) {{ Plotly.plot( '{id}', {data}, {layout}, {config} ).then(function () {add_frames}){animate} + }} '''.format( id=plotdivid, data=jdata, @@ -373,7 +377,11 @@ def _plot_html(figure_or_data, config, validate, default_width, animate=animate ) else: - script = 'Plotly.newPlot("{id}", {data}, {layout}, {config})'.format( + script = """ +if (document.getElementById("{id}")) {{ + Plotly.newPlot("{id}", {data}, {layout}, {config}); +}} +""".format( id=plotdivid, data=jdata, layout=jlayout, From baca9c374c3454e30ba2424d2ba9ca78160a28b4 Mon Sep 17 00:00:00 2001 From: Jon Mease Date: Tue, 5 Mar 2019 20:18:57 -0500 Subject: [PATCH 2/2] Always undef and re-define plotly.js with init_notebook_mode. Checking !window._Plotly doesn't save any time and it prevents init_notebook_mode from reinitializing plotly.js in the notebook if things somehow got messed up --- plotly/offline/offline.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plotly/offline/offline.py b/plotly/offline/offline.py index 7964e07bf3b..1180a897c1d 100644 --- a/plotly/offline/offline.py +++ b/plotly/offline/offline.py @@ -291,14 +291,13 @@ def init_notebook_mode(connected=False): '{win_config}' '{mathjax_config}' '' '').format(script=get_plotlyjs(), win_config=_window_plotly_config,