如何获取Plotly Dash上动画滑块的当前值?

时间:2020-11-10 08:27:40

标签: python animation slider plotly plotly-dash

我是Plotly和Dash的新手。我可以知道如何获取动画滑块的当前值吗?

我在这里所做的是单击按钮以找出动画滑块的当前值。我尝试使用“ fig.layout.sliders [0] .active”,但是即使滑块处于其他值,它也始终返回相同的索引,即0。 例如,我想在下图中获取动画的当前值,即1972(索引为4)。我以为'fig.layout.sliders [0] .active'将返回4,但始终返回0(起始索引,1952)。

编辑:让我在这里给出一个更清晰的场景。 我单击了“播放”按钮,动画滑块开始从1952年移动到2007年(如图所示)。我在“ 1972”停止了带有“ year = 1972”标签的动画幻灯片。 然后,当我按下“单击我”按钮时,它应该打印“ 4”,因为1972位于animation_frame的索引4处。但是,它打印为“ 0”。无论我在哪一年停止动画滑块,它都只会打印“ 0” snapshot

import dash_core_components as dcc
import dash_html_components as html

from dash.dependencies import Input, Output
import plotly.express as px

df = px.data.gapminder()

fig = px.bar(df, x="continent", y="pop", color="continent", animation_frame="year", animation_group="country", range_y=[0,4000000000])

layout = html.Div([
    html.Label(id='test', children=[]),
    html.Button('Click me', id='btn'),
    dcc.Graph(id='my-map', figure=fig ),

])


@app.callback(Output('test', 'children'),
              [Input(component_id='btn', component_property='n_clicks')])
def testing(value):
    if value:
        print('can',fig.layout.sliders[0].active)
    return ['dummy']

1 个答案:

答案 0 :(得分:0)

如果没有完整的应用设置,很难确定。但是从您的图形设置中,您可以使用以下方法检索框架的标签:

fig.layout.sliders[0].steps[0].label

哪个返回'1952'

fig.layout.sliders[0].steps[-1].label

将返回'2007'

我希望这会有所帮助。如果没有,请与我们分享有关您的应用程序的所有详细信息,我将仔细研究。

相关问题