Adding multiple play buttons that animate every nth frame

Hi,

I am trying to create an animation that has multiple play buttons.

  • One button that plays every frame
  • One button that plays every other frame
  • One button that plays every 5th frame

The below example sort of works but ideally I want all three play buttons to work with one pause button and one slider.

Additionally, the below example is suboptimal because it makes the size of the html file twice as big. Each button stores their list of frames separately within the html file. Iā€™d prefer each frame is stored once in the html code.

import plotly.graph_objects as go

list_of_frames = []
for x in range(-50,50):
    plot = go.Figure()
    this_frame = plot.add_trace(go.Scatter3d(x=[x/20, x/20+1], y=[-x/20, -x/20-1], z=[1,6], line=dict(color='red', width=8)))
    list_of_frames.append(go.Frame(name="{:.3f}".format(x), data=this_frame['data'], layout=this_frame['layout']))

fig = go.Figure(data=list_of_frames[0]['data'],
    frames=list_of_frames)

def frame_args(duration):
    return {"frame": {"duration": duration},
            "mode": "immediate",
            "fromcurrent": True,
            "transition": {"duration": 0},}

sliders = [
    {"steps": [{"args": [[f.name], frame_args(0)],
             "label": str(k),
             "method": "animate",}
            for k, f in enumerate(fig.frames)],}]

fig.update_layout(
    scene=dict(
        xaxis=dict(range=[-6, 6], showgrid=False, showspikes=False, showbackground=True, showaxeslabels=True),
        yaxis=dict(range=[-6, 6], showgrid=False, showspikes=False, showbackground=True,showaxeslabels=True),
        zaxis=dict(range=[0, 8], showgrid=False, showspikes=False, showbackground=True, showaxeslabels=True)),
    transition=dict(duration=0),
    updatemenus=[{"buttons": [
                {   "args": [None, frame_args(0.000001)],
                    "label": "PLAY",  # play symbol
                    "method": "animate",},
                {   "args": [list_of_frames[::2], frame_args(0.000001)],
                    "label": "PLAY 2X",  # play symbol
                    "method": "animate",},
                {   "args": [list_of_frames[::5], frame_args(0.000001)],
                    "label": "PLAY 5X",  # play symbol
                    "method": "animate",},
                {   "args": [[None], {"frame": {"duration": 0},
                                      "mode": "immediate",
                                      "transition": {"duration": 0}}],
                    "label": "PAUSE",  # pause symbol
                    "method": "animate",},],
            "type": "buttons",}],
    sliders=sliders)

fig.show()