plotly / dash

Data Apps & Dashboards for Python. No JavaScript Required.

Home Page:https://plotly.com/dash

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] Duplicate callback outputs

Liripo opened this issue · comments

Hello:
I tried using the same Input, but there are some differences in the Output, but dash prompts me that I need to increase allow_duplicate = True
image

example dash:

from dash import Dash, html,callback,Output,Input,no_update

app = Dash(__name__)

app.layout = html.Div([
    html.Div(children='output1',id="output1"),
    html.Div(children='output2',id="output2"),
    html.Button("button",id = "button")
])

@callback(
    Output("output1","children",allow_duplicate=True),
    Input("button","n_clicks"),
    prevent_initial_call=True,
)
def out1(n_clicks):
    return no_update

@callback(
    Output("output1","children",allow_duplicate=True),
    Output("output2","children"),
    Input("button","n_clicks"),
    prevent_initial_call=True,
)
def out2(n_clicks):
    return no_update,no_update

if __name__ == '__main__':
    app.run(debug=True)

If I change one of the Output parameters to allow_duplicate=False, no error is reported.

Thanks.

@Liripo 对于多次出现的Output,其中一个不设置allow_duplicate=True即可,譬如:
image

commented

hi @Liripo
I'm not getting the error that you are getting.

But @CNFeffery 's code is appropriate. Technically, only the second callback is the duplicate, so you only need to add allow_duplicate=True there.

If I increase it to 3, it has error.

from dash import Dash, html,callback,Output,Input,no_update

app = Dash(__name__)

app.layout = html.Div([
    html.Div(children='output1',id="output1"),
    html.Div(children='output2',id="output2"),
    html.Div(children='output3',id="output3"),
    html.Button("button",id = "button")
])

@callback(
    Output("output1","children"),
    Input("button","n_clicks"),
)
def out1(n_clicks):
    return no_update

@callback(
    Output("output1","children",allow_duplicate=True),
    Output("output2","children"),
    Input("button","n_clicks"),
    prevent_initial_call=True,
)
def out2(n_clicks):
    return no_update,no_update

@callback(
    Output("output1","children",allow_duplicate=True),
    Output("output3","children"),
    Input("button","n_clicks"),
    prevent_initial_call=True,
)
def out3(n_clicks):
    return no_update,no_update

if __name__ == '__main__':
    app.run(debug=True)

I think maybe it's a bug. @Coding-with-Adam

@Liripo 我觉得你这几个函数写法本身就很多余,明明是相同的Input()allow_duplicate不应该被滥用,只有在截然不同的Input角色对应重复的Output时才有它的设计意义