Can't we make a callback function that shares output?

The output of the callback functions 1 and 2 is the same. But their inputs are different.
When there is only the first callback function, the callback function works normally.
However, if Iwrite the callback function number 2, both number 1 and number 2 do not work.
Even if I write a code like No. 3, nothing is printed.

I dont konw why this code doesn’t work.

First Callback:
image

Second Callback:
image

Third Image:

Thank you

1 Like

It is not possible to have multiple callback functions updating the same Output. You will need to combine your two callbacks into a single one.

1 Like

You can use the MultiplexerTransform from dash-extensions,

import dash_html_components as html
from dash_extensions.enrich import Output, DashProxy, Input, MultiplexerTransform

app = DashProxy(prevent_initial_callbacks=True, transforms=[MultiplexerTransform()])
app.layout = html.Div([html.Button("left", id="left"), html.Button("right", id="right"), html.Div(id="log")])


@app.callback(Output("log", "children"), Input("left", "n_clicks"))
def left(_):
    return "left"


@app.callback(Output("log", "children"), Input("right", "n_clicks"))
def right(_):
    return "right"


if __name__ == '__main__':
    app.run_server()

Thanks! I solved this problem. :slight_smile:

Thank you! It helps me to solve this problem

Screen Shot 2022-07-09 at 12.30.45 PM

It’s silly that I can’t have a “reset” button pattern that clears a component’s children just because some other callback created that component’s children. The reset state would have to become an input to the creation callback.

It would be easier to just target the same component property. Conditionally nulling/hiding the different inputs in a monolithic callback creates undue complexity. It leads to [not my post]:
python - A nonexistent object was used in an `Input` of a Dash callback an multipage app - Stack Overflow.

I don’t want 3 separate pages or 3 separate output divs

This feels similar to how: app.config.supress_callback_exceptions is False by default.

:heart:

Is preventing multiple callbacks from sharing a common output either: (a) hard-wired into how the callback DAG works, or (b) more of a preventative best-practice-like measure?

Hi @HashRocketSyntax You can find more background information and discussion about this issue in this post: Duplicate Callback Outputs - Solution & API Discussion

1 Like

@HashRocketSyntax I believe sharing a common ouput between callbacks will be feature of Dash at some point, but current it is not possible to do directly (it is not just a best practices thing, you’ll get a error if you do it). That being said, the MultiplexerTransform from dash-extensions should cover most cases, so I would recommend using it until a native Dash solution is in place :slight_smile:

1 Like