handle_on for args with triggers
- Dominant language
- Python
- Stars
- 4.3k
- Forks
- 371
- Avg merge
- 26d 1h
- Merged PRs (30d)
- 1
Description
### Wave SDK Version, OS
Wave 0.16.0, OS agnostic
### Actual behavior
Following code triggers `dark_mode` instead of `click_me`
trigger for the toggle is implemented like this:
`@on('dark_mode', lambda x: isinstance(x, bool))`
#### Code
```python
from h2o_wave import Q, app, handle_on, main, on, ui # noqa: F401
from h2o_wave.core import expando_to_dict
def log_q_args(q: Q):
print('>>>> q.args >>>>')
for k, v in expando_to_dict(q.args).items():
print(f'{k}: {v}')
print('<<<< q.args <<<<')
def meta_card(theme):
return ui.meta_card(
box='',
title='Dark Mode',
layouts=[
ui.layout(
breakpoint='xs',
zones=[ui.zone('main', direction=ui.ZoneDirection.COLUMN)],
max_width='350px',
)
],
theme=theme,
)
async def initialize_client(q: Q):
if q.client.initialized:
print('Already Initialized')
return
print('Initializing App')
q.user.dark_mode = False
q.client.clicks = 0
q.page['meta'] = meta_card(theme='light')
q.page['dark_mode'] = ui.section_card(
box='main',
title='',
subtitle='',
items=[
ui.toggle(
name='dark_mode',
label='Dark Mode',
value=q.user.dark_mode,
trigger=True,
)
],
)
q.page['clicker'] = ui.form_card(
box='main',
items=[
ui.text(f'Clicked {q.client.clicks} times'),
ui.button(name='click_me', label='Click Me', primary=True),
],
)
q.client.initialized = True
await q.page.save()
@app('/')
async def serve(q: Q):
print('Enter Serve')
log_q_args(q)
await initialize_client(q)
await handle_on(q)
print('Exit Serve')
@on('dark_mode', lambda x: isinstance(x, bool))
async def dark_mode(q: Q):
print('Triggered "dark_mode" handler')
q.user.dark_mode = q.args.dark_mode
q.page['dark_mode'].items[0].toggle.value = q.user.dark_mode
q.page['meta'].theme = 'neon' if q.user.dark_mode else 'light'
await q.page.save()
@on()
async def click_me(q: Q):
q.client.clicks += 1
q.page['clicker'].items[0].text.content = f'Clicked {q.client.clicks} times'
await q.page.save()
```
#### Video of the broken output
https://user-images.githubusercontent.com/16831326/120125037-d5f1ff00-c16b-11eb-9459-b744f71496d4.mp4
### Expected behavior
Trigger dark_mode only when dark_mode is toggled. Clicking the `Click Me` button should increment the count as shown in the video below.
https://user-images.githubusercontent.com/16831326/120124568-cc679780-c169-11eb-9c74-a2a8c42d1a9b.mp4
### Steps To Reproduce
1. Save the above code to `dark_mode.py`
2. Run it
```shell
$ wave run dark_mode.py
```
### A proposal
Pass `q` to the predicate on this line https://github.com/h2oai/wave/blob/f7a99220a2971a357d3fc02a703ddeae9c1412a2/py/h2o_wave/routing.py#L113
changing it to `if predicate(q, arg_value):`
Change the app code from above to
```python
def dark_mode_check(q: Q, dark_mode: bool) -> bool:
return dark_mode != q.user.dark_mode
@on('dark_mode', dark_mode_check)
async def dark_mode(q: Q):
...
```
Contributor guide
Assessment
This issue has not been assessed yet.