Plotly Dash链式回调失败

时间:2019-02-17 20:25:33

标签: python callback plotly-dash

我刚刚熟悉Dash,所以不确定我是否正确理解。事件处理的Dash实现对我来说似乎违反直觉。

我有一个图形(现在暂存),一个确定我的数据集的哪一部分将在图形中显示的滑块,以及一个增加滑块值的按钮。 (这是我最终要完成的工作的简化版本,但现在我被困在这里。)

我遇到的问题是,单击按钮更新滑块时,滑块会发生变化。但是滑块的变化不会影响到更新图形的回调。 (手动更改滑块将调用图形更新回调。)

我想念什么吗?我该怎么做? (请注意,如果我在图更新回调的输入装饰器中添加按钮事件,那么我将无法控制回调的顺序AFAIK。这样做,可能会发生图更新而无需先更新开始时间的情况)

这是我实现回调的方式:

    # Update the graph whenever the start_time_slider value changes.
    # Update the value of the start_time_slider whenever the up button is clicked. Then, let the programmatic slider change cause the slider to fire an event, updating the graph.

    # When start_time_slider changes, update the graph
    @app.callback(
        dash.dependencies.Output('indicator-graphic', 'figure'),
            [dash.dependencies.Input('start-time-slider', 'value'),
             dash.dependencies.Input('graph-width-slider', 'value')]
            )
    def update_graphs(range_start,graph_width):
        print('Graph is updated. Start Time is {}, Width is {}'.format(range_start,graph_width))
        return None

    # On button click, update the start_time_slider
    @app.callback(
        dash.dependencies.Output('start-time-slider', 'value'),
        [dash.dependencies.Input('up-button', 'n_clicks')]
    )
    def graph_start_button_clicked(click_up, click_dn):
        self.graph_start_time += self.graph_width
        return self.graph_start_time

我最初将滑块配置为仅在“ mouseup”上引起回调。我认为这可能会阻止它由于程序更改而启动,因此我删除了它。删除它并没有使按钮按下事件冒泡到图形更新中。

                    dcc.Slider(
                        id='start-time-slider',
                        min=self.slider_start_time,
                        max=self.slider_end_time,
                        step=self.graph_start_time,
                        value=self.slider_start_time,
                        # updatemode='mouseup',
                    ),

0 个答案:

没有答案
相关问题