From c09fb77ad93b37c1119829cad2c7fc0ffeedb196 Mon Sep 17 00:00:00 2001 From: Matt Lilley Date: Thu, 12 Feb 2026 20:42:20 +0000 Subject: [PATCH 1/2] io: prevent RequireJS define() mismatch when using CDN plotly.js nbviewer injects RequireJS; when plotly.js is loaded via a plain CDN - """.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..f42eaf0584e 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 = ( '
\n " - ' ' - '
" + + '" crossorigin="anonymous"> ' + + requirejs_workaround_post + + " " + + '
" ) if "text/html" in bundle: str_bundle = bundle["text/html"] From 28319556ef58c2fb5bce2c87731a58bad9e16f13 Mon Sep 17 00:00:00 2001 From: Matt Lilley Date: Sun, 15 Feb 2026 21:50:14 +0000 Subject: [PATCH 2/2] Fixes CI formatting errors --- plotly/io/_base_renderers.py | 1 - tests/test_io/test_renderers.py | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/plotly/io/_base_renderers.py b/plotly/io/_base_renderers.py index d2b825ced7e..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 diff --git a/tests/test_io/test_renderers.py b/tests/test_io/test_renderers.py index f42eaf0584e..1e9d47982ed 100644 --- a/tests/test_io/test_renderers.py +++ b/tests/test_io/test_renderers.py @@ -327,13 +327,13 @@ def test_repr_html(renderer): "undefined, has_module: Object.prototype.hasOwnProperty.call(window, " '"module"), module: window.module, has_exports: ' 'Object.prototype.hasOwnProperty.call(window, "exports"), exports: ' - 'window.exports});/*nbviewer loads RequireJS; plotly.js may register as an ' + "window.exports});/*nbviewer loads RequireJS; plotly.js may register as an " 'anonymous AMD module, triggering"Mismatched anonymous define()" and leaving ' "`Plotly` undefined. Temporarily disableAMD/CommonJS detection while loading " 'plotly.js from the CDN.*/if (typeof window.define === "function" && ' "window.define.amd) { window.define.amd = undefined;}if (typeof " 'window.module === "object" && window.module && window.module.exports) { ' - "window.module = undefined;}if (typeof window.exports === \"object\") { " + 'window.module = undefined;}if (typeof window.exports === "object") { ' "window.exports = undefined;} \n " )