Replies: 2 comments
-
That's expected behavior, because q.args still holds the information about the button click. So if you really want to call serve, you need to make sure that q.args is in the state you want, e.g. clearing q.args beforehand. That's why it might be better to avoid calling serve from app-code (it's suppossed to be the submit entry point).
One way is below. It's basically a state transition from nothing created -> something created & show countdown -> something created post countdown (either countdown completed or user hit "click here" => show main page) Side note: avoid using time.sleep, because it blocks the running event loop import asyncio
from h2o_wave import Q, app, main, ui
async def create(q: Q):
q.page['main'] = ui.form_card(box="1 1 5 5", items=[ui.progress(label="Creating Something")])
await q.page.save()
await q.sleep(2)
async def countdown(q: Q):
for sec in range(9, -1, -1):
widgets = []
widgets.extend([
ui.inline([
ui.text(f"Refreshing the page in {sec} seconds. Or"),
ui.button(label=' click here ', name='admin', link=True),
ui.text("to refresh now.")
])
])
await q.sleep(1)
q.page['main'] = ui.form_card(box='1 1 5 5', items=[*widgets])
await q.page.save()
async def main_page(q: Q):
q.page['main'] = ui.form_card(box='1 1 5 5', items=[ui.text("Main Page")])
await q.page.save()
@app("/")
async def serve(q: Q):
q.page.drop()
if q.args.admin:
q.client.countdown.cancel()
return await main_page(q)
if not q.client.something_created:
await create(q)
q.client.something_created = True
q.client.countdown = asyncio.create_task(countdown(q))
try:
await q.client.countdown
except asyncio.CancelledError:
print("User clicked 'click here'")
return await main_page(q)
await main_page(q) NrOAhdy38U.mp4 |
Beta Was this translation helpful? Give feedback.
-
@azim-b what is the purpose of the "reload"? Running the scenario again? From my limited point of view, it seems like calling a regular python function would do the trick. This way you are free of hacks and infinite loops. Calling Note: The reason why you are getting |
Beta Was this translation helpful? Give feedback.
-
When I hit a button 'Create', let's say the name is
create
,q.args['create']
isTrue.
.Then I perform some logic, and let's say the creation was successful.
Then, I display a message bar saying it was successful, and show a message that says 'redirecting in x seconds' where x goes from 10 to 0, and the
page
is saved in every iteration.Finally, after the for loop completes, I "reload" the page, i.e. call
serve(q)
.Primary problem:
serve(q)
is called, and the function in subject is called again (as@on('#admin')
), let's say the name isadmin()
, the value ofq.args['create']
is stillTrue.
. (Expectation: False) So, the same logic repeats again, like an infinite loop.a. why is the
q.args
value of the button still true?b. how do I prevent the infinite loop, i.e. not execute the create login again (since create is not hit this time)
Secondary question:
serve(q)
.Example flow (with the fault):
auto_refresh.mov
Code snippet that deals with this use case:
Beta Was this translation helpful? Give feedback.
All reactions