Instantiation on Running Script

Hi,

Your question makes a lot of sense, and I will try to answer it to the best of my knowledge.

It is not possible to do it by importing the page submodule after the app starts. The app needs all callbacks defined when the process start, so even if you could (maybe?) import the layout from a different file on the fly, none of the callbacks n the page will work. Plus, this is a very bad idea.

Now, your concern about startup when fetching data from a DB is very much dependent on how you intend to deploy the application. If your deployment environment automatically kills the process running the app when idle for some time (for example, Heroku with free dynos), then this can be a problem because it is very likely that the service will start the process when a user request the page. If the process rarely stops, then this is less of an issue.

The other side of this question is if the values that you are fetching from the DB could be updated after your application starts. If you fetch just when the app starts, it is of course not possible to update the dropdown options after that. The same is true for other values calculated at runtime: as an example, if you set a date picker value to be dt.datetime.today() and deploy the app, the selected value will still be the day the process started.

One simple alternative for the case of multi-page application is to update the dropdown values of each page when you load it on the layout using a callback. Since Dash will trigger all callbacks depending on a given input when the component is added to the layout, you can simply use the id or children of a static element, like:

# page1.py

layout = html.Div(
    id="page1",
    children=[
        dcc.Dropdown(id="dd1"),
        dcc.Dropdown(id="dd2"),
    ]
)

@callback(
    Output("dd1", "options"),
    Output("dd2", "options"),
    Input("page1", "id")
)
def update_options(dummy):
     # Here you fetch from the DB
     return #....

More performant alternatives are:

You can use dcc.Store to save the dropdown options on the clientside, so if the user loads one of the pages, goes to another page and back, there is no need to fetch again from the DB. You can find multiple examples discussed in the community.

You can also consider server side caching, so the cache can be shared across multiple users. It might be harder to deploy the app in certain environments.

Hope this helps! :slight_smile:

1 Like