Hello,
I am not sure if my design makes any sense at this point, but I will go ahead and ask here before re-designing.
Essentially, I have a multipage app where I would like to have a single instance of a Data Access object (more or less), insofar as, it has multiple connections to databases, and if I can not have to make this class more than once (Singleton essentially), that would be ideal.
Please excuse any pythonic style issues, as I’m bouncing around languages and “styles” I realized that DataService should be more like data_service (etc).
app.py:
from dataservice import DataService
app = Dash(
__name__,
use_pages=True,
title='Data Plots',
external_stylesheets=[dbc.themes.BOOTSTRAP],
)
dataService = None
def get_dataservice():
if dataService is None:
dataService = DataService()
return dataService
else:
return dataServce
So, essentially, I have done an unnecessary getter as I am sort of in between implementations. Theoretically, I would only need to call app.dataService to get it (assuming I have a reference to my app.py)
In one of my Page(s).py I have some code as this:
left_select = dbc.Select(id="expid-dropdown", options=[{"label":x,"value":x} for x in **dash.get_app().get_dataservice()**.EXPIDS])
Eventually, I put that left_select in side the layout object (this is just an example, it’s not that actual implementation)
layout = html.Div(left_select)
As I read about the lifecycle of the dash app, it looks like I can’t quite get things in the right order or perhaps, I need to wrap everything in functions?
Either way, I receive the error when I try to start the app:
dbc.Select(id="expid-dropdown", options=[{"label":x,"value":x} for x in dash.get_app().get_dataservice().EXPIDS])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Dash' object has no attribute 'get_dataservice'
I didn’t see a lot of get_app() documentation, and I assumed it was a reference to the app.py.