使用作为实例变量的 Dash 对象作为 Dash 中的回调的 Python 装饰器 - 失败

Jim*_*Jim 14 python callback python-decorators plotly-dash

我正在更新一些代码以使用 Dash 和 plotly。绘图的主要代码是在一个类中定义的。我用 Dash 控件替换了一些 Bokeh 小部件,最后得到了一个如下所示的回调:

class MakeStuff:
    def __init__(self, ..., **optional):
        ...
        self.app = dash.Dash(...)
        ...

    @self.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(self,range_start,graph_width):
        print(...)
Run Code Online (Sandbox Code Playgroud)

我正在关注Dash 网站上的一些示例。我能够运行示例,包括回调。在我的代码中,没有装饰器,代码运行时不会出错,按照我的预期生成图形和控件。(当然,代码不完整,但没有错误。)当我包含装饰器时,我得到这个错误:

NameError: 名称 'self' 未定义

我就这样累了,首先,只是模仿代码示例:

class MakeStuff:
    def __init__(self, ..., **optional):
        ...
        app = dash.Dash(...)
        ...

    @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(self,range_start,graph_width):
        print(...)
Run Code Online (Sandbox Code Playgroud)

当然,变量“app”只在init函数的范围内知道,所以它不起作用也就不足为奇了,给出了类似的错误:

NameError:未定义名称“应用程序”

是否有一种简单的方法可以设置此装饰器使其工作,同时仍将我的代码保留在类定义中?我猜装饰器正在进行一些预处理,但我不太了解它,无法提出解决方案。

Sho*_*alt 17

您可以调用回调函数而不是作为装饰器,如本答案所示。这应该在您的__init__函数中起作用:

class MakeStuff:
    def __init__(self, ..., **optional):
        ...
        self.app = dash.Dash(...)
        app.callback(dash.dependencies.Output('indicator-graphic', 'figure'),
            [dash.dependencies.Input('start-time-slider', 'value'),
             dash.dependencies.Input('graph-width-slider', 'value')])(self.update_graphs)
        ...

    def update_graphs(self,range_start,graph_width):
        print(...)
Run Code Online (Sandbox Code Playgroud)

我以前从未用类实例尝试过它,但没有理由让它不起作用。


tis*_*res 5

ned2在这里提供了一个解决方案,他使用以下结构在类定义中设置装饰器。

class BaseBlock:
def __init__(self, app=None):
    self.app = app

    if self.app is not None and hasattr(self, 'callbacks'):
        self.callbacks(self.app)

class MyBlock(BaseBlock):
    layout = html.Div('layout for this "block".')

    def callbacks(self, app):

        @app.callback(Output('foo', 'figure'), [Input('bar')])
        def do_things(bar):
            return SOME_DATA

        @app.callback(Output('baz', 'figure'), [Input('boop')])
        def do_things(boop):
            return OTHER_DATA

# creating a new MyBlock will register all callbacks
block = MyBlock(app=app)

# now insert this component into the app's layout 
app.layout['slot'] = block.layout
Run Code Online (Sandbox Code Playgroud)