Suggestions on how to separate call backs in to their own py file?

After a day of really digging in, I’ve found that trying to build views using the structure shown in the examples gets really messy and with all the nesting it’s really easy to break.

I’m in the process of splitting up my views in to different py scripts to manage different parts of the page.

  • page_layout
  • navigation
  • forms
  • data_tables
  • database query result lists / cards
  • callbacks

I would appreciate suggestions on how to split callbacks to it’s own py file. I know how to do this with objects & functions but I’m not sure how to handle this since it’s using decorators.

For instance, how would you populate the following files, so that you can import a reactive form objective in to app.py.

./forms
init.py
my_request_form.py
my_request_form_callbacks.py

app.py

What I’d like to have in the end is a very simple app.py file such as this one below:

import dash
import dash_bootstrap_components as dbc
from dash_bootstrap_components import Jumbotron
import dash_html_components as html
from dash_html_components import Br, P
from navigation.top import TopNav #this is working
from components.forms.rfs_new import active_form #this isnt

app = dash.Dash()

app.layout = html.Div(
    [
        TopNav(),
        Br(),
        Container(
            Jumbotron(P('This is example text'))
        ),
        Container(
            Jumbotron(active_form())
        )
    ]
)  


if __name__ == "__main__":
    app.run_server(port=8888, debug=True)

This is totally possible and even advise for big projects.

You can just put the callbacks in another file, just remember to import the app

from init import app

where you’ll have your callbacks, and to import the callback files where you’ll have your layout, in init for you (in my case, I also split the layout apart to make it easier to read):

import my_request_form_callbacks

That works like a charm in the end

1 Like

I figured it would be possible. I get where you’re going but not sure I’ll be able to fill in all the gaps. Would you expand on this and provide a basic skeleton example?

See the section on “Structuring a Multipage App” in this section of the Dash Docs. It shows you how you can move both layout and callbacks to different modules, but the same approach works for moving just callbacks to a separate file.

The main trick is that your entry point into the app that Python is running (ie the file that contains app.run_server) can’t both define a Dash app instance and import a script that needs that reference to app (for callbacks say). Because the imports will try to happen before the app instance is created, and will then break. This is why the example pushes creation of the Dash app instance into a separate app module, so it can be imported directly before the modules that require this attribute to be defined (app1 and app2).

I had glanced over this in the doc but I missed it, must have been later in the day. Thanks for pointing in me in the right direction.

1 Like