Pattern-Matching outputs

Hi guys,

I’ve been trying to improve on the code that I had written and I’m trying to implement a pattern-matching callbacks since I believe this would be an elegant solution to the code that I had written prior. I have also asked about the implementation of my current code in another Dash python topic here.

I have dynamic childrens outputs, each with its own value, description, unit and style and the input is a nested dict that is saved on a dcc.Store. The name of the children’s ids are quite similar, with only the numbering of the ids that make each of them differ than another.

The way I did before was to loop the whole callbacks and then the outputs will be determined based on the iteration of the inputs from a list of keys of the nested dict.

for i in range(7):
    @app.callback(
        [Output(f'gd{i+1}'.format(i+1), 'children'),
         Output(f'gd{i+1}-value'.format(i+1), 'children'),
         Output(f'gd{i+1}-unit'.format(i+1), 'children')],
        Input('ret_data', 'data')
    )
    def glob_output(data, k=i):  #k = i so that the output will have the right input from the loop iteration
        results = simulation_store(**data)
        g = results[0]
        gl = list(g.keys())
        val, desc, unit = g[gl[k]]['value'],  gl[k],  g[gl[k]]['units']

        return val, desc, unit

So for different children, a different value and units and description would be retrieved from the nested dict which could be determined by the iteration of the loop.

This is what I have for now for the latest implementation (definitely not complete)

@app.callback(
    [Output({'type': 'global_children', 'id': ALL}, 'children'),  #id naming convention remains the same (gd1, gd2 etc)
     Output({'type': 'global_value', 'id': ALL}, 'children'),     #(gd1-value, gd2-value etc....)
     Output({'type': 'global_unit', 'id': ALL}, 'children'),      #(gd1-unit etc.....)
    Input('ret_data', 'data')
)
def glob_outputs(data): .....

The problem I’m facing currently is how am I going to implement the pattern-matching callbacks to my current code since I’m not sure how to make sure that outputs would not be the same for every value and unit (children). Before I can just depend on the way Python scopes work, but with pattern-matching callback it is still not clear to me!

Hope that the question is clear to everyone. Thank you again for your time!

Btw, I have found out that the output should be a list instead of a single value, and I can also make them having the same id for different type. So basically the solution is kinda solved and it was due to the lack of knowledge of pattern-matching callbacks on my part :sweat_smile: