From 3ed505804570be0eacb7d7a4f2511042b2701597 Mon Sep 17 00:00:00 2001 From: adehad <26027314+adehad@users.noreply.github.com> Date: Tue, 8 Dec 2020 12:41:39 +0000 Subject: [PATCH 1/7] feature/include_plotlyjs uses bundles js version, new cdn-latest option --- packages/python/plotly/plotly/io/_html.py | 39 +++++++++----- .../python/plotly/plotly/tests/test_html.py | 52 +++++++++++++++++++ packages/python/plotly/plotly/tests/utils.py | 8 +++ 3 files changed, 87 insertions(+), 12 deletions(-) create mode 100644 packages/python/plotly/plotly/tests/test_html.py diff --git a/packages/python/plotly/plotly/io/_html.py b/packages/python/plotly/plotly/io/_html.py index cb90de32f98..d0ba380de34 100644 --- a/packages/python/plotly/plotly/io/_html.py +++ b/packages/python/plotly/plotly/io/_html.py @@ -6,7 +6,7 @@ import six from plotly.io._utils import validate_coerce_fig_to_dict -from plotly.offline.offline import _get_jconfig, get_plotlyjs +from plotly.offline.offline import _get_jconfig, get_plotlyjs, get_plotlyjs_version from plotly import utils @@ -58,10 +58,16 @@ def to_html( 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. + 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 'cdn-latest', a script tag that always references the latest 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 @@ -266,12 +272,15 @@ def to_html( require_start = 'require(["plotly"], function(Plotly) {' require_end = "});" - elif include_plotlyjs == "cdn": + elif include_plotlyjs == "cdn" or include_plotlyjs == "cdn-latest": + cdn_ver = get_plotlyjs_version() + if include_plotlyjs == "cdn-latest": + cdn_ver = "latest" load_plotlyjs = """\ {win_config} - \ + \ """.format( - win_config=_window_plotly_config + win_config=_window_plotly_config, cdn_ver=cdn_ver ) elif include_plotlyjs == "directory": @@ -417,10 +426,16 @@ def write_html( 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. + 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 'cdn-latest', a script tag that always references the latest 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 diff --git a/packages/python/plotly/plotly/tests/test_html.py b/packages/python/plotly/plotly/tests/test_html.py new file mode 100644 index 00000000000..3c0efa4f546 --- /dev/null +++ b/packages/python/plotly/plotly/tests/test_html.py @@ -0,0 +1,52 @@ +import sys + +import pytest +import numpy as np + + +import plotly.graph_objs as go +import plotly.io as pio +from plotly.tests.utils import plotly_cdn_url + + +if sys.version_info >= (3, 3): + import unittest.mock as mock + from unittest.mock import MagicMock +else: + import mock + from mock import MagicMock + +# fixtures +# -------- +@pytest.fixture +def fig1(request): + return go.Figure( + data=[ + { + "type": "scatter", + "y": np.array([2, 1, 3, 2, 4, 2]), + "marker": {"color": "green"}, + } + ], + layout={"title": {"text": "Figure title"}}, + ) + + +# HTML +# ---- +def assert_latest_cdn_connected(html): + assert plotly_cdn_url(cdn_ver="latest") in html + + +def assert_locked_version_cdn_connected(html): + assert plotly_cdn_url() in html + + +def test_latest_cdn_included(fig1): + html_str = pio.to_html(fig1, include_plotlyjs="cdn-latest") + assert_latest_cdn_connected(html_str) + + +def test_versioned_cdn_included(fig1): + html_str = pio.to_html(fig1, include_plotlyjs="cdn") + assert_locked_version_cdn_connected(html_str) diff --git a/packages/python/plotly/plotly/tests/utils.py b/packages/python/plotly/plotly/tests/utils.py index fc0ad0e8c75..eb4471efef2 100644 --- a/packages/python/plotly/plotly/tests/utils.py +++ b/packages/python/plotly/plotly/tests/utils.py @@ -2,6 +2,7 @@ from numbers import Number as Num from unittest import TestCase import plotly.io as pio +from plotly.offline import get_plotlyjs_version class TestCaseNoTemplate(TestCase): @@ -95,3 +96,10 @@ def is_num_list(item): except TypeError: return False return True + + +def plotly_cdn_url(cdn_ver=get_plotlyjs_version()): + """Return plotly CDN url for use in assertions.""" + return "https://cdn.plot.ly/plotly-{cdn_ver}.min.js".format( + cdn_ver=cdn_ver + ) From 49a777dad658218b126e993cf3d65440585df57e Mon Sep 17 00:00:00 2001 From: adehad <26027314+adehad@users.noreply.github.com> Date: Tue, 8 Dec 2020 16:07:39 +0000 Subject: [PATCH 2/7] fix other tests that are broken by new 'cdn' option behaviour --- .../plotly/tests/test_core/test_offline/test_offline.py | 4 +++- .../python/plotly/plotly/tests/test_io/test_renderers.py | 5 +++-- 2 files changed, 6 insertions(+), 3 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 315a81b9417..e407864a0ec 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 @@ -12,6 +12,8 @@ import plotly import plotly.io as pio +from plotly.tests.utils import plotly_cdn_url + import json packages_root = os.path.dirname( @@ -39,7 +41,7 @@ """ -cdn_script = '" +cdn_script = ''.format(cdn_url=plotly_cdn_url()) directory_script = '' 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 3511201c7d5..ed9870fc941 100644 --- a/packages/python/plotly/plotly/tests/test_io/test_renderers.py +++ b/packages/python/plotly/plotly/tests/test_io/test_renderers.py @@ -11,6 +11,7 @@ import plotly.graph_objs as go import plotly.io as pio from plotly.offline import get_plotlyjs +from plotly.tests.utils import plotly_cdn_url if sys.version_info >= (3, 3): import unittest.mock as mock @@ -135,7 +136,7 @@ def assert_not_full_html(html): def assert_connected(html): - assert "https://cdn.plot.ly/plotly-latest.min" in html + assert plotly_cdn_url() in html def assert_offline(html): @@ -306,7 +307,7 @@ def test_repr_html(renderer): template = ( '
\n " - ' ' + ' ' '
""".format( - win_config=_window_plotly_config, mathjax_config=_mathjax_config + 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 d0ba380de34..52f1cee7f79 100644 --- a/packages/python/plotly/plotly/io/_html.py +++ b/packages/python/plotly/plotly/io/_html.py @@ -5,8 +5,8 @@ import six -from plotly.io._utils import validate_coerce_fig_to_dict -from plotly.offline.offline import _get_jconfig, get_plotlyjs, get_plotlyjs_version +from plotly.io._utils import validate_coerce_fig_to_dict, plotly_cdn_url +from plotly.offline.offline import _get_jconfig, get_plotlyjs from plotly import utils @@ -273,14 +273,14 @@ def to_html( require_end = "});" elif include_plotlyjs == "cdn" or include_plotlyjs == "cdn-latest": - cdn_ver = get_plotlyjs_version() + cdn_url = plotly_cdn_url() if include_plotlyjs == "cdn-latest": - cdn_ver = "latest" + cdn_url = plotly_cdn_url(cdn_ver="latest") load_plotlyjs = """\ {win_config} - \ + \ """.format( - win_config=_window_plotly_config, cdn_ver=cdn_ver + win_config=_window_plotly_config, cdn_url=cdn_url ) elif include_plotlyjs == "directory": diff --git a/packages/python/plotly/plotly/io/_utils.py b/packages/python/plotly/plotly/io/_utils.py index b3b376e9d89..93648bc6c37 100644 --- a/packages/python/plotly/plotly/io/_utils.py +++ b/packages/python/plotly/plotly/io/_utils.py @@ -2,6 +2,7 @@ import plotly import plotly.graph_objs as go +from plotly.offline import get_plotlyjs_version def validate_coerce_fig_to_dict(fig, validate): @@ -40,3 +41,10 @@ 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_html.py b/packages/python/plotly/plotly/tests/test_io/test_html.py similarity index 95% rename from packages/python/plotly/plotly/tests/test_html.py rename to packages/python/plotly/plotly/tests/test_io/test_html.py index 3c0efa4f546..026677c0d58 100644 --- a/packages/python/plotly/plotly/tests/test_html.py +++ b/packages/python/plotly/plotly/tests/test_io/test_html.py @@ -6,7 +6,7 @@ import plotly.graph_objs as go import plotly.io as pio -from plotly.tests.utils import plotly_cdn_url +from plotly.io._utils import plotly_cdn_url if sys.version_info >= (3, 3): 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 ed9870fc941..8a58a2373be 100644 --- a/packages/python/plotly/plotly/tests/test_io/test_renderers.py +++ b/packages/python/plotly/plotly/tests/test_io/test_renderers.py @@ -11,7 +11,7 @@ import plotly.graph_objs as go import plotly.io as pio from plotly.offline import get_plotlyjs -from plotly.tests.utils import plotly_cdn_url +from plotly.io._utils import plotly_cdn_url if sys.version_info >= (3, 3): import unittest.mock as mock @@ -135,8 +135,8 @@ def assert_not_full_html(html): assert not html.startswith(" Date: Tue, 8 Dec 2020 17:39:07 +0000 Subject: [PATCH 4/7] add new `include_plotlyjs='cdn'`behaviour to changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ac2a557fa5..00e51141230 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Added ### Fixed +- Plotly.js cdn url will now be versioned by default for: +`include_plotlyjs='cdn'` a new `include_plotlyjs='cdn-latest'` option +has the original behaviour. Prevents likelihood of htmls generated with older `plotly.js` versions breaking with version bumps. [2961](https://github.com/plotly/plotly.py/pull/2961) ### Updated From 023ee573acef95a9a62636082149d82e416caca2 Mon Sep 17 00:00:00 2001 From: adehad <26027314+adehad@users.noreply.github.com> Date: Tue, 8 Dec 2020 17:45:54 +0000 Subject: [PATCH 5/7] fix tests after poor refactor --- .../plotly/plotly/tests/test_core/test_offline/test_offline.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 e407864a0ec..c3f428117e0 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 @@ -12,7 +12,7 @@ import plotly import plotly.io as pio -from plotly.tests.utils import plotly_cdn_url +from plotly.io._utils import plotly_cdn_url import json From 12db5cb1f3fa22cec33873e95b89d90adaa2ccb6 Mon Sep 17 00:00:00 2001 From: adehad <26027314+adehad@users.noreply.github.com> Date: Tue, 8 Dec 2020 17:53:08 +0000 Subject: [PATCH 6/7] format after using right black version --- packages/python/plotly/plotly/io/_utils.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/python/plotly/plotly/io/_utils.py b/packages/python/plotly/plotly/io/_utils.py index 93648bc6c37..c8d47712d11 100644 --- a/packages/python/plotly/plotly/io/_utils.py +++ b/packages/python/plotly/plotly/io/_utils.py @@ -45,6 +45,4 @@ def validate_coerce_output_type(output_type): 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, - ) + return "https://cdn.plot.ly/plotly-{cdn_ver}.min.js".format(cdn_ver=cdn_ver,) From 7e4b256150391f8186f80bcd881fa35f8d793d33 Mon Sep 17 00:00:00 2001 From: adehad <26027314+adehad@users.noreply.github.com> Date: Tue, 8 Dec 2020 17:58:28 +0000 Subject: [PATCH 7/7] format changelog entry better --- CHANGELOG.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00e51141230..a89170626b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +7,11 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Added ### Fixed -- Plotly.js cdn url will now be versioned by default for: -`include_plotlyjs='cdn'` a new `include_plotlyjs='cdn-latest'` option -has the original behaviour. Prevents likelihood of htmls generated with older `plotly.js` versions breaking with version bumps. [2961](https://github.com/plotly/plotly.py/pull/2961) +- Plotly.js cdn url will now be versioned by default for: + `include_plotlyjs='cdn'` a new `include_plotlyjs='cdn-latest'` option + has the original behaviour. Prevents likelihood of htmls generated with older + `plotly.js` versions breaking with version bumps. + [2961](https://github.com/plotly/plotly.py/pull/2961) ### Updated