Skip to content

Commit 69dc7ba

Browse files
committed
Add option to specify include_plotlyjs as a path/url to a *.js file
This makes it possible to specify an alternative CDN or offline location for plotly.js
1 parent f4f17ff commit 69dc7ba

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

plotly/offline/offline.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ def plot(figure_or_data, show_link=True, link_text='Export to plot.ly',
449449
in a standalone HTML file.
450450
Use 'div' if you are embedding these graphs in an HTML file with
451451
other graphs or HTML markup, like a HTML report or an website.
452-
include_plotlyjs (True | False | 'cdn' | 'directory' - default=True) --
452+
include_plotlyjs (True | False | 'cdn' | 'directory' | path - default=True)
453453
Specifies how the plotly.js library is included in the output html
454454
file or div string.
455455
@@ -475,6 +475,10 @@ def plot(figure_or_data, show_link=True, link_text='Export to plot.ly',
475475
the same directory because the plotly.js source code will be included
476476
only once per output directory, rather than once per output file.
477477
478+
If a string that ends in '.js', a script tag is included that
479+
references the specified path. This approach can be used to point
480+
the resulting HTML file to an alternative CDN.
481+
478482
If False, no script tag referencing plotly.js is included. This is
479483
useful when output_type='div' and the resulting div string will be
480484
placed inside an HTML document that already loads plotly.js. This
@@ -530,6 +534,10 @@ def plot(figure_or_data, show_link=True, link_text='Export to plot.ly',
530534
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>"""
531535
elif include_plotlyjs == 'directory':
532536
plotly_js_script = '<script src="plotly.min.js"></script>'
537+
elif (isinstance(include_plotlyjs, six.string_types) and
538+
include_plotlyjs.endswith('.js')):
539+
plotly_js_script = '<script src="{url}"></script>'.format(
540+
url=include_plotlyjs)
533541
elif include_plotlyjs:
534542
plotly_js_script = ''.join([
535543
'<script type="text/javascript">',

plotly/tests/test_core/test_offline/test_offline.py

+33
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,39 @@ def test_including_plotlyjs_directory_div(self):
183183
# when output_type is div
184184
self.assertFalse(os.path.exists('plotly.min.js'))
185185

186+
def test_including_plotlyjs_path_html(self):
187+
for include_plotlyjs in [
188+
('https://cdnjs.cloudflare.com/ajax/libs/plotly.js/1.40.1/'
189+
'plotly.min.js'),
190+
'subpath/to/plotly.min.js',
191+
'something.js']:
192+
193+
html = self._read_html(plotly.offline.plot(
194+
fig,
195+
include_plotlyjs=include_plotlyjs,
196+
output_type='file',
197+
auto_open=False))
198+
self.assertNotIn(PLOTLYJS, html)
199+
self.assertNotIn(cdn_script, html)
200+
self.assertNotIn(directory_script, html)
201+
self.assertIn(include_plotlyjs, html)
202+
203+
def test_including_plotlyjs_path_div(self):
204+
for include_plotlyjs in [
205+
('https://cdnjs.cloudflare.com/ajax/libs/plotly.js/1.40.1/'
206+
'plotly.min.js'),
207+
'subpath/to/plotly.min.js',
208+
'something.js']:
209+
210+
html = plotly.offline.plot(
211+
fig,
212+
include_plotlyjs=include_plotlyjs,
213+
output_type='div')
214+
self.assertNotIn(PLOTLYJS, html)
215+
self.assertNotIn(cdn_script, html)
216+
self.assertNotIn(directory_script, html)
217+
self.assertIn(include_plotlyjs, html)
218+
186219
def test_div_output(self):
187220
html = plotly.offline.plot(fig, output_type='div', auto_open=False)
188221

0 commit comments

Comments
 (0)