Dash Multi-App do not run pages to initiate

Hi

I have set-up a dash multi-app server and I have a bunch of analytics pages that do a lot of heavy lifting when they are run.
Problem I have is that when I start a server all the pages seems to be loading or atleast running and doing the calcs before the server is up and running fully.

How can I avoid that and only have the page fully run when its called? My app initialization code is as below

Any help would be appreciated. And apologies in advance if this has been asked/answered somewhere already.

import dash
from dash import dcc

from dash import html, page_registry
import dash_bootstrap_components as dbc

# To use Font Awesome Icons
FA621 = "https://use.fontawesome.com/releases/v6.2.1/css/all.css"
APP_TITLE = "First Dash App"

app = dash.Dash(
    __name__,
    suppress_callback_exceptions=True,
    external_stylesheets=[
        dbc.themes.SPACELAB,  # Dash Themes CSS
        FA621,  # Font Awesome Icons CSS
    ],
    title=APP_TITLE,
    use_pages=True,  # New in Dash 2.7 - Allows us to register pages
)

NAVBAR = create_navbar()
# To use Font Awesome Icons
FA621 = "https://use.fontawesome.com/releases/v6.2.1/css/all.css"
APP_TITLE = "My Analytic Pages"

sidebar = dbc.NavbarSimple(
    [dbc.NavLink(pg['title'], href=pg['path']) for pg in dash.page_registry.values()]
)

app.layout = html.Div([sidebar, dcc.Loading(dash.page_container)])

server = app.server



def create_navbar():
    navbar = dbc.NavbarSimple(

        children=[  # Add as many menu items as you need
            dbc.DropdownMenu(
                [
                    dbc.DropdownMenuItem(page["name"], href=page["path"])
                    for page in page_registry.values()

                ],
                nav=True,
                label="Menu",
            ),
        ],
        #    ),
        # ],
        brand='Home',
        brand_href="/",
        color="dark",
        dark=True,
    )

    return navbar

if __name__ == '__main__':
    app.run_server(debug=False)

HI @VolGuy welcome to the forums.

You could wrap the heavy lifting in a function and call this function whenever needed.

Thanks @AIMPED . This seems to work indeed.

I have created a doHeavyLifting() function and calling it within def layout() function.
SO that I am clear initial server run is only trying to compile the pages and checking for any errors rather than calling for the layout of the function?
SO the doHeavyLifting() will only run when the layout is called as part of accessing the page url?

Hello @VolGuy,

The function is one step, an additional thing I do is to check if there is a flask.request.

def layout():
    if not flask.request:
        return html.Div()
    # rest of code here

This will keep it from trying to load this layout initially when the app is spinning up, but will cater it like normal when requested.

Interestin @jinnyzor do you mind elaborating a bit?

When the app spins up, it goes through and runs what it can. This is outside the context of an actual request, therefore when you check for it and it doesn’t exist, then it will exit the return the empty div.

Beautiful…this
if not flask.request:

really sped up the initial startup

1 Like