Skip to content

Commit e53e626

Browse files
authored
add new auto_play argument to offline plot and iplot (#1447)
This controls whether to automatically start the animation on page load if the figure contains frames
1 parent b3dbceb commit e53e626

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

Diff for: plotly/offline/offline.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ def init_notebook_mode(connected=False):
311311

312312

313313
def _plot_html(figure_or_data, config, validate, default_width,
314-
default_height, global_requirejs):
314+
default_height, global_requirejs, auto_play):
315315

316316
figure = tools.return_figure_from_figure_or_data(figure_or_data, validate)
317317

@@ -348,13 +348,20 @@ def _plot_html(figure_or_data, config, validate, default_width,
348348
'https://plot.ly')
349349

350350
if jframes:
351+
if auto_play:
352+
animate = (".then(function(){" +
353+
"Plotly.animate('{id}');".format(id=plotdivid) +
354+
"})")
355+
else:
356+
animate = ''
357+
351358
script = '''
352359
Plotly.plot(
353360
'{id}',
354361
{data},
355362
{layout},
356363
{config}
357-
).then(function () {add_frames}).then(function(){animate})
364+
).then(function () {add_frames}){animate}
358365
'''.format(
359366
id=plotdivid,
360367
data=jdata,
@@ -363,7 +370,7 @@ def _plot_html(figure_or_data, config, validate, default_width,
363370
add_frames="{" + "return Plotly.addFrames('{id}',{frames}".format(
364371
id=plotdivid, frames=jframes
365372
) + ");}",
366-
animate="{" + "Plotly.animate('{id}');".format(id=plotdivid) + "}"
373+
animate=animate
367374
)
368375
else:
369376
script = 'Plotly.newPlot("{id}", {data}, {layout}, {config})'.format(
@@ -397,7 +404,7 @@ def _plot_html(figure_or_data, config, validate, default_width,
397404

398405
def iplot(figure_or_data, show_link=False, link_text='Export to plot.ly',
399406
validate=True, image=None, filename='plot_image', image_width=800,
400-
image_height=600, config=None):
407+
image_height=600, config=None, auto_play=True):
401408
"""
402409
Draw plotly graphs inside an IPython or Jupyter notebook without
403410
connecting to an external server.
@@ -433,6 +440,9 @@ def iplot(figure_or_data, show_link=False, link_text='Export to plot.ly',
433440
config (default=None) -- Plot view options dictionary. Keyword arguments
434441
`show_link` and `link_text` set the associated options in this
435442
dictionary if it doesn't contain them already.
443+
auto_play (default=True) -- Whether to automatically start the animation
444+
sequence if the figure contains frames. Has no effect if the figure
445+
does not contain frames.
436446
437447
Example:
438448
```
@@ -473,8 +483,7 @@ def iplot(figure_or_data, show_link=False, link_text='Export to plot.ly',
473483

474484
if __PLOTLY_OFFLINE_INITIALIZED:
475485
plot_html, plotdivid, width, height = _plot_html(
476-
figure_or_data, config, validate, '100%', 525, True
477-
)
486+
figure_or_data, config, validate, '100%', 525, True, auto_play)
478487
resize_script = ''
479488
if width == '100%' or height == '100%':
480489
resize_script = _build_resize_script(
@@ -515,7 +524,7 @@ def plot(figure_or_data, show_link=False, link_text='Export to plot.ly',
515524
validate=True, output_type='file', include_plotlyjs=True,
516525
filename='temp-plot.html', auto_open=True, image=None,
517526
image_filename='plot_image', image_width=800, image_height=600,
518-
config=None, include_mathjax=False):
527+
config=None, include_mathjax=False, auto_play=True):
519528
""" Create a plotly graph locally as an HTML document or string.
520529
521530
Example:
@@ -625,6 +634,9 @@ def plot(figure_or_data, show_link=False, link_text='Export to plot.ly',
625634
If a string that ends in '.js', a script tag is included that
626635
references the specified path. This approach can be used to point the
627636
resulting HTML file to an alternative CDN.
637+
auto_play (default=True) -- Whether to automatically start the animation
638+
sequence on page load if the figure contains frames. Has no effect if
639+
the figure does not contain frames.
628640
"""
629641
if output_type not in ['div', 'file']:
630642
raise ValueError(
@@ -642,7 +654,7 @@ def plot(figure_or_data, show_link=False, link_text='Export to plot.ly',
642654

643655
plot_html, plotdivid, width, height = _plot_html(
644656
figure_or_data, config, validate,
645-
'100%', '100%', global_requirejs=False)
657+
'100%', '100%', global_requirejs=False, auto_play=auto_play)
646658

647659
# Build resize_script
648660
resize_script = ''

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

+25
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@
2323
)
2424
}
2525

26+
fig_frames = {
27+
'data': [
28+
plotly.graph_objs.Scatter(x=[1, 2, 3], y=[10, 20, 30])
29+
],
30+
'layout': plotly.graph_objs.Layout(
31+
title='offline plot'
32+
),
33+
'frames': [
34+
{'layout': {'title': 'frame 1'}}
35+
]
36+
}
37+
2638

2739
resize_code_strings = [
2840
'window.addEventListener("resize", ',
@@ -52,6 +64,9 @@
5264

5365
mathjax_font = 'STIX-Web'
5466

67+
add_frames = 'Plotly.addFrames'
68+
69+
do_auto_play = 'Plotly.animate'
5570

5671
class PlotlyOfflineBaseTestCase(TestCase):
5772
def tearDown(self):
@@ -396,3 +411,13 @@ def test_include_mathjax_path_div(self):
396411
self.assertNotIn(mathjax_cdn_script, html)
397412
self.assertIn(other_cdn+mathjax_config_str, html)
398413
self.assertIn(mathjax_font, html)
414+
415+
def test_auto_play(self):
416+
html = plotly.offline.plot(fig_frames, output_type='div')
417+
self.assertIn(add_frames, html)
418+
self.assertIn(do_auto_play, html)
419+
420+
def test_no_auto_play(self):
421+
html = plotly.offline.plot(fig_frames, output_type='div', auto_play=False)
422+
self.assertIn(add_frames, html)
423+
self.assertNotIn(do_auto_play, html)

0 commit comments

Comments
 (0)