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 = ( '