【问题标题】:TypeError when using a callback in Dash在 Dash 中使用回调时出现 TypeError
【发布时间】:2021-01-07 02:37:22
【问题描述】:

我已经看到了很多关于回调不起作用的其他问题,但我的问题略有不同。

当我开始编写仪表板时,我决定将它变成一个类,因为它的使用方式将需要使用不同的数据或特定参数创建它的实例。这已经让我有点悲伤了,但我不知道该怎么做。

无论如何,现在当前的问题是我需要根据滑块的值更新一个绘图。绘图本身最初是在此方法中创建的:

    def _create_plan_view(self, rl: int, component: str, thickness=20):
        plan_data = self.data.loc[(self.data.zcentre <= rl) &
                                  (self.data.zcentre >= rl-thickness), :]
        plan = px.scatter(plan_data, x='xcentre', y='ycentre', color=component,
                          color_continuous_scale='turbo')

        plan.update_layout(
            autosize=False,
            width=1200,
            height=1000,
            plot_bgcolor='#383838',
            paper_bgcolor='#383838',
            font_color='#ffffff')
        return plan

这是滑块和绘图的应用布局代码:

            html.Div(children=[
                html.Div(children=[
                    dcc.Slider(
                        id='elevation_slider',
                        min=self.data.zcentre.min(),
                        max=self.data.zcentre.max(),
                        step=(self.data.zcentre.max() - self.data.zcentre.min()) / 100,
                        value=0,
                        marks={val: f'{val}' for val in range(self.data.zcentre.min(), self.data.zcentre.max(), 100)},
                        className='two columns offset-by-one',
                        vertical=True,
                        verticalHeight=900
                    )
                ]),
                html.Div(children=[
                    dcc.Graph(id='plan_view',
                              figure=self._create_plan_view(rl=400, component='foo'))],
                         className='nine columns'
                )]),

现在要更新它,我只想获取滑块的当前值并使用不同的参数运行 self._create_plan_view() 方法:

    @app.callback(
        Output('plan_view', 'figure'),
        Input('elevation_slider', 'value')
    )
    def _update_plan_view_for_elevation(self, elevation):
        fig = self._create_plan_view(rl=elevation, component='foo')
        return fig

但这只会导致 Dash 不断抛出错误:TypeError: _update_plan_view_for_elevation() missing 1 required positional argument: 'elevation'

我不确定如何调试回调,但我觉得可能正在发生的是滑块的实际值被分配给self,然后elevation 没有一个值。

会是这样吗?我尝试将self 作为输入之一传递,但这不起作用。

【问题讨论】:

    标签: python callback plotly-dash


    【解决方案1】:

    我不确定如何调试回调,但我觉得可能发生的是滑块的实际值被分配给 self,然后高度没有值...

    是的,这就是正在发生的事情

    您可以创建一个初始化回调的方法,这样回调本身就不必将self 作为参数:

    def init_callbacks(self):
        @app.callback(Output("plan_view", "figure"), Input("elevation_slider", "value"))
        def _update_plan_view_for_elevation(elevation):
            fig = self._create_plan_view(rl=elevation, component="foo")
            return fig
    

    _update_plan_view_for_elevation 可以从其外部范围init_callbacks 访问self。现在你可以在你的类的__init__ 函数中调用self.init_callbacks(),或者在你的布局设置好之后的其他地方。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-20
      • 2021-12-22
      • 1970-01-01
      • 2017-12-17
      • 2021-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多