@@ -100,11 +100,11 @@ def __init__(self, name=None, serve_locally=False,
100
100
bootstrap_source = css_url ()['href' ]
101
101
self .css .append_script ({'external_url' :[bootstrap_source ,]})
102
102
103
- def as_dash_instance (self ):
103
+ def as_dash_instance (self , cache_id = None ):
104
104
'''
105
105
Form a dash instance, for stateless use of this app
106
106
'''
107
- return self .do_form_dash_instance ()
107
+ return self .do_form_dash_instance (cache_id = cache_id )
108
108
109
109
def handle_current_state (self ):
110
110
'Do nothing impl - only matters if state present'
@@ -116,7 +116,7 @@ def have_current_state_entry(self, wid, key):
116
116
'Do nothing impl - only matters if state present'
117
117
pass
118
118
119
- def get_base_pathname (self , specific_identifier ):
119
+ def get_base_pathname (self , specific_identifier , cache_id ):
120
120
'Base path name of this instance, taking into account any state or statelessness'
121
121
if not specific_identifier :
122
122
app_pathname = "%s:app-%s" % (app_name , main_view_label )
@@ -125,13 +125,21 @@ def get_base_pathname(self, specific_identifier):
125
125
app_pathname = "%s:%s" % (app_name , main_view_label )
126
126
ndid = specific_identifier
127
127
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 + '/'
129
137
return ndid , full_url
130
138
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 ):
132
140
'Perform the act of constructing a Dash instance taking into account state'
133
141
134
- ndid , base_pathname = self .get_base_pathname (specific_identifier )
142
+ ndid , base_pathname = self .get_base_pathname (specific_identifier , cache_id )
135
143
return self .form_dash_instance (replacements , ndid , base_pathname )
136
144
137
145
def form_dash_instance (self , replacements = None , ndid = None , base_pathname = None ):
@@ -242,16 +250,24 @@ def use_dash_layout(self):
242
250
'''
243
251
return self ._use_dash_layout
244
252
245
- def augment_initial_layout (self , base_response ):
253
+ def augment_initial_layout (self , base_response , initial_arguments = None ):
246
254
'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 :
248
256
return base_response .data , base_response .mimetype
257
+
249
258
# Adjust the base layout response
250
259
baseDataInBytes = base_response .data
251
260
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
+
252
267
# Walk tree. If at any point we have an element whose id
253
268
# 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
+
255
271
response_data = json .dumps (reworked_data ,
256
272
cls = PlotlyJSONEncoder )
257
273
@@ -274,7 +290,7 @@ def walk_tree_and_extract(self, data, target):
274
290
for element in data :
275
291
self .walk_tree_and_extract (element , target )
276
292
277
- def walk_tree_and_replace (self , data ):
293
+ def walk_tree_and_replace (self , data , overrides ):
278
294
'''
279
295
Walk the tree. Rely on json decoding to insert instances of dict and list
280
296
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):
285
301
# look for id entry
286
302
thisID = data .get ('id' , None )
287
303
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 , {})
289
307
# walk all keys and replace if needed
290
308
for k , v in data .items ():
291
309
r = replacements .get (k , None )
292
310
if r is None :
293
- r = self .walk_tree_and_replace (v )
311
+ r = self .walk_tree_and_replace (v , overrides )
294
312
response [k ] = r
295
313
return response
296
314
if isinstance (data , list ):
297
315
# 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 ]
299
317
return data
300
318
301
319
def flask_app (self ):
@@ -328,7 +346,7 @@ def locate_endpoint_function(self, name=None):
328
346
# pylint: disable=no-member
329
347
@Dash .layout .setter
330
348
def layout (self , value ):
331
- 'Overloaded layoyt function to fix component names as needed'
349
+ 'Overloaded layout function to fix component names as needed'
332
350
333
351
if self ._adjust_id :
334
352
self ._fix_component_id (value )
0 commit comments