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.
- database query result lists / cards
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.
What I’d like to have in the end is a very simple app.py file such as this one below:
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(
Jumbotron(P('This is example text'))
if __name__ == "__main__":
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):
That works like a charm in the end
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 (
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.