Skip to content

Instantly share code, notes, and snippets.

@MBulli
Created February 21, 2023 12:25
Show Gist options
  • Save MBulli/eded787c6011c09339956c9f814972d0 to your computer and use it in GitHub Desktop.
Save MBulli/eded787c6011c09339956c9f814972d0 to your computer and use it in GitHub Desktop.
import plotly.graph_objects as go
def plotly_generate_slider(figure: go.Figure, sliderValues, numTracesPerStep=1, title='') -> go.Figure:
assert len(figure.data) % numTracesPerStep == 0
steps = [] # slider steps
numTraceGroups = int(len(figure.data) / numTracesPerStep)
for i in range(0, numTraceGroups):
visArgs = [False] * len(figure.data)
for traceGroupIdx in range(numTracesPerStep):
visArgs[numTracesPerStep * i + traceGroupIdx] = True
figure.data[numTracesPerStep * i + traceGroupIdx].visible = (i == 0) # show the first trace group and hide all other
steps.append(go.layout.slider.Step(
method="update",
args=[{"visible": visArgs},
{"title": title}
], # layout attribute
label=f"{sliderValues[i]}",
))
sliders = [go.layout.Slider(
active=0,
currentvalue={"prefix": "RSSI: "},
pad={"t": 50},
steps=steps
)]
figure.update_layout(
sliders=sliders
)
return figure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment