What are Callback Functions?

  • Callback functions are used to define the behavior of your app in response to user interactions, such as selecting an option from a dropdown menu or checkbox, and displaying the appropriate output based on those interactions.
  • They are Python functions that automatically get called by Dash. This happens whenever its input components property changes, then it updates its output components property with the return value of the function.

Component Properties:

  • Dash components have properties such as:
    • children of html.Div() or html.H1()
    • options and value of dcc.Dropdown()
    • options and value of dcc.Checklist()
    • figure of dcc.Graph()

Callback Syntax:

  • In the below syntax, Input() is used to specify the input component and its property that will trigger the callback when it changes. The Output() function is used to specify the output component and its property that will be updated with the return value of the callback function.
python
1@app.callback(
2	Output(component_id, component_property),
3	Input(component_id, component_property)
4)
5def function_name(input_object):
6	# Do something with the input object, such as:
7	# Groupby, filter, sort_values or manipulate the input data
8	# Call plotly.express to create a figure object
9	return output_object