Skip to content

Commit 01c65e1

Browse files
authored
Added get_plotlyjs and get_plotlyjs_version functions (#1246)
* Make get_plotlyjs public and add docstring * Added plotly.offline.get_plotlyjs_version() function. This function returns the plotly.js versions that is bundled with plotly.py. The returned version is updated automatically during the updatebundle setup.py command.
1 parent d4c965c commit 01c65e1

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

Diff for: plotly/offline/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"""
66
from . offline import (
77
download_plotlyjs,
8+
get_plotlyjs_version,
9+
get_plotlyjs,
810
enable_mpl_offline,
911
init_notebook_mode,
1012
iplot,

Diff for: plotly/offline/_plotlyjs_version.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# DO NOT EDIT
2+
# This file is generated by the updatebundle setup.py command
3+
__plotlyjs_version__ = '1.41.3'

Diff for: plotly/offline/offline.py

+56
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import plotly
1919
from plotly import optional_imports, tools, utils
2020
from plotly.exceptions import PlotlyError
21+
from ._plotlyjs_version import __plotlyjs_version__
2122

2223
ipython = optional_imports.get_module('IPython')
2324
ipython_display = optional_imports.get_module('IPython.display')
@@ -37,11 +38,66 @@ def download_plotlyjs(download_url):
3738
pass
3839

3940

41+
def get_plotlyjs_version():
42+
"""
43+
Returns the version of plotly.js that is bundled with plotly.py.
44+
45+
Returns
46+
-------
47+
str
48+
Plotly.js version string
49+
"""
50+
return __plotlyjs_version__
51+
52+
4053
def get_plotlyjs():
54+
"""
55+
Return the contents of the minified plotly.js library as a string.
56+
57+
This may be useful when building standalone HTML reports.
58+
59+
Returns
60+
-------
61+
str
62+
Contents of the minified plotly.js library as a string
63+
64+
Examples
65+
--------
66+
Here is an example of creating a standalone HTML report that contains
67+
two plotly figures, each in their own div. The include_plotlyjs argument
68+
is set to False when creating the divs so that we don't include multiple
69+
copies of the plotly.js library in the output. Instead, a single copy
70+
of plotly.js is included in a script tag in the html head element.
71+
72+
>>> import plotly.graph_objs as go
73+
>>> from plotly.offline import plot, get_plotlyjs
74+
>>> fig1 = go.Figure(data=[{'type': 'bar', 'y': [1, 3, 2]}],
75+
... layout={'height': 400})
76+
>>> fig2 = go.Figure(data=[{'type': 'scatter', 'y': [1, 3, 2]}],
77+
... layout={'height': 400})
78+
>>> div1 = plot(fig1, output_type='div', include_plotlyjs=False)
79+
>>> div2 = plot(fig2, output_type='div', include_plotlyjs=False)
80+
81+
>>> html = '''
82+
... <html>
83+
... <head>
84+
... <script type="text/javascript">{plotlyjs}</script>
85+
... </head>
86+
... <body>
87+
... {div1}
88+
... {div2}
89+
... </body>
90+
... </html>
91+
...'''.format(plotlyjs=get_plotlyjs(), div1=div1, div2=div2)
92+
93+
>>> with open('multi_plot.html', 'w') as f:
94+
... f.write(html)
95+
"""
4196
path = os.path.join('package_data', 'plotly.min.js')
4297
plotlyjs = pkgutil.get_data('plotly', path).decode('utf-8')
4398
return plotlyjs
4499

100+
45101
def get_image_download_script(caller):
46102
"""
47103
This function will return a script that will download an image of a Plotly

Diff for: plotly/tests/test_core/test_offline/test_offline.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from requests.compat import json as _json
1111

1212
import plotly
13+
import json
1314

1415

1516
fig = {
@@ -28,7 +29,7 @@
2829
]
2930

3031

31-
PLOTLYJS = plotly.offline.offline.get_plotlyjs()
32+
PLOTLYJS = plotly.offline.get_plotlyjs()
3233

3334
cdn_script = ('<script src="https://cdn.plot.ly/plotly-latest.min.js">'
3435
'</script>')
@@ -270,3 +271,11 @@ def test_config(self):
270271
self.assertIn('"linkText": "Plotly rocks!"', html)
271272
self.assertIn('"showLink": true', html)
272273
self.assertIn('"editable": true', html)
274+
275+
def test_plotlyjs_version(self):
276+
with open('js/package.json', 'rt') as f:
277+
package_json = json.load(f)
278+
expected_version = package_json['dependencies']['plotly.js']
279+
280+
self.assertEqual(expected_version,
281+
plotly.offline.get_plotlyjs_version())

Diff for: setup.py

+8
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,14 @@ def run(self):
199199
with open('plotly/package_data/plotly.min.js', 'wb') as f:
200200
f.write(response.read())
201201

202+
# Write plotly.js version file
203+
with open('plotly/offline/_plotlyjs_version.py', 'w') as f:
204+
f.write("""\
205+
# DO NOT EDIT
206+
# This file is generated by the updatebundle setup.py command
207+
__plotlyjs_version__ = '{plotlyjs_version}'
208+
""".format(plotlyjs_version=plotly_js_version()))
209+
202210

203211
class UpdatePlotlyJsCommand(Command):
204212
description = 'Update project to a new version of plotly.js'

0 commit comments

Comments
 (0)