33
44from django .urls import reverse
55
6+ from .app_name import app_name
7+
68uid_counter = 0
79
810usable_apps = {}
911app_instances = {}
1012nd_apps = {}
1113
1214def get_app_by_name (name ):
15+ '''
16+ Locate a registered dash app by name, and return a DelayedDash instance encapsulating the app.
17+ '''
1318 return usable_apps .get (name ,None )
1419
1520def get_app_instance_by_id (id ):
21+ '''
22+ Locate an instance of a dash app by identifier, or return None if one does not exist
23+ '''
1624 return nd_apps .get (id ,None )
1725
26+ def get_or_form_app (id , name , ** kwargs ):
27+ '''
28+ Locate an instance of a dash app by identifier, loading or creating a new instance if needed
29+ '''
30+ app = get_app_instance_by_id (id )
31+ if app :
32+ return app
33+ dd = get_app_by_name (name )
34+ return dd .form_dash_instance ()
35+
36+ class Holder :
37+ def __init__ (self ):
38+ self .items = []
39+ def append_css (self , stylesheet ):
40+ self .items .append (stylesheet )
41+ def append_script (self , script ):
42+ self .items .append (script )
43+
1844class DelayedDash :
1945 def __init__ (self , name = None , ** kwargs ):
2046 if name is None :
@@ -24,27 +50,27 @@ def __init__(self, name=None, **kwargs):
2450 else :
2551 self ._uid = name
2652 self .layout = None
27- self ._rep_dash = None
2853 self ._callback_sets = []
2954
55+ self .css = Holder ()
56+ self .scripts = Holder ()
57+
3058 global usable_apps
3159 usable_apps [self ._uid ] = self
3260
33- def _RepDash (self ):
34- if self ._rep_dash is None :
35- self ._rep_dash = self ._form_repdash ()
36- return self ._rep_dash
37-
38- def _form_repdash (self ):
61+ def form_dash_instance (self ):
3962 rd = NotDash (name_root = self ._uid ,
40- app_pathname = "django_plotly_dash :main" )
63+ app_pathname = "%s :main" % app_name )
4164 rd .layout = self .layout
65+
4266 for cb , func in self ._callback_sets :
4367 rd .callback (** cb )(func )
44- return rd
68+ for s in self .css .items :
69+ rd .css .append_css (s )
70+ for s in self .scripts .items :
71+ rd .scripts .append_script (s )
4572
46- def base_url (self ):
47- return self ._RepDash ().base_url ()
73+ return rd
4874
4975 def callback (self , output , inputs = [], state = [], events = []):
5076 callback_set = {'output' :output ,
@@ -92,12 +118,12 @@ def __init__(self, name_root, app_pathname, **kwargs):
92118
93119 kwargs ['url_base_pathname' ] = self ._base_pathname
94120 kwargs ['server' ] = self ._notflask
121+
95122 super (NotDash , self ).__init__ (** kwargs )
96123 global nd_apps
97124 nd_apps [self ._uid ] = self
98- if False : # True for some debug info and a load of errors...
99- self .css .config .serve_locally = True
100- self .scripts .config .serve_locally = True
125+
126+ self ._adjust_id = False
101127
102128 def flask_app (self ):
103129 return self ._flask_app
@@ -123,10 +149,13 @@ def locate_endpoint_function(self, name=None):
123149
124150 @Dash .layout .setter
125151 def layout (self , value ):
126- self ._fix_component_id (value )
152+
153+ if self ._adjust_id :
154+ self ._fix_component_id (value )
127155 return Dash .layout .fset (self , value )
128156
129157 def _fix_component_id (self , component ):
158+
130159 theID = getattr (component ,"id" ,None )
131160 if theID is not None :
132161 setattr (component ,"id" ,self ._fix_id (theID ))
@@ -137,6 +166,8 @@ def _fix_component_id(self, component):
137166 pass
138167
139168 def _fix_id (self , name ):
169+ if not self ._adjust_id :
170+ return name
140171 return "%s_-_%s" % (self ._uid ,
141172 name )
142173
0 commit comments