@@ -100,11 +100,11 @@ def __init__(self, name=None, serve_locally=False,
100100 bootstrap_source = css_url ()['href' ]
101101 self .css .append_script ({'external_url' :[bootstrap_source ,]})
102102
103- def as_dash_instance (self ):
103+ def as_dash_instance (self , cache_id = None ):
104104 '''
105105 Form a dash instance, for stateless use of this app
106106 '''
107- return self .do_form_dash_instance ()
107+ return self .do_form_dash_instance (cache_id = cache_id )
108108
109109 def handle_current_state (self ):
110110 'Do nothing impl - only matters if state present'
@@ -116,7 +116,7 @@ def have_current_state_entry(self, wid, key):
116116 'Do nothing impl - only matters if state present'
117117 pass
118118
119- def get_base_pathname (self , specific_identifier ):
119+ def get_base_pathname (self , specific_identifier , cache_id ):
120120 'Base path name of this instance, taking into account any state or statelessness'
121121 if not specific_identifier :
122122 app_pathname = "%s:app-%s" % (app_name , main_view_label )
@@ -125,13 +125,21 @@ def get_base_pathname(self, specific_identifier):
125125 app_pathname = "%s:%s" % (app_name , main_view_label )
126126 ndid = specific_identifier
127127
128- full_url = reverse (app_pathname , kwargs = {'ident' :ndid })
128+ kwargs = {'ident' : ndid }
129+
130+ if cache_id :
131+ kwargs ['cache_id' ] = cache_id
132+ app_pathname = app_pathname + "--args"
133+
134+ full_url = reverse (app_pathname , kwargs = kwargs )
135+ if full_url [- 1 ] != '/' :
136+ full_url = full_url + '/'
129137 return ndid , full_url
130138
131- def do_form_dash_instance (self , replacements = None , specific_identifier = None ):
139+ def do_form_dash_instance (self , replacements = None , specific_identifier = None , cache_id = None ):
132140 'Perform the act of constructing a Dash instance taking into account state'
133141
134- ndid , base_pathname = self .get_base_pathname (specific_identifier )
142+ ndid , base_pathname = self .get_base_pathname (specific_identifier , cache_id )
135143 return self .form_dash_instance (replacements , ndid , base_pathname )
136144
137145 def form_dash_instance (self , replacements = None , ndid = None , base_pathname = None ):
@@ -242,16 +250,24 @@ def use_dash_layout(self):
242250 '''
243251 return self ._use_dash_layout
244252
245- def augment_initial_layout (self , base_response ):
253+ def augment_initial_layout (self , base_response , initial_arguments = None ):
246254 'Add application state to initial values'
247- if self .use_dash_layout () and False :
255+ if self .use_dash_layout () and not initial_arguments and False :
248256 return base_response .data , base_response .mimetype
257+
249258 # Adjust the base layout response
250259 baseDataInBytes = base_response .data
251260 baseData = json .loads (baseDataInBytes .decode ('utf-8' ))
261+
262+ # Also add in any initial arguments
263+ if initial_arguments :
264+ if isinstance (initial_arguments , str ):
265+ initial_arguments = json .loads (initial_arguments )
266+
252267 # Walk tree. If at any point we have an element whose id
253268 # matches, then replace any named values at this level
254- reworked_data = self .walk_tree_and_replace (baseData )
269+ reworked_data = self .walk_tree_and_replace (baseData , initial_arguments )
270+
255271 response_data = json .dumps (reworked_data ,
256272 cls = PlotlyJSONEncoder )
257273
@@ -274,7 +290,7 @@ def walk_tree_and_extract(self, data, target):
274290 for element in data :
275291 self .walk_tree_and_extract (element , target )
276292
277- def walk_tree_and_replace (self , data ):
293+ def walk_tree_and_replace (self , data , overrides ):
278294 '''
279295 Walk the tree. Rely on json decoding to insert instances of dict and list
280296 ie we use a dna test for anatine, rather than our eyes and ears...
@@ -285,17 +301,19 @@ def walk_tree_and_replace(self, data):
285301 # look for id entry
286302 thisID = data .get ('id' , None )
287303 if thisID is not None :
288- replacements = self ._replacements .get (thisID , {})
304+ replacements = overrides .get (thisID , None ) if overrides else None
305+ if not replacements :
306+ replacements = self ._replacements .get (thisID , {})
289307 # walk all keys and replace if needed
290308 for k , v in data .items ():
291309 r = replacements .get (k , None )
292310 if r is None :
293- r = self .walk_tree_and_replace (v )
311+ r = self .walk_tree_and_replace (v , overrides )
294312 response [k ] = r
295313 return response
296314 if isinstance (data , list ):
297315 # process each entry in turn and return
298- return [self .walk_tree_and_replace (x ) for x in data ]
316+ return [self .walk_tree_and_replace (x , overrides ) for x in data ]
299317 return data
300318
301319 def flask_app (self ):
@@ -328,7 +346,7 @@ def locate_endpoint_function(self, name=None):
328346 # pylint: disable=no-member
329347 @Dash .layout .setter
330348 def layout (self , value ):
331- 'Overloaded layoyt function to fix component names as needed'
349+ 'Overloaded layout function to fix component names as needed'
332350
333351 if self ._adjust_id :
334352 self ._fix_component_id (value )
0 commit comments