Skip to content

Commit 3ed5058

Browse files
committed
feature/include_plotlyjs uses bundles js version, new cdn-latest option
1 parent 063eb06 commit 3ed5058

File tree

3 files changed

+87
-12
lines changed

3 files changed

+87
-12
lines changed

packages/python/plotly/plotly/io/_html.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import six
77

88
from plotly.io._utils import validate_coerce_fig_to_dict
9-
from plotly.offline.offline import _get_jconfig, get_plotlyjs
9+
from plotly.offline.offline import _get_jconfig, get_plotlyjs, get_plotlyjs_version
1010
from plotly import utils
1111

1212

@@ -58,10 +58,16 @@ def to_html(
5858
fully self-contained and can be used offline.
5959
6060
If 'cdn', a script tag that references the plotly.js CDN is included
61-
in the output. HTML files generated with this option are about 3MB
62-
smaller than those generated with include_plotlyjs=True, but they
63-
require an active internet connection in order to load the plotly.js
64-
library.
61+
in the output. The url used is versioned to match the bundled plotly.js.
62+
HTML files generated with this option are about 3MB smaller than those
63+
generated with include_plotlyjs=True, but they require an active
64+
internet connection in order to load the plotly.js library.
65+
66+
If 'cdn-latest', a script tag that always references the latest plotly.js
67+
CDN is included in the output.
68+
HTML files generated with this option are about 3MB smaller than those
69+
generated with include_plotlyjs=True, but they require an active
70+
internet connection in order to load the plotly.js library.
6571
6672
If 'directory', a script tag is included that references an external
6773
plotly.min.js bundle that is assumed to reside in the same
@@ -266,12 +272,15 @@ def to_html(
266272
require_start = 'require(["plotly"], function(Plotly) {'
267273
require_end = "});"
268274

269-
elif include_plotlyjs == "cdn":
275+
elif include_plotlyjs == "cdn" or include_plotlyjs == "cdn-latest":
276+
cdn_ver = get_plotlyjs_version()
277+
if include_plotlyjs == "cdn-latest":
278+
cdn_ver = "latest"
270279
load_plotlyjs = """\
271280
{win_config}
272-
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>\
281+
<script src="https://cdn.plot.ly/plotly-{cdn_ver}.min.js"></script>\
273282
""".format(
274-
win_config=_window_plotly_config
283+
win_config=_window_plotly_config, cdn_ver=cdn_ver
275284
)
276285

277286
elif include_plotlyjs == "directory":
@@ -417,10 +426,16 @@ def write_html(
417426
fully self-contained and can be used offline.
418427
419428
If 'cdn', a script tag that references the plotly.js CDN is included
420-
in the output. HTML files generated with this option are about 3MB
421-
smaller than those generated with include_plotlyjs=True, but they
422-
require an active internet connection in order to load the plotly.js
423-
library.
429+
in the output. The url used is versioned to match the bundled plotly.js.
430+
HTML files generated with this option are about 3MB smaller than those
431+
generated with include_plotlyjs=True, but they require an active
432+
internet connection in order to load the plotly.js library.
433+
434+
If 'cdn-latest', a script tag that always references the latest plotly.js
435+
CDN is included in the output.
436+
HTML files generated with this option are about 3MB smaller than those
437+
generated with include_plotlyjs=True, but they require an active
438+
internet connection in order to load the plotly.js library.
424439
425440
If 'directory', a script tag is included that references an external
426441
plotly.min.js bundle that is assumed to reside in the same
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import sys
2+
3+
import pytest
4+
import numpy as np
5+
6+
7+
import plotly.graph_objs as go
8+
import plotly.io as pio
9+
from plotly.tests.utils import plotly_cdn_url
10+
11+
12+
if sys.version_info >= (3, 3):
13+
import unittest.mock as mock
14+
from unittest.mock import MagicMock
15+
else:
16+
import mock
17+
from mock import MagicMock
18+
19+
# fixtures
20+
# --------
21+
@pytest.fixture
22+
def fig1(request):
23+
return go.Figure(
24+
data=[
25+
{
26+
"type": "scatter",
27+
"y": np.array([2, 1, 3, 2, 4, 2]),
28+
"marker": {"color": "green"},
29+
}
30+
],
31+
layout={"title": {"text": "Figure title"}},
32+
)
33+
34+
35+
# HTML
36+
# ----
37+
def assert_latest_cdn_connected(html):
38+
assert plotly_cdn_url(cdn_ver="latest") in html
39+
40+
41+
def assert_locked_version_cdn_connected(html):
42+
assert plotly_cdn_url() in html
43+
44+
45+
def test_latest_cdn_included(fig1):
46+
html_str = pio.to_html(fig1, include_plotlyjs="cdn-latest")
47+
assert_latest_cdn_connected(html_str)
48+
49+
50+
def test_versioned_cdn_included(fig1):
51+
html_str = pio.to_html(fig1, include_plotlyjs="cdn")
52+
assert_locked_version_cdn_connected(html_str)

packages/python/plotly/plotly/tests/utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from numbers import Number as Num
33
from unittest import TestCase
44
import plotly.io as pio
5+
from plotly.offline import get_plotlyjs_version
56

67

78
class TestCaseNoTemplate(TestCase):
@@ -95,3 +96,10 @@ def is_num_list(item):
9596
except TypeError:
9697
return False
9798
return True
99+
100+
101+
def plotly_cdn_url(cdn_ver=get_plotlyjs_version()):
102+
"""Return plotly CDN url for use in assertions."""
103+
return "https://cdn.plot.ly/plotly-{cdn_ver}.min.js".format(
104+
cdn_ver=cdn_ver
105+
)

0 commit comments

Comments
 (0)