Skip to content

List index out of range with dynamic callback args #382

@sytham

Description

@sytham

It seems dash-extensions does not allow dynamic callback args while the "regular" dash does.

Minimal example to reproduce the issue:

from dash import html, dcc
from dash_extensions.enrich import Dash, Output, Input, State, Trigger

app = Dash(__name__)

app.layout = html.Div([
    dcc.Input(id='input-1', type='number', value=0, placeholder="Enter first number"),
    dcc.Input(id='input-2', type='number', value=0, placeholder="Enter second number"),
    html.Button("Calc", id='calc-button', n_clicks=0),
    html.Div(id='output', style={'marginTop': '20px', 'fontSize': '20px'})
])

# Dynamically create a list of State dependencies for the two input fields.
input_ids = ['input-1', 'input-2']
states = [State(comp_id, 'value') for comp_id in input_ids]

@app.callback(Output('output', 'children'),
            Input('calc-button', 'n_clicks'),
            *states
            )
def update_output(*values):
    try:
        total = sum((float(v) if v is not None else 0) for v in values[1:])
    except ValueError:
        total = "Invalid input"
    return f"Sum: {total}"

if __name__ == '__main__':
    app.run(debug=True, port=8057, host='0.0.0.0')

When running this I get

Traceback (most recent call last):
  File "../py3.9_venv/lib/python3.9/site-packages/dash_extensions/enrich.py", line 1055, in decorated_function
    an = full_arg_spec.annotations.get(full_arg_spec.args[i])
IndexError: list index out of range

When importing Dash from dash instead of from dash_extensions.enrich, the app runs fine.
The issue seems to be that inspect.getfullargspec() sees an empty list of args when the arguments are defined as *args, as update_output does.

I'm on python 3.9 and dash-extensions 1.0.11, but from looking at the dash-extensions code, the issue is present in later versions as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions