I am trying to implement tests for Dash callbacks. I am using flexible callback signature, so that I have grouped inputs to callback function. Something like code snippet below:
There seems to be a logic error in the post, the all_inputs is not used in the function and then it calls the function with multiple argument while it only takes one argument.
The original code in the docs is:
@callback(Output('container','children'),
Input('btn-1', 'n_clicks'),
Input('btn-2', 'n_clicks'),
Input('btn-3', 'n_clicks'))
def display(btn1, btn2, btn3):
button_clicked = ctx.triggered_id
return f'You last clicked button with ID {button_clicked}'
But here it changed to use arg_groupings which uses another context var that needs mocking. The call needs to be changed to something like {'btn1': 1}
I never really used arg_grouping, so I’m not sure how it works, and I can’t recommend using it in tests since it seems to require a bunch of stuff.
@callback(Output('container','children'),
Input('btn-1', 'n_clicks'),
Input('btn-2', 'n_clicks'),
Input('btn-3', 'n_clicks'))
def display(btn1, btn2, btn3):
button_clicked = ctx.triggered_id
return f'You last clicked button with ID {button_clicked}'
But, the thing is that at company I work for, we have a lot of legacy code that was written using Dash flexible callback, and we need to write unittests for them now. Rewriting all callbacks would be very difficult at this stage. So, I am looking for way to test Dash callbacks with flexible signatures…
This is type of callback that we have in our Dash project. I know, it is possible to rewrite callback in a way that it does not use flexible callback signatures, but it will require big refactoring of project. Almost all callbacks in project that I work on are written using flexible callback signatures.
I am curious how could I write unittest for display() callback here? I know that callback context should be mocked, but I have no idea how to do it (because mocking should be done differently than this example shows Dash Testing | Dash for Python Documentation | Plotly . This example is for mocking callback context of simple callback, not one with flexible callback signatures).