简体   繁体   English

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

[英]Python Decorator as Callback in Dash Using Dash Object That is an Instance Variable - Fails

I'm updating some code to use Dash and plotly.我正在更新一些代码以使用 Dash 和 plotly。 The main code for graphing is defined within a class.绘图的主要代码是在一个类中定义的。 I replaced some Bokeh widgets with Dash controls, and ended up with a callback that looks like this:我用 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(...)

I am following some examples from the Dash website .我正在关注Dash 网站上的一些示例。 I was able to run the examples, including callbacks.我能够运行示例,包括回调。 In my code, without the decorator, the code runs without error, producing the graphics and controls as I expected it to.在我的代码中,没有装饰器,代码运行时不会出错,按照我的预期生成图形和控件。 (Of course, the code is incomplete, but there is no error.) When I include the decorator, I get this error: (当然,代码不完整,但没有错误。)当我包含装饰器时,我得到这个错误:

NameError: name 'self' is not defined NameError: 名称 'self' 未定义

I tired it this way, first, just mimicking the code examples:我就这样累了,首先,只是模仿代码示例:

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(...)

Of course, the variable "app" is only know within the scope of the init function, so it's no surprise that that doesn't work, giving the similar error:当然,变量“app”只在init函数的范围内知道,所以它不起作用也就不足为奇了,给出了类似的错误:

NameError: name 'app' is not defined NameError:未定义名称“应用程序”

Is there a straightforward way to set up this decorator to work while still keeping my code within a class definition?是否有一种简单的方法可以设置此装饰器使其工作,同时仍将我的代码保留在类定义中? I am guessing some pre-processing is going on with the decorator, but I don't understand it well enough to come up with a solution.我猜装饰器正在进行一些预处理,但我不太了解它,无法提出解决方案。

You could call the callback function not as a decorator, as shown in this answer .您可以调用回调函数而不是作为装饰器,如本答案所示。 This should work from within your __init__ function:这应该在您的__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(...)

I've never tried it with a class instance before, but see no reason for it not to work.我以前从未用类实例尝试过它,但没有理由让它不起作用。

ned2 provides a solution here , he uses the following structure to set up the decorators within a class definition. 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM