|
|
|
|
|
|
|
|
|
import time |
|
import asyncio |
|
import concurrent.futures |
|
from h2o_wave import main, app, Q, ui |
|
|
|
|
|
|
|
def blocking_function(secs) -> str: |
|
time.sleep(secs) |
|
return 'Download completed!' |
|
|
|
|
|
|
|
async def display_progress_bar(q: Q, form, seconds: int): |
|
for i in range(seconds): |
|
progress_value = (i + 1.0) / seconds |
|
form.items = [ |
|
ui.progress( |
|
label='Downloading the interwebs...', |
|
caption=f'{int(progress_value * 100)}%', |
|
value=progress_value, |
|
) |
|
] |
|
await q.page.save() |
|
await q.sleep(1) |
|
|
|
|
|
@app('/demo') |
|
async def serve(q: Q): |
|
if q.args.start: |
|
seconds = 5 |
|
|
|
|
|
form = q.page['form'] |
|
|
|
|
|
future = asyncio.ensure_future(display_progress_bar(q, form, seconds)) |
|
|
|
|
|
with concurrent.futures.ThreadPoolExecutor() as pool: |
|
message = await q.exec(pool, blocking_function, seconds) |
|
|
|
|
|
future.cancel() |
|
|
|
|
|
form.items = [ui.message_bar('info', message)] |
|
await q.page.save() |
|
else: |
|
q.page['form'] = ui.form_card(box='1 1 2 2', items=[ui.button(name='start', label='Start')]) |
|
await q.page.save() |
|
|