33
44from django .urls import reverse
55from django .http import HttpResponse
6+ from django .utils .text import slugify
67
78import json
89
1314uid_counter = 0
1415
1516usable_apps = {}
16- nd_apps = {}
1717
1818def add_usable_app (name , app ):
19+ name = slugify (name )
1920 global usable_apps
2021 usable_apps [name ] = app
22+ return name
2123
22- def add_instance (id , instance ):
23- global nd_apps
24- nd_apps [id ] = instance
25-
26- def get_app_by_name (name ):
27- '''
28- Locate a registered dash app by name, and return a DelayedDash instance encapsulating the app.
29- '''
30- return usable_apps .get (name ,None )
31-
32- def get_app_instance_by_id (id ):
24+ def get_stateless_by_name (name ):
3325 '''
34- Locate an instance of a dash app by identifier, or return None if one does not exist
26+ Locate a registered dash app by name, and return a DjangoDash instance encapsulating the app.
3527 '''
36- return nd_apps .get (id ,None )
37-
38- def clear_app_instance (id ):
39- try :
40- del nd_apps [id ]
41- except :
42- pass
43-
44- def get_or_form_app (id , name , ** kwargs ):
45- '''
46- Locate an instance of a dash app by identifier, loading or creating a new instance if needed
47- '''
48- app = get_app_instance_by_id (id )
49- if app :
50- return app
51- dd = get_app_by_name (name )
52- return dd .form_dash_instance ()
28+ name = slugify (name )
29+ # TODO wrap this in raising a 404 if not found
30+ return usable_apps [name ]
5331
5432class Holder :
5533 def __init__ (self ):
@@ -59,7 +37,7 @@ def append_css(self, stylesheet):
5937 def append_script (self , script ):
6038 self .items .append (script )
6139
62- class DelayedDash :
40+ class DjangoDash :
6341 def __init__ (self , name = None , ** kwargs ):
6442 if name is None :
6543 global uid_counter
@@ -78,9 +56,30 @@ def __init__(self, name=None, **kwargs):
7856
7957 self ._expanded_callbacks = False
8058
59+ def as_dash_instance (self ):
60+ '''
61+ Form a dash instance, for stateless use of this app
62+ '''
63+ return self .form_dash_instance ()
64+
65+ def handle_current_state (self ):
66+ 'Do nothing impl - only matters if state present'
67+ pass
68+ def update_current_state (self , wid , key , value ):
69+ 'Do nothing impl - only matters if state present'
70+ pass
71+ def have_current_state_entry (self , wid , key ):
72+ 'Do nothing impl - only matters if state present'
73+ pass
74+
8175 def form_dash_instance (self , replacements = None , specific_identifier = None ):
76+ if not specific_identifier :
77+ app_pathname = "%s:app-%s" % (app_name , main_view_label )
78+ else :
79+ app_pathname = "%s:%s" % (app_name , main_view_label )
80+
8281 rd = NotDash (name_root = self ._uid ,
83- app_pathname = "%s:%s" % ( app_name , main_view_label ) ,
82+ app_pathname = app_pathname ,
8483 expanded_callbacks = self ._expanded_callbacks ,
8584 replacements = replacements ,
8685 specific_identifier = specific_identifier )
@@ -134,8 +133,6 @@ def __init__(self, name_root, app_pathname=None, replacements = None, specific_i
134133 else :
135134 self ._uid = name_root
136135
137- add_instance (self ._uid , self )
138-
139136 self ._flask_app = Flask (self ._uid )
140137 self ._notflask = NotFlask ()
141138 self ._base_pathname = reverse (app_pathname ,kwargs = {'id' :self ._uid })
@@ -279,20 +276,27 @@ def dispatch_with_args(self, body, argMap):
279276
280277 target_id = '{}.{}' .format (output ['id' ], output ['property' ])
281278 args = []
282- for component_registration in self .callback_map [target_id ]['inputs' ]:
283- args .append ([
284- c .get ('value' , None ) for c in inputs if
285- c ['property' ] == component_registration ['property' ] and
286- c ['id' ] == component_registration ['id' ]
287- ][0 ])
288-
289- for component_registration in self .callback_map [target_id ]['state' ]:
290- args .append ([
291- c .get ('value' , None ) for c in state if
292- c ['property' ] == component_registration ['property' ] and
293- c ['id' ] == component_registration ['id' ]
294- ][0 ])
295279
296- return self . callback_map [ target_id ][ 'callback' ]( * args , ** argMap )
280+ da = argMap . get ( 'dash_app' , None )
297281
282+ for component_registration in self .callback_map [target_id ]['inputs' ]:
283+ for c in inputs :
284+ if c ['property' ] == component_registration ['property' ] and c ['id' ] == component_registration ['id' ]:
285+ v = c .get ('value' ,None )
286+ args .append (v )
287+ if da : da .update_current_state (c ['id' ],c ['property' ],v )
298288
289+ for component_registration in self .callback_map [target_id ]['state' ]:
290+ for c in state :
291+ if c ['property' ] == component_registration ['property' ] and c ['id' ] == component_registration ['id' ]:
292+ v = c .get ('value' ,None )
293+ args .append (v )
294+ if da : da .update_current_state (c ['id' ],c ['property' ],v )
295+
296+ res = self .callback_map [target_id ]['callback' ](* args ,** argMap )
297+ if da and da .have_current_state_entry (output ['id' ], output ['property' ]):
298+ response = json .loads (res .data .decode ('utf-8' ))
299+ value = response .get ('response' ,{}).get ('props' ,{}).get (output ['property' ],None )
300+ da .update_current_state (output ['id' ], output ['property' ], value )
301+
302+ return res
0 commit comments