# Handel multiple callback outputs programmatically

**URL:** <https://community.plotly.com/t/handel-multiple-callback-outputs-programmatically/4759>\
**Category:** Dash Python\
**Created:** [July 3, 2017, 9:27pm UTC](https://community.plotly.com/t/handel-multiple-callback-outputs-programmatically/4759 "2017-07-03T21:27:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![icewater](https://avatars.discourse-cdn.com/v4/letter/i/8e7dd6/32.png) [@icewater](https://community.plotly.com/u/icewater)\
**Post date:** [July 3, 2017, 9:27pm UTC](https://community.plotly.com/t/handel-multiple-callback-outputs-programmatically/4759/1 "2017-07-03T21:27:59Z")

</div>

According to Dash’s user guide, each Dash callback function can only update a single Output property. I have multiple outputs to be updated for a callback input. It is advised to define a callback function for each output. However, it is very tedious when the outputs to be updated are many and very similar. Is there anyway to define a callback function programmatically to handle multiple outputs?

---

<div class="post-metadata">

**Author:** ![chriddyp](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/chriddyp/32/16855_2.png) [@chriddyp](https://community.plotly.com/u/chriddyp)\
**Post date:** [July 3, 2017, 10:12pm UTC](https://community.plotly.com/t/handel-multiple-callback-outputs-programmatically/4759/2 "2017-07-03T22:12:26Z")

</div>

Good question. There are a few patterns in Python that can help out with this.

1 - Functions can return other functions  
2 - Decorators can be called directly.

I highly reccommend this essay on Stack Overflow about decorators: [https://stackoverflow.com/questions/739654/how-to-make-a-chain-of-function-decorators/1594484#1594484](https://stackoverflow.com/questions/739654/how-to-make-a-chain-of-function-decorators/1594484#1594484)

In practice, this might look something like:

```auto
output_elements = ['id1', 'id2']

def create_callback(output):
     def callback(input_value):
        if output == 'id1':
            # do something
        elif output == 'id2':
            # do something different
    return callback

for output_element in output_elements:
     dynamically_generated_function = create_callback(output_element)
    app.callback(Output(output_element, '...'), [Input(...)])(dynamically_generated_function)

```

---

<div class="post-metadata">

**Author:** ![icewater](https://avatars.discourse-cdn.com/v4/letter/i/8e7dd6/32.png) [@icewater](https://community.plotly.com/u/icewater)\
**Post date:** [July 5, 2017, 2:37pm UTC](https://community.plotly.com/t/handel-multiple-callback-outputs-programmatically/4759/3 "2017-07-05T14:37:38Z")

</div>

Awesome! I just tried your advice, it worked!

---

<div class="post-metadata">

**Author:** ![chriddyp](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/chriddyp/32/16855_2.png) [@chriddyp](https://community.plotly.com/u/chriddyp)\
**Post date:** [July 5, 2017, 10:34pm UTC](https://community.plotly.com/t/handel-multiple-callback-outputs-programmatically/4759/4 "2017-07-05T22:34:59Z")

</div>

Great! Here’s another real-world example of generating dash callback functions: [https://github.com/plotly/dash/issues/59#issuecomment-313214212](https://github.com/plotly/dash/issues/59#issuecomment-313214212)

---

<div class="post-metadata">

**Author:** ![sterlingbutters](https://avatars.discourse-cdn.com/v4/letter/s/ed8c4c/32.png) [@sterlingbutters](https://community.plotly.com/u/sterlingbutters)\
**Post date:** [March 22, 2018, 2:50am UTC](https://community.plotly.com/t/handel-multiple-callback-outputs-programmatically/4759/5 "2018-03-22T02:50:08Z")

</div>

In my case, this does not seem to work.

> <https://stackoverflow.com/questions/49419718/plotly-dash-callback-decorator-in-loop-not-possible>

---

<div class="post-metadata">

**Author:** ![chriddyp](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/chriddyp/32/16855_2.png) [@chriddyp](https://community.plotly.com/u/chriddyp)\
**Post date:** [March 22, 2018, 4:48am UTC](https://community.plotly.com/t/handel-multiple-callback-outputs-programmatically/4759/6 "2018-03-22T04:48:30Z")

</div>

> [@sterlingbutters](#):
>
> In my case, this does not seem to work.

Also see this topic here: [Dynamic Controls and Dynamic Output Components](https://community.plotly.com/t/dynamic-controls-and-dynamic-output-components/5519).
