From 2184798220577719a743cea1e579f80245552bbb Mon Sep 17 00:00:00 2001 From: Jon Mease Date: Sat, 27 Oct 2018 08:04:03 -0400 Subject: [PATCH 1/2] Make get_plotlyjs public and add docstring --- plotly/offline/__init__.py | 1 + plotly/offline/offline.py | 42 +++++++++++++++++++ .../test_core/test_offline/test_offline.py | 2 +- 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/plotly/offline/__init__.py b/plotly/offline/__init__.py index f4a5fc2d2b6..eea46a1f792 100644 --- a/plotly/offline/__init__.py +++ b/plotly/offline/__init__.py @@ -5,6 +5,7 @@ """ from . offline import ( download_plotlyjs, + get_plotlyjs, enable_mpl_offline, init_notebook_mode, iplot, diff --git a/plotly/offline/offline.py b/plotly/offline/offline.py index d0a98a86ac5..865d9d3c4b1 100644 --- a/plotly/offline/offline.py +++ b/plotly/offline/offline.py @@ -38,6 +38,48 @@ def download_plotlyjs(download_url): def get_plotlyjs(): + """ + Return the contents of the minified plotly.js library as a string. + + This may be useful when building standalone HTML reports. + + Returns + ------- + str + Contents of the minified plotly.js library as a string + + Examples + -------- + Here is an example of creating a standalone HTML report that contains + two plotly figures, each in their own div. The include_plotlyjs argument + is set to False when creating the divs so that we don't include multiple + copies of the plotly.js library in the output. Instead, a single copy + of plotly.js is included in a script tag in the html head element. + + >>> import plotly.graph_objs as go + >>> from plotly.offline import plot, get_plotlyjs + >>> fig1 = go.Figure(data=[{'type': 'bar', 'y': [1, 3, 2]}], + ... layout={'height': 400}) + >>> fig2 = go.Figure(data=[{'type': 'scatter', 'y': [1, 3, 2]}], + ... layout={'height': 400}) + >>> div1 = plot(fig1, output_type='div', include_plotlyjs=False) + >>> div2 = plot(fig2, output_type='div', include_plotlyjs=False) + + >>> html = ''' + ... + ... + ... + ... + ... + ... {div1} + ... {div2} + ... + ... + ...'''.format(plotlyjs=get_plotlyjs(), div1=div1, div2=div2) + + >>> with open('multi_plot.html', 'w') as f: + ... f.write(html) + """ path = os.path.join('package_data', 'plotly.min.js') plotlyjs = pkgutil.get_data('plotly', path).decode('utf-8') return plotlyjs diff --git a/plotly/tests/test_core/test_offline/test_offline.py b/plotly/tests/test_core/test_offline/test_offline.py index 40f14e9b08e..10d143d0c92 100644 --- a/plotly/tests/test_core/test_offline/test_offline.py +++ b/plotly/tests/test_core/test_offline/test_offline.py @@ -28,7 +28,7 @@ ] -PLOTLYJS = plotly.offline.offline.get_plotlyjs() +PLOTLYJS = plotly.offline.get_plotlyjs() cdn_script = ('') From 288a683cc03da7a314cdaf0eb203c314490e5362 Mon Sep 17 00:00:00 2001 From: Jon Mease Date: Sat, 27 Oct 2018 08:30:31 -0400 Subject: [PATCH 2/2] 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. --- plotly/offline/__init__.py | 1 + plotly/offline/_plotlyjs_version.py | 3 +++ plotly/offline/offline.py | 14 ++++++++++++++ .../tests/test_core/test_offline/test_offline.py | 9 +++++++++ setup.py | 8 ++++++++ 5 files changed, 35 insertions(+) create mode 100644 plotly/offline/_plotlyjs_version.py diff --git a/plotly/offline/__init__.py b/plotly/offline/__init__.py index eea46a1f792..f918c68c589 100644 --- a/plotly/offline/__init__.py +++ b/plotly/offline/__init__.py @@ -5,6 +5,7 @@ """ from . offline import ( download_plotlyjs, + get_plotlyjs_version, get_plotlyjs, enable_mpl_offline, init_notebook_mode, diff --git a/plotly/offline/_plotlyjs_version.py b/plotly/offline/_plotlyjs_version.py new file mode 100644 index 00000000000..07b8272ef77 --- /dev/null +++ b/plotly/offline/_plotlyjs_version.py @@ -0,0 +1,3 @@ +# DO NOT EDIT +# This file is generated by the updatebundle setup.py command +__plotlyjs_version__ = '1.41.3' diff --git a/plotly/offline/offline.py b/plotly/offline/offline.py index 865d9d3c4b1..44d2c4fc796 100644 --- a/plotly/offline/offline.py +++ b/plotly/offline/offline.py @@ -18,6 +18,7 @@ import plotly from plotly import optional_imports, tools, utils from plotly.exceptions import PlotlyError +from ._plotlyjs_version import __plotlyjs_version__ ipython = optional_imports.get_module('IPython') ipython_display = optional_imports.get_module('IPython.display') @@ -37,6 +38,18 @@ def download_plotlyjs(download_url): pass +def get_plotlyjs_version(): + """ + Returns the version of plotly.js that is bundled with plotly.py. + + Returns + ------- + str + Plotly.js version string + """ + return __plotlyjs_version__ + + def get_plotlyjs(): """ Return the contents of the minified plotly.js library as a string. @@ -84,6 +97,7 @@ def get_plotlyjs(): plotlyjs = pkgutil.get_data('plotly', path).decode('utf-8') return plotlyjs + def get_image_download_script(caller): """ This function will return a script that will download an image of a Plotly diff --git a/plotly/tests/test_core/test_offline/test_offline.py b/plotly/tests/test_core/test_offline/test_offline.py index 10d143d0c92..f98123b741f 100644 --- a/plotly/tests/test_core/test_offline/test_offline.py +++ b/plotly/tests/test_core/test_offline/test_offline.py @@ -10,6 +10,7 @@ from requests.compat import json as _json import plotly +import json fig = { @@ -270,3 +271,11 @@ def test_config(self): self.assertIn('"linkText": "Plotly rocks!"', html) self.assertIn('"showLink": true', html) self.assertIn('"editable": true', html) + + def test_plotlyjs_version(self): + with open('js/package.json', 'rt') as f: + package_json = json.load(f) + expected_version = package_json['dependencies']['plotly.js'] + + self.assertEqual(expected_version, + plotly.offline.get_plotlyjs_version()) diff --git a/setup.py b/setup.py index e6dc61dd443..b8c6d0ed98b 100644 --- a/setup.py +++ b/setup.py @@ -199,6 +199,14 @@ def run(self): with open('plotly/package_data/plotly.min.js', 'wb') as f: f.write(response.read()) + # Write plotly.js version file + with open('plotly/offline/_plotlyjs_version.py', 'w') as f: + f.write("""\ +# DO NOT EDIT +# This file is generated by the updatebundle setup.py command +__plotlyjs_version__ = '{plotlyjs_version}' +""".format(plotlyjs_version=plotly_js_version())) + class UpdatePlotlyJsCommand(Command): description = 'Update project to a new version of plotly.js'