Skip to content

Instantly share code, notes, and snippets.

@mtanco
Created March 25, 2021 20:49
Show Gist options
  • Save mtanco/cd803adcca5fa7da96948112f9970804 to your computer and use it in GitHub Desktop.
Save mtanco/cd803adcca5fa7da96948112f9970804 to your computer and use it in GitHub Desktop.
This is a simple app for understanding the behavior of `handle_on` and when `q.arg` dictionary is cleared.
from h2o_wave import main, app, Q, ui, on, handle_on
@app('/')
async def serve(q: Q):
if not q.client.initialized: # first time a tab comes this is app
q.page['a'] = ui.form_card(
box='4 3 4 2',
items=[
ui.textbox(name='color_textbox', placeholder='Purple'),
ui.button(name='color_submit', label='Submit', primary=True),
]
)
q.client.initialized = True
else: # if a tab is already active
await handle_on(q) # calls all "on" functions in order of q.args and stops after first match
await q.page.save()
@on()
async def color_submit(q: Q):
# In this app, this function is never called as we have a textbox with an on handled above the button
print('Function: color_submit')
print(f'Arguments: {str(q.args)}')
q.page['b'] = ui.form_card(
box='4 5 4 1',
items=[
ui.text(f'<center>I am the Button Handler: {q.args.color_textbox}</center>')
]
)
@on()
async def color_textbox(q: Q):
print('Function: color_textbox')
print(f'Arguments: {str(q.args)}')
q.page['c'] = ui.form_card(
box='4 6 4 1',
items=[
ui.text(f'<center>I am the Textbox Handler: {q.args.color_textbox}</center>')
]
)
# We can call the other function from this function
# await color_submit(q)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment