@@ -172,15 +172,21 @@ def find_text(spec):
172
172
173
173
174
174
@pytest .mark .parametrize (
175
- "kwargs, expected_update_title" ,
175
+ "kwargs, expected_update_title, clientside_title " ,
176
176
[
177
- ({}, "Updating..." ),
178
- ({"update_title" : None }, "Dash" ),
179
- ({"update_title" : "" }, "Dash" ),
180
- ({"update_title" : "Hello World" }, "Hello World" ),
181
- ]
177
+ ({}, "Updating..." , False ),
178
+ ({"update_title" : None }, "Dash" , False ),
179
+ ({"update_title" : "" }, "Dash" , False ),
180
+ ({"update_title" : "Hello World" }, "Hello World" , False ),
181
+ ({}, "Updating..." , True ),
182
+ ({"update_title" : None }, "Dash" , True ),
183
+ ({"update_title" : "" }, "Dash" , True ),
184
+ ({"update_title" : "Hello World" }, "Hello World" , True ),
185
+ ],
182
186
)
183
- def test_rdls003_update_title (dash_duo , kwargs , expected_update_title ):
187
+ def test_rdls003_update_title (
188
+ dash_duo , kwargs , expected_update_title , clientside_title
189
+ ):
184
190
app = dash .Dash ("Dash" , ** kwargs )
185
191
lock = Lock ()
186
192
@@ -189,26 +195,110 @@ def test_rdls003_update_title(dash_duo, kwargs, expected_update_title):
189
195
html .H3 ("Press button see document title updating" ),
190
196
html .Div (id = "output" ),
191
197
html .Button ("Update" , id = "button" , n_clicks = 0 ),
198
+ html .Button ("Update Page" , id = "page" , n_clicks = 0 ),
199
+ html .Div (id = "dummy" ),
192
200
]
193
201
)
202
+ if clientside_title :
203
+ app .clientside_callback (
204
+ """
205
+ function(n_clicks) {
206
+ document.title = 'Page ' + n_clicks;
207
+ return 'Page ' + n_clicks;
208
+ }
209
+ """ ,
210
+ Output ("dummy" , "children" ),
211
+ [Input ("page" , "n_clicks" )],
212
+ )
194
213
195
- @app .callback (
196
- Output ("output" , "children" ),
197
- [Input ("button" , "n_clicks" )]
198
- )
214
+ @app .callback (Output ("output" , "children" ), [Input ("button" , "n_clicks" )])
199
215
def update (n ):
200
216
with lock :
201
217
return n
202
218
203
219
with lock :
204
220
dash_duo .start_server (app )
205
221
# check for update-title during startup
206
- until (lambda : dash_duo .driver .title == expected_update_title , timeout = 1 )
222
+ # the clientside callback isn't blocking so it may update the title
223
+ if not clientside_title :
224
+ until (lambda : dash_duo .driver .title == expected_update_title , timeout = 1 )
207
225
208
226
# check for original title after loading
209
- until (lambda : dash_duo .driver .title == "Dash" , timeout = 1 )
227
+ until (lambda : dash_duo .driver .title == "Page 0" if clientside_title else " Dash" , timeout = 1 )
210
228
211
229
with lock :
212
230
dash_duo .find_element ("#button" ).click ()
213
231
# check for update-title while processing callback
214
- until (lambda : dash_duo .driver .title == expected_update_title , timeout = 1 )
232
+ if clientside_title and not kwargs .get ('update_title' , True ):
233
+ until (lambda : dash_duo .driver .title == 'Page 0' , timeout = 1 )
234
+ else :
235
+ until (lambda : dash_duo .driver .title == expected_update_title , timeout = 1 )
236
+
237
+ if clientside_title :
238
+ dash_duo .find_element ("#page" ).click ()
239
+ dash_duo .wait_for_text_to_equal ("#dummy" , "Page 1" )
240
+ until (lambda : dash_duo .driver .title == "Page 1" , timeout = 1 )
241
+
242
+ # verify that when a separate callback runs, the page title gets restored
243
+ dash_duo .find_element ("#button" ).click ()
244
+ dash_duo .wait_for_text_to_equal ("#output" , "2" )
245
+ if clientside_title :
246
+ until (lambda : dash_duo .driver .title == "Page 1" , timeout = 1 )
247
+ else :
248
+ until (lambda : dash_duo .driver .title == "Dash" , timeout = 1 )
249
+
250
+
251
+ @pytest .mark .parametrize (
252
+ "update_title" ,
253
+ [
254
+ None ,
255
+ 'Custom Update Title' ,
256
+ ],
257
+ )
258
+ def test_rdls004_update_title_chained_callbacks (dash_duo , update_title ):
259
+ initial_title = 'Initial Title'
260
+ app = dash .Dash ("Dash" , title = initial_title , update_title = update_title )
261
+ lock = Lock ()
262
+
263
+ app .layout = html .Div (
264
+ children = [
265
+ html .Button (id = "page-title" , n_clicks = 0 , children = 'Page Title' ),
266
+ html .Div (id = "page-output" ),
267
+ html .Div (id = "final-output" )
268
+ ]
269
+ )
270
+ app .clientside_callback (
271
+ """
272
+ function(n_clicks) {
273
+ if (n_clicks > 0) {
274
+ document.title = 'Page ' + n_clicks;
275
+ }
276
+ return n_clicks;
277
+ }
278
+ """ ,
279
+ Output ("page-output" , "children" ),
280
+ [Input ("page-title" , "n_clicks" )],
281
+ )
282
+
283
+ @app .callback (
284
+ Output ("final-output" , "children" ),
285
+ [Input ("page-output" , "children" )])
286
+ def update (n ):
287
+ with lock :
288
+ return n
289
+
290
+ # check for original title after loading
291
+ dash_duo .start_server (app )
292
+ dash_duo .wait_for_text_to_equal ("#final-output" , "0" )
293
+ until (lambda : dash_duo .driver .title == initial_title , timeout = 1 )
294
+
295
+ with lock :
296
+ dash_duo .find_element ("#page-title" ).click ()
297
+ # check for update-title while processing the serverside callback
298
+ if update_title :
299
+ until (lambda : dash_duo .driver .title == update_title , timeout = 1 )
300
+ else :
301
+ until (lambda : dash_duo .driver .title == 'Page 1' , timeout = 1 )
302
+
303
+ dash_duo .wait_for_text_to_equal ("#final-output" , "1" )
304
+ until (lambda : dash_duo .driver .title == 'Page 1' , timeout = 1 )
0 commit comments