A little late to the party but I have found this to be a straightforward way of doing it:
- You create a separate script called ‘callbacks.py’ (for example)
- Define a function within callbacks.py which takes a dash.Dash object (i.e. the app) as a parameter (you can of course pass more arguments if necessary) and within which you define all your callbacks as you normally would in the main script:
def get_callbacks(app):
@app.callback([Output("figure1", "figure")],
[Input("child1", "value")])
def callback1(figure):
return
@app.callback([Output("figure2", "figure")],
[Input("child2", "value")])
def callback2(figure):
return
- Within the main script, simply import the function and call it after instantiating the dash.Dash object passing the same object into it:
import dash
from callbacks import get_callbacks
import layout
app = dash.Dash(__name__)
app.layout = layout.layout
get_callbacks(app)