I have a multipage layout, so I have callback exceptions suppressed, and I generate the callbacks when needed.
Let’s say I have a dropdown that modifies a set of components. I can generate multiple callbacks (one for each of a certain component) like this:
def gen_callback(css_id, x):
@app.callback(
Output(css_id, 'children'),
Input('my-dropdown', 'value'),
background=True,
)
def callback_name(value):
print(f"Inside callback_name {self._css_id} for {org_id}")
return int(value) + x
gen_callback
will be called before server initialization, when the components are created.
The problem is, when I change the dropdown value, it changes ALL COMPONENTS to be like the last one that was registered.
This is because the hash_function of the CallbackManager takes into account just the code of the function
@staticmethod
def hash_function(fn):
fn_source = inspect.getsource(fn)
fn_str = fn_source
return hashlib.sha1(fn_str.encode("utf-8")).hexdigest()
And the code is the same among all of the callbacks. Nevertheless, the css_id of the output is not the same among all the callbacks!
Is there any solution for this? How should I register the callbacks then? Should I compile the function (changing the name, for example) before registering it?
Note: I’d want to avoid using multiple outputs, because each callback takes some time and I want them to be run separately in a celery worker.