Created
May 5, 2021 16:41
-
-
Save mtanco/ab7b8cc469fb441318cb7a8aa6708080 to your computer and use it in GitHub Desktop.
This Gist extends the https://wave.h2o.ai/docs/examples/background/ example and shows that apps are still interactive while running background tasks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import random | |
from h2o_wave import main, app, Q, ui | |
def sleeper_agent(secs) -> str: | |
time.sleep(secs) # Blocks! | |
return f'Done waiting for {secs} seconds!' | |
@app('/demo') | |
async def serve(q: Q): | |
print(q.args) | |
print(q.client) | |
if q.args.start: | |
await start_button_clicked(q) | |
else: | |
new_browser_tab(q) | |
await q.page.save() | |
def new_browser_tab(q: Q): | |
# Layout of the page | |
q.page['meta'] = ui.meta_card( | |
box='', | |
layouts=[ | |
ui.layout( | |
breakpoint='xs', | |
width='1200px', | |
zones=[ | |
ui.zone('main_body', direction='column') | |
] | |
) | |
] | |
) | |
# Home screen | |
q.page['form'] = ui.form_card( | |
box='main_body', | |
items=[ui.button(name='start', label='Start')] | |
) | |
# Variable for this instance of the app | |
q.client.event_count = 0 | |
async def start_button_clicked(q: Q): | |
count = q.client.event_count # This function call uses this number | |
q.client.event_count += 1 # Increment instantly in case another call is made while this runs | |
q.page[f'event_card_{count}'] = ui.form_card( | |
box='main_body', | |
items=[ui.progress('Running...')] | |
) | |
await q.page.save() | |
seconds = random.randint(1, 3) | |
message = await q.run(sleeper_agent, seconds) | |
q.page[f'event_card_{count}'] = ui.form_card( | |
box='main_body', | |
items=[ui.message_bar('info', message)] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment