question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

handle multiple Outputs in app.callback

See original GitHub issue

It would be nice to have the ability to update multiple outputs at once by specifying a list of Outputs and returning a tuple with the same number of outputs like in

@app.callback(
     [Output('label1', 'children'),
      Output('label2', 'children'),
     ],
     [Input('dropdown', 'value'),
      ])
 def cb_test_multi(value):
     # some very complex calculation to be triggered only once when 'dropdown' changes
     ...
     # result of calculation need to be pushed partly on 'label1' and partly on 'label2'
     return "Hello", value

This would allow simplifying otherwise complex flow to update multiple components based on one change.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:54
  • Comments:20 (8 by maintainers)

github_iconTop GitHub Comments

102reactions
chriddypcommented, Oct 27, 2017

Makes sense to me! The app.callback syntax and the HTTP API were designed in such a way to allow something like this. I left this out of the initial release because I thought that it would cause some confusion among users (mainly: when do you use multiple callbacks vs multiple outputs) and I wanted to keep the surface area as small as possible for the initial release. There are some other alternatives to “sharing computations” among multiple callbacks (see https://plot.ly/dash/sharing-data-between-callbacks) but none of them are as simple as this proposal.

By introducing multiple outputs, there will be 2 ways to do the same thing: 2 outputs in a single callback vs 2 callbacks with a single output. It really only makes to use multiple outputs if the callback is expensive.

I can also imagine that some users will end up using multiple outputs in cases where two callbacks that executed in parallel would end up resulting in faster UI updates, for example:

@app.callback([Output(...), Output(...)], ...)
def update_outputs(*inputs):
    val_1 = expensive_computation_1(*inputs)
    output_1 = make_output(val_1)

    # if this were a separate callback, the user could return output_1 at this point
    # instead, they have lumped together all of the outputs into one function, 
    # blocking the UI from updating until all outputs are ready
    val_2 = expensive_computation_2(*inputs)
    output_2 = make_output(val_2)
    return [output_1, output_2]

In any case, I’m 👍 with this now. It’ll be a bit of work to implement, so if you would like to see this work please 👍 this issue. If you or your company needs this work expedited and can sponsor development, please reach out: https://plot.ly/products/consulting-and-oem/

9reactions
chriddypcommented, Nov 14, 2018

Was this idea ever implemented?

It hasn’t been, but it’s still a good idea and I think we’ll end up implementing before the end of the year.

(as this would make my code very long/ugly).

What I’ve done in these types of situations is create a look that generates these functions:

def create_callback(id):
    def respond_to_button(*callback_args):
        ...

    return respond_to_button


# Dash callbacks have to be defined up-front and therefore must anticipate
# all of the elements that might exist on the page
for i in range(100):
    app.callback(
        Output('output-{}'.format(i), 'some-property'),
        [Input('button-{}'.format(i), 'n_clicks')])(
    create_callback(i))
Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple outputs in Dash - Now Available!
Use a list/tuple of Output as output in callbacks. Return a tuple/list of value from the callbacks. The returned list must have the...
Read more >
Is there a better way to perform multiple output with Dash by ...
As we can see in Interactivity part of Getting started, one callback function can accept multiple inputs but always has single ...
Read more >
The Dash Callback - Input, Output, State, and more - YouTube
The Callback is the most important element in Dash. You need to know it so you can create interactive dashboard apps.
Read more >
Learn about the Callback first if you are starting to learn Dash ...
The callback is what allows you to create interactive web apps and ... file) 12:26 - Multiple Outputs Inputs (radio-slider file) 17:03 ...
Read more >
Basic Dash Callbacks - angela1c.com
Multiple Inputs and Outputs · A dash app can have multiple inputs. · Any “Output” can have multiple “Input” components. · More than...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found