I'm building a dashboard in Dash, where I want to visualie a dataset that I load at the start of the dashboard. As this dataset is to be used by all methods in the dashboard, I keep it in a singleton class.
The issue is though that the data is loaded twice when I start the dashboard ("generated" is shown twice in the console when I start the dashboard). Here is a MWE showing the problem:
app.py
from dash import Dash, dcc, html
from callbacks import get_callbacks
from data import MyData
app = Dash(__name__, suppress_callback_exceptions=True)
get_callbacks(app) #load all callbacks
m = MyData() #load data at start of the dashboard -- somehow, this happens twice?
if __name__ == '__main__':
app.layout = html.Div([dcc.Location(id="url")])
app.run_server(debug=True)
data.py
import pandas as pd
def generate_df():
print("generated")
return pd.DataFrame({'a':[1,2,3]})
class MyData:
def __new__(cls):
if not hasattr(cls, 'instance'):
cls.instance = super(MyData, cls).__new__(cls)
return cls.instance
def __init__(self):
self.df = generate_df()
callbacks.py
def get_callbacks(app):
pass
Where does this additional load of data happen?