Flexible Callback Signatures

Flexible callback signatures are new in Dash 2.0! If you’re new to callbacks, read the Basic Callbacks chapter first.

Using flexible callback signatures, introduced in Dash 2.0, you can write app code that is easier to manage by using keyword arguments, argument grouping using dicts or tuples, and by mixing Input and State dependencies objects.

In Dash 1, the Input, State, and Output dependency objects are always provided to @app.callback as positional arguments (either positional arguments directly to @app.callback, or as lists to the inputs, state and output keyword arguments). The order in which the dependency objects are provided dictates the order of the positional arguments that are passed to the decorated callback function. This means that the names of the callback function arguments don’t matter, only the order they are defined in.

Positional Arguments

@app.callback(
    Output(...), Output(...),
    Input(...), Input(...),
    State(...)
)
def callback(a, b, c):
    return [a + b, b + c]
@app.callback(
    [Output(...), Output(...)],
    [Input(...), Input(...)],
    [State(...)]
)
def callback(a, b, c):
    return [a + b, b + c]
@app.callback(
    output=[Output(...), Output(...)],
    inputs=[Input(...), Input(...)],
    state=[State(...)]
)
def callback(a, b, c):
    return [a + b, b + c]

Keyword Arguments

Callback functions can register to be called with named keyword arguments. You can do this by passing dictionaries to the inputs and state arguments of @app.callback. In this case, the order of the callback’s function arguments doesn’t matter. All that matters is that the keys of the dependency dictionary match the function argument names.

Here is an example of using keyword input and state arguments:

@app.callback(
    output=[Output(...), Output(...)],
    inputs=dict(a=Input(...), b=Input(...)),
    state=dict(c=State(...))
)
def callback(b, c, a):
    return [a + b, b + c]

You can also specify the output of a callback function using named arguments. In this case the function is expected to return a dictionary with keys matching the keys in the dictionary passed to the output argument of @app.callback. For example:

@app.callback(
    output=dict(x=Output(...), y=Output(...)),
    inputs=dict(a=Input(...), b=Input(...)),
    state=dict(c=State(...))
)
def callback(b, c, a):
    return dict(x=a + b, y=b + c)

Interchangeable Input and State

Flexible Callback Signatures mean you can freely mix Input and State dependencies objects. This means that State dependencies can be included in the inputs argument, and Input dependencies can be included in the state argument. We recommended you put both Input and State dependencies in inputs rather than using the state keyword argument. For example:

@app.callback(
    output=dict(x=Output(...), y=Output(...)),
    inputs=dict(a=Input(...), b=Input(...), c=State(...)),
)
def callback(b, c, a):
    return dict(x=a + b, y=b + c)

Tuple and Dictionary Argument Grouping

You can also combine multiple Input and State dependency values into a single function argument.

Tuple Grouping

You can group dependency values in a tuple. Here the ab keyword function argument is a tuple consisting of the values of two Input dependency values.

@app.callback(
    output=[Output(...), Output(...)],
    inputs=dict(
        ab=(Input(...), Input(...)),
        c=Input(...)
    )
)
def callback(ab, c):
    a, b = ab
    return [a + b, b + c]

Or with positional indexing

@app.callback(
    output=[Output(...), Output(...)],
    inputs=[(Input(...), Input(...)), Input(...)]
)
def callback(ab, c):
    a, b = ab
    return [a + b, b + c]

Dictionary Grouping

Similarly, you can group multiple Input and State values together into a dictionary of values when passed to the function. Here, the ab argument is passed to the function as a dict containing "a" and "b" keys with values corresponding to the Input dependency values in the @app.callback specification.

@app.callback(
    output=[Output(...), Output(...)],
    inputs=dict(
        ab=dict(a=Input(...), b=Input(...)),
        c=Input(...)
    )
)
def callback(ab, c):
    a, b = ab["a"], ab["b"]
    return [a + b, b + c]

You can also nest these groupings arbitrarily deep.

@app.callback(
    output=[Output(...), Output(...)],
    args=dict(
        abc=dict(a=Input(...), b=(Input(...), Input(...)))
    )
)
def param_fn(abc):
    a, (b, c) = abc["a"], abc["b"]
    return [a + b, b + c]

Output Grouping

You can use the same tuple and dict groupings for the function output values as well.

Output Tuple Grouping

@app.callback(
    output=[Output(...), (Output(...), Output(...))],
    inputs=dict(
        a=Input(...),
        b=Input(...),
        c=Input(...),
    )
)
def callback(a, b, c):
    return [a, (a + b, b + c)]

Output Dict Grouping

@app.callback(
    output=[Output(...), dict(x=Output(...), y=Output(...))],
    inputs=dict(
        a=Input(...),
        b=Input(...),
        c=Input(...),
    )
)
def callback(a, b, c):
    return [a, dict(x=a+b, y=b+c)]

Limitations

Reference

app.callback API reference