diff --git a/plotly/io/_base_renderers.py b/plotly/io/_base_renderers.py index 9d4ac1f563c..e4317dd4006 100644 --- a/plotly/io/_base_renderers.py +++ b/plotly/io/_base_renderers.py @@ -7,7 +7,6 @@ from plotly import optional_imports from plotly.io import to_json, to_image, write_image, write_html -from plotly.io._utils import plotly_cdn_url from plotly.offline.offline import _get_jconfig, get_plotlyjs from plotly.tools import return_figure_from_figure_or_data @@ -277,11 +276,9 @@ def activate(self): {win_config} {mathjax_config} - """.format( win_config=_window_plotly_config, mathjax_config=_mathjax_config, - plotly_cdn=plotly_cdn_url().rstrip(".js"), ) else: diff --git a/plotly/io/_html.py b/plotly/io/_html.py index 3e7b89c7d2f..e694fb90d32 100644 --- a/plotly/io/_html.py +++ b/plotly/io/_html.py @@ -260,7 +260,62 @@ def to_html( load_plotlyjs = """\ {win_config} + \ + \ """.format( win_config=_window_plotly_config, cdn_url=plotly_cdn_url(), diff --git a/tests/test_io/test_renderers.py b/tests/test_io/test_renderers.py index 43dd250d8b7..1e9d47982ed 100644 --- a/tests/test_io/test_renderers.py +++ b/tests/test_io/test_renderers.py @@ -147,6 +147,13 @@ def assert_offline(html): assert get_plotlyjs() in html +def assert_requirejs_workaround(html): + # nbviewer runs RequireJS; ensure we include the guard that prevents plotly.js + # from registering as an anonymous AMD module (which would leave `Plotly` + # undefined in the output). + assert "__PLOTLY_PY_REQUIREJS_BACKUP__" in html + + def test_colab_renderer_show(fig1): pio.renderers.default = "colab" @@ -164,6 +171,7 @@ def test_colab_renderer_show(fig1): html = mock_arg1["text/html"] assert_full_html(html) assert_html_renderer_connected(html) + assert_requirejs_workaround(html) # check kwargs mock_kwargs = mock_call_args[1] @@ -191,7 +199,8 @@ def test_notebook_connected_show(fig1, name, connected): # Check init display contents bundle_display_html = mock_arg1_html if connected: - assert_html_renderer_connected(bundle_display_html) + assert "window.PlotlyConfig" in bundle_display_html + assert get_plotlyjs() not in bundle_display_html else: assert_offline(bundle_display_html) @@ -206,6 +215,9 @@ def test_notebook_connected_show(fig1, name, connected): # Check html display contents bundle_html = mock_arg1["text/html"] assert_not_full_html(bundle_html) + if connected: + assert_html_renderer_connected(bundle_html) + assert_requirejs_workaround(bundle_html) # check kwargs mock_kwargs = mock_call_args[1] @@ -305,22 +317,60 @@ def test_repr_html(renderer): plotlyjs_content = get_plotlyjs() sri_hash = _generate_sri_hash(plotlyjs_content) + requirejs_workaround_pre = ( + '\n " + ) + + requirejs_workaround_post = ( + '" + ) + template = ( '