Site / Async

Update any page on a site from within an app using an AsyncSite instance.

from .synth import FakePercent
from h2o_wave import Q, app, main, ui, AsyncSite
site = AsyncSite()
# Grab a reference to the /stats page
stats_page = site['/stats']
# A flag to indicate whether to pause or resume updating the page at '/stats'
update_stats = False
async def update_stats_page(q, page):
f = FakePercent()
card = page['example']
while update_stats:
await q.sleep(1)
price, percent = = price = percent
card.progress = percent
async def serve(q: Q):
if not q.client.initialized:
# Set up up the page at /stats
stats_page.drop() # Clear any existing page
stats_page['example'] = ui.wide_gauge_stat_card(
box='1 1 2 1',
value='=${{intl price minimum_fraction_digits=2 maximum_fraction_digits=2}}',
aux_value='={{intl percent style="percent" minimum_fraction_digits=2 maximum_fraction_digits=2}}',
data=dict(price=0, percent=0),
# Set up this app's UI['form'] = ui.form_card(box='1 1 12 10', items=[
ui.frame(path='/stats', height='110px'),
ui.button(name='toggle', label='Start updates', primary=True),
q.client.initialized = True
if q.args.toggle:
global update_stats
update_stats = not update_stats['form'].items[1].button.label = 'Stop updates' if update_stats else 'Start updates'
await update_stats_page(q, stats_page)

Tags: โ€‚site