Handel multiple callback outputs programmatically

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

In practice, this might look something like:

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)
9 Likes