With a change that went into Dash recently, it’s possible now to introspect details of registered callback functions. I’ve taken advantage of this to make a function which prints out the details of all registered callbacks:
callback router @ router.py:25
Output dash-container.children
Inputs 1 url.pathname
States 0
Events 0
callback update_nav @ router.py:34
Output navbar.children
Inputs 1 url.pathname
States 0
Events 0
callback callback @ app.py:10
Output graph.figure
Inputs 3 normalize.value, sort-type.value, text-input.value
States 0
Events 0
This is particularly useful for larger apps where it can be harder to keep track of callbacks, especially if they are being created in bulk. It’s also helpful for debugging the callback logic, where the first thing I want to do when reading someone else code is work out what callbacks are defined and what their input and output targets are, before working out what they return. If this is something people found useful, I could put together a PR.
def show_callbacks(app):
def format_regs(registrations, padding=10):
# TODO: -- switch to single line printing if > 79 chars
vals = sorted("{}.{}".format(i['id'], i['property'])
for i in registrations)
return ", ".join(vals)
output_list = []
for callback_id, callback in app.callback_map.items():
wrapped_func = callback['callback'].__wrapped__
inputs = callback['inputs']
states = callback['state']
events = callback['events']
str_values = {
'callback': wrapped_func.__name__,
'output': callback_id,
'filename': os.path.split(wrapped_func.__code__.co_filename)[-1],
'lineno': wrapped_func.__code__.co_firstlineno,
'num_inputs': len(inputs),
'num_states': len(states),
'num_events': len(events),
'inputs': format_regs(inputs),
'states': format_regs(states),
'events': format_regs(events)
}
output = """
callback {callback} @ {filename}:{lineno}
Output {output}
Inputs {num_inputs:>4} {inputs}
States {num_states:>4} {states}
Events {num_events:>4} {events}
""".format(**str_values)
output_list.append(output)
return "\n".join(output_list)