Why serve function runs twice when app is reloaded? #2072
-
Is there any reason for running serve function twice when app is reloaded? Sample code from h2o_wave import main, app, Q, ui
@app('/')
async def serve(q: Q):
print("Testing......")
content = 'Welcome to our store!'
location = q.args['#']
if location:
if location == 'menu/spam':
content = "Sorry, we're out of spam!"
elif location == 'menu/ham':
content = "Sorry, we're out of ham!"
elif location == 'menu/eggs':
content = "Sorry, we're out of eggs!"
elif location == 'about':
content = 'Everything here is gluten-free!'
if not q.client.initialized:
q.page['nav'] = ui.tab_card(
box='1 1 4 1',
items=[
ui.tab(name='#menu/spam', label='Spam'),
ui.tab(name='#menu/ham', label='Ham'),
ui.tab(name='#menu/eggs', label='Eggs'),
ui.tab(name='#about', label='About'),
],
value=f'#{location}' if location else None,
)
q.page['blurb'] = ui.markdown_card(
box='1 2 4 2',
title='Store',
content=content,
)
q.client.initialized = True
elif location:
blurb = q.page['blurb']
blurb.content = content
await q.page.save() Log INFO: 127.0.0.1:56126 - "POST / HTTP/1.1" 200 OK
Testing......
INFO: 127.0.0.1:56126 - "POST /disconnect HTTP/1.1" 200 OK
2023/07/12 15:23:10 # {"addr":"[::1]:56079","t":"ui_drop"}
INFO: 127.0.0.1:56126 - "POST / HTTP/1.1" 200 OK
Testing......
2023/07/12 15:23:11 # {"addr":"[::1]:56127","route":"/","t":"ui_add"}
2023/07/12 15:23:11 # {"addr":"[::1]:56127","route":"/b14664c5-5a5a-476e-8389-81ea59784829","t":"ui_add"}
2023/07/12 15:23:11 * /b14664c5-5a5a-476e-8389-81ea59784829 {"d":[{"k":"nav","d":{"view":"tab","box":"1 1 4 1","items":[{"name":"#menu/spam","label":"Spam"},{"name":"#menu/ham","label":"Ham"},{"name":"#menu/eggs","label":"Eggs"},{"name":"#about","label":"About"}]}},{"k":"blurb","d":{"view":"markdown","box":"1 2 4 2","title":"Store","content":"Welcome to our store!"}}]} cc @dulajra |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Yes, the reason is that when reload is hit, the first |
Beta Was this translation helpful? Give feedback.
-
@mturoci We have a specific use case where we need to obtain the browser timezone, here I have shared a sample code with our workaround to get the browser timezone while using WAVE. This is causing the serve function to run one more time totaling thrice when reloading the app. Is there a better way to get the browser timezone without triggering the serve function?
|
Beta Was this translation helpful? Give feedback.
Yes, the reason is that when reload is hit, the first
serve
corresponds to client disconnect and the second one is the initialserve
call.