From 08a175dbdb077f683b3ffe08aa83dbad70079147 Mon Sep 17 00:00:00 2001 From: Martha Cryan Date: Wed, 11 Sep 2024 17:52:44 +0200 Subject: [PATCH 1/6] Remove references to plotly.js CDN Latest plotly.js used to be on a CDN, but it isn't anymore. --- .../python/plotly/plotly/io/_base_renderers.py | 7 ------- packages/python/plotly/plotly/io/_html.py | 5 ++--- packages/python/plotly/plotly/io/_utils.py | 7 ------- .../tests/test_core/test_offline/test_offline.py | 14 -------------- .../plotly/plotly/tests/test_io/test_html.py | 5 ----- .../plotly/plotly/tests/test_io/test_renderers.py | 9 +-------- 6 files changed, 3 insertions(+), 44 deletions(-) diff --git a/packages/python/plotly/plotly/io/_base_renderers.py b/packages/python/plotly/plotly/io/_base_renderers.py index 5a75f010633..cd2b9b320fd 100644 --- a/packages/python/plotly/plotly/io/_base_renderers.py +++ b/packages/python/plotly/plotly/io/_base_renderers.py @@ -8,7 +8,6 @@ from plotly import utils, optional_imports from plotly.io import to_json, to_image, write_image, write_html from plotly.io._orca import ensure_server -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 @@ -286,11 +285,6 @@ def activate(self): {mathjax_config} if (typeof require !== 'undefined') {{ require.undef("plotly"); - requirejs.config({{ - paths: {{ - 'plotly': ['{plotly_cdn}'] - }} - }}); require(['plotly'], function(Plotly) {{ window._Plotly = Plotly; }}); @@ -299,7 +293,6 @@ def activate(self): """.format( win_config=_window_plotly_config, mathjax_config=_mathjax_config, - plotly_cdn=plotly_cdn_url().rstrip(".js"), ) else: diff --git a/packages/python/plotly/plotly/io/_html.py b/packages/python/plotly/plotly/io/_html.py index 981f414aedf..cfcd0a55d34 100644 --- a/packages/python/plotly/plotly/io/_html.py +++ b/packages/python/plotly/plotly/io/_html.py @@ -3,7 +3,7 @@ import webbrowser from _plotly_utils.optional_imports import get_module -from plotly.io._utils import validate_coerce_fig_to_dict, plotly_cdn_url +from plotly.io._utils import validate_coerce_fig_to_dict from plotly.offline.offline import _get_jconfig, get_plotlyjs _json = get_module("json") @@ -270,9 +270,8 @@ def to_html( elif include_plotlyjs == "cdn": load_plotlyjs = """\ {win_config} - \ """.format( - win_config=_window_plotly_config, cdn_url=plotly_cdn_url() + win_config=_window_plotly_config ) elif include_plotlyjs == "directory": diff --git a/packages/python/plotly/plotly/io/_utils.py b/packages/python/plotly/plotly/io/_utils.py index 658540ca71a..e3ca5490587 100644 --- a/packages/python/plotly/plotly/io/_utils.py +++ b/packages/python/plotly/plotly/io/_utils.py @@ -41,10 +41,3 @@ def validate_coerce_output_type(output_type): Must be one of: 'Figure', 'FigureWidget'""" ) return cls - - -def plotly_cdn_url(cdn_ver=get_plotlyjs_version()): - """Return a valid plotly CDN url.""" - return "https://cdn.plot.ly/plotly-{cdn_ver}.min.js".format( - cdn_ver=cdn_ver, - ) diff --git a/packages/python/plotly/plotly/tests/test_core/test_offline/test_offline.py b/packages/python/plotly/plotly/tests/test_core/test_offline/test_offline.py index 37076f501ad..27abc250f62 100644 --- a/packages/python/plotly/plotly/tests/test_core/test_offline/test_offline.py +++ b/packages/python/plotly/plotly/tests/test_core/test_offline/test_offline.py @@ -9,7 +9,6 @@ import plotly import plotly.io as pio -from plotly.io._utils import plotly_cdn_url packages_root = os.path.dirname( os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(plotly.__file__)))) @@ -36,9 +35,6 @@ """ -cdn_script = ''.format( - cdn_url=plotly_cdn_url() -) directory_script = '' @@ -124,7 +120,6 @@ def test_including_plotlyjs_truthy_html(self): self.assertIn(plotly_config_script, html) self.assertIn(PLOTLYJS, html) - self.assertNotIn(cdn_script, html) self.assertNotIn(directory_script, html) def test_including_plotlyjs_truthy_div(self): @@ -137,7 +132,6 @@ def test_including_plotlyjs_truthy_div(self): self.assertIn(plotly_config_script, html) self.assertIn(PLOTLYJS, html) - self.assertNotIn(cdn_script, html) self.assertNotIn(directory_script, html) def test_including_plotlyjs_false_html(self): @@ -156,7 +150,6 @@ def test_including_plotlyjs_false_html(self): self.assertNotIn(plotly_config_script, html) self.assertNotIn(PLOTLYJS, html) - self.assertNotIn(cdn_script, html) self.assertNotIn(directory_script, html) def test_including_plotlyjs_false_div(self): @@ -166,7 +159,6 @@ def test_including_plotlyjs_false_div(self): ) self.assertNotIn(plotly_config_script, html) self.assertNotIn(PLOTLYJS, html) - self.assertNotIn(cdn_script, html) self.assertNotIn(directory_script, html) def test_including_plotlyjs_cdn_html(self): @@ -182,7 +174,6 @@ def test_including_plotlyjs_cdn_html(self): ) self.assertIn(plotly_config_script, html) self.assertNotIn(PLOTLYJS, html) - self.assertIn(cdn_script, html) self.assertNotIn(directory_script, html) def test_including_plotlyjs_cdn_div(self): @@ -192,7 +183,6 @@ def test_including_plotlyjs_cdn_div(self): ) self.assertIn(plotly_config_script, html) self.assertNotIn(PLOTLYJS, html) - self.assertIn(cdn_script, html) self.assertNotIn(directory_script, html) def test_including_plotlyjs_directory_html(self): @@ -209,7 +199,6 @@ def test_including_plotlyjs_directory_html(self): ) self.assertIn(plotly_config_script, html) self.assertNotIn(PLOTLYJS, html) - self.assertNotIn(cdn_script, html) self.assertIn(directory_script, html) # plot creates plotly.min.js in the output directory @@ -230,7 +219,6 @@ def test_including_plotlyjs_directory_div(self): self.assertIn(plotly_config_script, html) self.assertNotIn(PLOTLYJS, html) - self.assertNotIn(cdn_script, html) self.assertIn(directory_script, html) # plot does NOT create a plotly.min.js file in the output directory @@ -257,7 +245,6 @@ def test_including_plotlyjs_path_html(self): ) ) self.assertNotIn(PLOTLYJS, html) - self.assertNotIn(cdn_script, html) self.assertNotIn(directory_script, html) self.assertIn(include_plotlyjs, html) @@ -275,7 +262,6 @@ def test_including_plotlyjs_path_div(self): fig, include_plotlyjs=include_plotlyjs, output_type="div" ) self.assertNotIn(PLOTLYJS, html) - self.assertNotIn(cdn_script, html) self.assertNotIn(directory_script, html) self.assertIn(include_plotlyjs, html) diff --git a/packages/python/plotly/plotly/tests/test_io/test_html.py b/packages/python/plotly/plotly/tests/test_io/test_html.py index a056fd8f872..662e0a9d16b 100644 --- a/packages/python/plotly/plotly/tests/test_io/test_html.py +++ b/packages/python/plotly/plotly/tests/test_io/test_html.py @@ -6,7 +6,6 @@ import plotly.graph_objs as go import plotly.io as pio -from plotly.io._utils import plotly_cdn_url if sys.version_info >= (3, 3): @@ -36,10 +35,6 @@ def fig1(request): # ---- -def test_versioned_cdn_included(fig1): - assert plotly_cdn_url() in pio.to_html(fig1, include_plotlyjs="cdn") - - def test_html_deterministic(fig1): div_id = "plotly-root" assert pio.to_html(fig1, include_plotlyjs="cdn", div_id=div_id) == pio.to_html( diff --git a/packages/python/plotly/plotly/tests/test_io/test_renderers.py b/packages/python/plotly/plotly/tests/test_io/test_renderers.py index 63305cc719f..3cfdc72930d 100644 --- a/packages/python/plotly/plotly/tests/test_io/test_renderers.py +++ b/packages/python/plotly/plotly/tests/test_io/test_renderers.py @@ -12,7 +12,6 @@ import plotly.graph_objs as go import plotly.io as pio from plotly.offline import get_plotlyjs -from plotly.io._utils import plotly_cdn_url if sys.version_info >= (3, 3): import unittest.mock as mock @@ -137,10 +136,6 @@ def assert_not_full_html(html): assert not html.startswith(" \n " - ' ' + ' ' '
- """.format( - win_config=_window_plotly_config, - mathjax_config=_mathjax_config, - ) - else: # If not connected then we embed a copy of the plotly.js # library in the notebook @@ -302,15 +279,7 @@ def activate(self): """.format( script=get_plotlyjs(), @@ -324,12 +293,8 @@ def to_mimebundle(self, fig_dict): from plotly.io import to_html - if self.requirejs: - include_plotlyjs = "require" - include_mathjax = False - else: - include_plotlyjs = True - include_mathjax = "cdn" + include_plotlyjs = True + include_mathjax = "cdn" # build post script post_script = [ @@ -406,7 +371,6 @@ def __init__( super(NotebookRenderer, self).__init__( connected=connected, full_html=False, - requirejs=True, global_init=True, config=config, auto_play=auto_play, @@ -434,7 +398,6 @@ def __init__( super(KaggleRenderer, self).__init__( connected=True, full_html=False, - requirejs=True, global_init=True, config=config, auto_play=auto_play, @@ -462,7 +425,6 @@ def __init__( super(AzureRenderer, self).__init__( connected=True, full_html=False, - requirejs=True, global_init=True, config=config, auto_play=auto_play, @@ -487,7 +449,6 @@ def __init__( super(ColabRenderer, self).__init__( connected=True, full_html=True, - requirejs=False, global_init=False, config=config, auto_play=auto_play, @@ -822,7 +783,6 @@ def __init__( super(SphinxGalleryHtmlRenderer, self).__init__( connected=connected, full_html=False, - requirejs=False, global_init=False, config=config, auto_play=auto_play, @@ -834,12 +794,8 @@ def to_mimebundle(self, fig_dict): from plotly.io import to_html - if self.requirejs: - include_plotlyjs = "require" - include_mathjax = False - else: - include_plotlyjs = True - include_mathjax = "cdn" + include_plotlyjs = True + include_mathjax = "cdn" html = to_html( fig_dict, diff --git a/packages/python/plotly/plotly/io/_html.py b/packages/python/plotly/plotly/io/_html.py index cfcd0a55d34..62ca7c85234 100644 --- a/packages/python/plotly/plotly/io/_html.py +++ b/packages/python/plotly/plotly/io/_html.py @@ -254,27 +254,10 @@ def to_html( if isinstance(include_plotlyjs, str): include_plotlyjs = include_plotlyjs.lower() - # Start/end of requirejs block (if any) - require_start = "" - require_end = "" - # Init and load load_plotlyjs = "" - # Init plotlyjs. This block needs to run before plotly.js is loaded in - # order for MathJax configuration to work properly - if include_plotlyjs == "require": - require_start = 'require(["plotly"], function(Plotly) {' - require_end = "});" - - elif include_plotlyjs == "cdn": - load_plotlyjs = """\ - {win_config} - """.format( - win_config=_window_plotly_config - ) - - elif include_plotlyjs == "directory": + if include_plotlyjs == "directory": load_plotlyjs = """\ {win_config} \ @@ -342,10 +325,8 @@ def to_html(
\ \ """.format( mathjax_script=mathjax_script, @@ -354,9 +335,7 @@ def to_html( width=div_width, height=div_height, base_url_line=base_url_line, - require_start=require_start, script=script, - require_end=require_end, ).strip() if full_html: diff --git a/packages/python/plotly/plotly/tests/test_io/test_renderers.py b/packages/python/plotly/plotly/tests/test_io/test_renderers.py index 3cfdc72930d..5ee993e9999 100644 --- a/packages/python/plotly/plotly/tests/test_io/test_renderers.py +++ b/packages/python/plotly/plotly/tests/test_io/test_renderers.py @@ -140,14 +140,6 @@ def assert_offline(html): assert get_plotlyjs() in html -def assert_requirejs(html): - assert 'require(["plotly"]' in html - - -def assert_not_requirejs(html): - assert 'require(["plotly"]' not in html - - def test_colab_renderer_show(fig1): pio.renderers.default = "colab" @@ -165,7 +157,6 @@ def test_colab_renderer_show(fig1): html = mock_arg1["text/html"] assert_full_html(html) assert_html_renderer_connected(html) - assert_not_requirejs(html) # check kwargs mock_kwargs = mock_call_args[1] @@ -208,7 +199,6 @@ def test_notebook_connected_show(fig1, name, connected): # Check html display contents bundle_html = mock_arg1["text/html"] assert_not_full_html(bundle_html) - assert_requirejs(bundle_html) # check kwargs mock_kwargs = mock_call_args[1] @@ -270,7 +260,6 @@ def open_url(url, new=0, autoraise=True): html = response.content.decode("utf8") assert_full_html(html) assert_offline(html) - assert_not_requirejs(html) # Validation From e2cfbf6cc0a9d30206cf5512831e9138147793c9 Mon Sep 17 00:00:00 2001 From: Martha Cryan Date: Wed, 11 Sep 2024 18:29:01 +0200 Subject: [PATCH 4/6] Update comments describing the type of include_plotlyjs --- .../python/plotly/plotly/basedatatypes.py | 18 -------------- packages/python/plotly/plotly/io/_html.py | 24 ------------------- 2 files changed, 42 deletions(-) diff --git a/packages/python/plotly/plotly/basedatatypes.py b/packages/python/plotly/plotly/basedatatypes.py index bfb10d5c611..54a78d08c25 100644 --- a/packages/python/plotly/plotly/basedatatypes.py +++ b/packages/python/plotly/plotly/basedatatypes.py @@ -3528,12 +3528,6 @@ def to_html(self, *args, **kwargs): plotly.min.js bundle that is assumed to reside in the same directory as the HTML file. - If 'require', Plotly.js is loaded using require.js. This option - assumes that require.js is globally available and that it has been - globally configured to know how to find Plotly.js as 'plotly'. - This option is not advised when full_html=True as it will result - in a non-functional html file. - If a string that ends in '.js', a script tag is included that references the specified path. This approach can be used to point the resulting HTML file to an alternative CDN or local bundle. @@ -3618,12 +3612,6 @@ def write_html(self, *args, **kwargs): is included in the output. HTML files generated with this option are fully self-contained and can be used offline. - If 'cdn', a script tag that references the plotly.js CDN is included - in the output. HTML files generated with this option are about 3MB - smaller than those generated with include_plotlyjs=True, but they - require an active internet connection in order to load the plotly.js - library. - If 'directory', a script tag is included that references an external plotly.min.js bundle that is assumed to reside in the same directory as the HTML file. If `file` is a string to a local file @@ -3637,12 +3625,6 @@ def write_html(self, *args, **kwargs): directory because the plotly.js source code will be included only once per output directory, rather than once per output file. - If 'require', Plotly.js is loaded using require.js. This option - assumes that require.js is globally available and that it has been - globally configured to know how to find Plotly.js as 'plotly'. - This option is not advised when full_html=True as it will result - in a non-functional html file. - If a string that ends in '.js', a script tag is included that references the specified path. This approach can be used to point the resulting HTML file to an alternative CDN or local bundle. diff --git a/packages/python/plotly/plotly/io/_html.py b/packages/python/plotly/plotly/io/_html.py index 62ca7c85234..65c4b1c60fe 100644 --- a/packages/python/plotly/plotly/io/_html.py +++ b/packages/python/plotly/plotly/io/_html.py @@ -57,22 +57,10 @@ def to_html( is included in the output. HTML files generated with this option are fully self-contained and can be used offline. - If 'cdn', a script tag that references the plotly.js CDN is included - in the output. The url used is versioned to match the bundled plotly.js. - HTML files generated with this option are about 3MB smaller than those - generated with include_plotlyjs=True, but they require an active - internet connection in order to load the plotly.js library. - If 'directory', a script tag is included that references an external plotly.min.js bundle that is assumed to reside in the same directory as the HTML file. - If 'require', Plotly.js is loaded using require.js. This option - assumes that require.js is globally available and that it has been - globally configured to know how to find Plotly.js as 'plotly'. - This option is not advised when full_html=True as it will result - in a non-functional html file. - If a string that ends in '.js', a script tag is included that references the specified path. This approach can be used to point the resulting HTML file to an alternative CDN or local bundle. @@ -392,12 +380,6 @@ def write_html( is included in the output. HTML files generated with this option are fully self-contained and can be used offline. - If 'cdn', a script tag that references the plotly.js CDN is included - in the output. The url used is versioned to match the bundled plotly.js. - HTML files generated with this option are about 3MB smaller than those - generated with include_plotlyjs=True, but they require an active - internet connection in order to load the plotly.js library. - If 'directory', a script tag is included that references an external plotly.min.js bundle that is assumed to reside in the same directory as the HTML file. If `file` is a string to a local file @@ -411,12 +393,6 @@ def write_html( directory because the plotly.js source code will be included only once per output directory, rather than once per output file. - If 'require', Plotly.js is loaded using require.js. This option - assumes that require.js is globally available and that it has been - globally configured to know how to find Plotly.js as 'plotly'. - This option is not advised when full_html=True as it will result - in a non-functional html file. - If a string that ends in '.js', a script tag is included that references the specified path. This approach can be used to point the resulting HTML file to an alternative CDN or local bundle. From c0205a1f344559c402a31e08290beda38a52aa29 Mon Sep 17 00:00:00 2001 From: Martha Cryan Date: Wed, 11 Sep 2024 18:34:05 +0200 Subject: [PATCH 5/6] Remove old tests --- .../test_core/test_offline/test_offline.py | 24 ------------------- 1 file changed, 24 deletions(-) diff --git a/packages/python/plotly/plotly/tests/test_core/test_offline/test_offline.py b/packages/python/plotly/plotly/tests/test_core/test_offline/test_offline.py index 27abc250f62..a45fd1a1216 100644 --- a/packages/python/plotly/plotly/tests/test_core/test_offline/test_offline.py +++ b/packages/python/plotly/plotly/tests/test_core/test_offline/test_offline.py @@ -161,30 +161,6 @@ def test_including_plotlyjs_false_div(self): self.assertNotIn(PLOTLYJS, html) self.assertNotIn(directory_script, html) - def test_including_plotlyjs_cdn_html(self): - for include_plotlyjs in ["cdn", "CDN", "Cdn"]: - html = self._read_html( - plotly.offline.plot( - fig, - include_plotlyjs=include_plotlyjs, - output_type="file", - filename=html_filename, - auto_open=False, - ) - ) - self.assertIn(plotly_config_script, html) - self.assertNotIn(PLOTLYJS, html) - self.assertNotIn(directory_script, html) - - def test_including_plotlyjs_cdn_div(self): - for include_plotlyjs in ["cdn", "CDN", "Cdn"]: - html = plotly.offline.plot( - fig, include_plotlyjs=include_plotlyjs, output_type="div" - ) - self.assertIn(plotly_config_script, html) - self.assertNotIn(PLOTLYJS, html) - self.assertNotIn(directory_script, html) - def test_including_plotlyjs_directory_html(self): self.assertFalse(os.path.exists(os.path.join(here, "plotly.min.js"))) From 4a67af43f02c8636a58912d4e02d4ec9545032db Mon Sep 17 00:00:00 2001 From: Martha Cryan Date: Wed, 11 Sep 2024 18:46:48 +0200 Subject: [PATCH 6/6] Remove references to cdn assertion function --- .../python/plotly/plotly/tests/test_io/test_renderers.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/python/plotly/plotly/tests/test_io/test_renderers.py b/packages/python/plotly/plotly/tests/test_io/test_renderers.py index 5ee993e9999..95c47f5c260 100644 --- a/packages/python/plotly/plotly/tests/test_io/test_renderers.py +++ b/packages/python/plotly/plotly/tests/test_io/test_renderers.py @@ -156,7 +156,6 @@ def test_colab_renderer_show(fig1): # Check html contents html = mock_arg1["text/html"] assert_full_html(html) - assert_html_renderer_connected(html) # check kwargs mock_kwargs = mock_call_args[1] @@ -183,10 +182,7 @@ 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) - else: - assert_offline(bundle_display_html) + assert_offline(bundle_display_html) # ### Check display call ### # Get display call arguments