My app is database backed, so for every record of one model there have to be same interface part generated, with 3 buttons each, 2 of them have to fire callback, but id’s of those buttons are unknown until page layout is generated (then database is queried for all ids).
I have created something factory-like to generate those callback:
def create_callbacks():
def delete(i):
def callback(*data):
instance = db_model.MyModel.query.get(i)
instance.delete()
return 'Removed'
return callback
def toggle(i):
def callback(*data):
instance = db_model.MyModel.query.get(i)
# some actions on instance
return str(instance)
return callback
try:
for model_id in [i for (i,) in db_model.MyModel.query.with_entities(db_model.MyModel.id).all()]:
try:
app.callback(Output('model-{}'.format(model_id), 'children'),
events=[Event('model-{}-delete-button'.format(model_id), 'click')])(delete(model_id))
app.callback(Output('model-{}-markdown'.format(model_id), 'children'),
events=[Event('model-{}-toggle-button'.format(model_id), 'click')])(toggle(model_id))
except CantHaveMultipleOutputs:
continue
except OperationalError:
# log db error
That function gets called inside of, as it is multi-page app:
@app.callback(
Output('page', 'children'),
[Input('location', 'pathname')])
def display_content(pathname):
# some ifs
create_callbacks()
content = create_content() # create_content() generates copies of one div for every instance putting instance id in button's id
return content
When I checked app.callbacks_map
for their existence, those callbacks were present, with proper components ids. But buttons do nothing on first page load, I have to reload it for buttons to work as intended.
Any ideas what can cause that behavior?