来自类内的 Dash 回调永远不会被触发

问题描述 投票:0回答:0

我创建了一个 Dash 应用程序,它基本上有一个按钮,一旦点击它就会创建一个绘图。

由于用户可能会多次点击按钮,我为该人物制作者创建了一个类。

但是,有一个称为 plot_figure 的回调,出于某种原因我从来没有得到它,它使图形为空。

课程如下:

class DashFigureApp:
    def __init__(self, title, x_value, y_value):
        self.app = DashProxy(title=title, transforms=[ServersideOutputTransform(), TriggerTransform()],
                             external_stylesheets=[dbc.themes.BOOTSTRAP])
        self.port = 8050
        self.x_value = x_value
        self.y_value = y_value

        uid = str(uuid4())

        # Defining the layout of the Dash app
        self.layout = html.Div(id={"type": "div-id", "index": uid}, style=DIV_GRAPH_STYLE,
                               children=[

            dcc.Graph(id={"type": "dynamic-graph", "index": uid},
                      figure=go.Figure()),
            dcc.Loading(
                dcc.Store(id={"type": "store", "index": uid})),
            TraceUpdater(id={"type": "dynamic-updater",
                             "index": uid}, gdID=f"{uid}"),

            dcc.Interval(
                id={"type": "interval", "index": uid}, max_intervals=1, interval=1
            ),
        ],
        )
        self.app.layout = self.layout
        self.register_callbacks()

    def run(self):
        if (self.x_value is not None) and (self.y_value is not None):
            return self.app

    def register_callbacks(self):
        @self.app.callback(
            ServersideOutput({"type": "store", "index": MATCH}, "data"),
            Output({"type": "dynamic-graph", "index": MATCH}, "figure"),
            Trigger({"type": "interval", "index": MATCH}, "n_intervals"),
        )
        def callback(n):
            return plot_figure(x_value=self.x_value, y_value=self.y_value)

我在单击按钮时调用它,如下所示(在我的主 div 中):

who_trigged = ctx.triggered_id
if who_trigged == 'add-plot':
    try:
        new_figure = DashFigureApp(
            title="", x_value=x_value, y_value=y_value)
        new_figure.run(is_new_figure=False)
        div_children.append(new_figure.layout)
        return div_children
    except:
        raise PreventUpdate

我知道所有组件都是单独工作的,如果我将回调移到类之外,一切正常,但我希望它在内部以组织代码。

谢谢

python callback plotly-dash
© www.soinside.com 2019 - 2024. All rights reserved.