dcc.Store query

Hi All,

I have a query before i entirely break my app, the question relates to the dcc.Store functionality.

Currently I have some nasty callbacks which run through mass lines of IF statements due to the multiple inputs - for example this is how one of my callbacks inputs and outputs looks:

    Output("g1", "children"),
    Output("g1", "className"),
    Output("g2", "children"),
    Output("g2", "className"),
    Output("gcol", "className"),
    Output("gcol2", "className"),
    Input("name", "value"),
    Input("name2", "value"),
    Input("v_name", "value"),
    Input("v_name2", "value"),
    Input("l_name", "value"),
    Input("l_name2", "value"),
    Input("t", "value"),
    Input("t2", "value"),

Now this callback does if checks on every single combination of those inputs and this results in a callback that is over 2000 lines long - which is ridiculous.

I want to refactor this and first question is: Is dcc.Store the right/best way to refactor it?

Secondly the idea for the dcc.Store utilization would be the following:

  1. Create a dcc.Store for v_name which makes use of the name Dataframe
  2. Create a dcc.Store for l_name which makes use of a dataframe created out of step 1 above
  3. Create a dcc.Store for t which makes use of a dataframe created out of step 2 above
  4. replace all inputs on the above callback with the dataframe created out of step 3 above therefore eliminating all IF loop combos on the callback and taking it from 2000 lines to roughly 20-30

Can someone advise if this is best practise/the right way to do it and also could you perhaps provide a simple example of how to move from step 1 to step 2 - would be much appreciated!

1 Like

Hello @DrSmith69,

That could be a way, or you might be able to use Pattern-Matching:

Pattern-matching would allow you to pull all the values necessary and you could set up different combos or whatnot for your other functions.

It’s hard to really say without seeing a ton of what you are trying to accomplish.

Yeah I know it is tough without seeing how it all functions - only problem with the matching etc is that all of my inputs can either work in conjunction or as standalone inputs, so there is a potentially for all of them to be None and therefore the Data Frame remains untouched, or 1 of them could be chosen which limits the DF or even 2 could be chosen, plus they are also all dynamic off of each other so if I select and option from 1 it limits the options in the 2nd one and vice versa

I think that you can probably manage it through pattern-matching as well.

Either way, is going to be tricky.

One of my favorite ways of dealing with stuff is by creating a dictionary of the opts and functions.

myDict = {'opt1': func1(), 'opt2': func2(), 'criteria1': func3()}

Then instead of using if statements, you just return the function that matches the criteria by:

return myDict[opts]

Also, another tip, if you set the function up to be a variable, you can pass arguments to it like you are calling the function:

myFunc = myDict[opts]

return myFunc(*args)