Bring Drag & Drop to Dash with Dashboard Engine. 💫 Learn how at our next webinar!

Dash Multi Page App: Functions are called twice, unintentionally

I am working on a Dash multi page App and have the issue that my functions are called twice, when the App is started. In the directory temp.data_sources I have multiple files, each one for different datasources. Then in the file I call these functions and combine the output to a single df. This df is then imported in the individual apps inside the temp.apps directory.

The following is an example just for this question, leaving all the unnecessary stuff out for simplicity.


import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

from app import app
from temp.apps import page_one

app.layout = html.Div([
    dcc.Location(id='url', refresh=False),

@app.callback(Output('page-content', 'children'),
              [Input('url', 'pathname')])
def display_page(pathname):
    if pathname == '/':
        return page_one.layout
        return '404'

if __name__ == '__main__':

from import create_df

df = create_df()

import pandas as pd

def create_df():
    data = [['tom', 10], ['nick', 15], ['juli', 14]]
    df = pd.DataFrame(data, columns=['Name', 'Age'])
    print('create_df was called')
    return df

import dash_html_components as html
import dash_core_components as dcc
import dash_table
import plotly.graph_objects as go

from app import app
from temp.apps import commonmodules
from import df

layout = html.Div([
            columns=[{"name": i, "id": i} for i in ['Name', 'Age']],

When I start the App by running the files, it leads to the following being displayed in the console. As you can see it calls the function create_df twice.


create_df was called
Dash is running on
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
create_df was called

What do I have to change to avoid calling the function twice?
Thank you very much!

Hi @HansPeter123! I believe this snippet from the documentation on our dev tools might help explain what is happening here:

The Code Reloading feature is provided by Flask & Werkzeug via the use_reloader keyword. A caveat of Code Reloading is that your app code is run twice when starting: once to start the parent process and another time to run the child process that gets reloaded.

Hope this helps!