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 temp.data.py
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.
temp
apps
commonmodules.py
page_one
data_sources
data.py
app.py
data.py
index.py
temp.index.py
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),
html.Div(id='page-content')
])
@app.callback(Output('page-content', 'children'),
[Input('url', 'pathname')])
def display_page(pathname):
if pathname == '/':
return page_one.layout
else:
return '404'
if __name__ == '__main__':
app.run_server(debug=True)
temp.data.py
from temp.data_sources.data import create_df
df = create_df()
temp.data_sources.data.py
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
temp.apps.page_one.py:
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 temp.data import df
layout = html.Div([
html.Div([
dash_table.DataTable(
id='table',
columns=[{"name": i, "id": i} for i in ['Name', 'Age']],
data=df.to_dict('records'),
)
])
])
When I start the App by running the index.py
files, it leads to the following being displayed in the console. As you can see it calls the function create_df
twice.
console:
create_df was called
Dash is running on http://127.0.0.1:8050/
* 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!