h2oai / wave

Realtime Web Apps and Dashboards for Python and R
https://wave.h2o.ai
Apache License 2.0
3.98k stars 327 forks source link

feat: Improved handle_on alternative #1484. #2113

Closed mturoci closed 1 year ago

mturoci commented 1 year ago

Introduces a new run_on that is an alternative to the current handle_on with a more intuitive behavior.

Main features

Testing code

from h2o_wave import main, app, Q, ui, on, run_on, handle_on, data

@on('show_inputs')
async def show_inputs(q: Q):
    print('btn works')

@on('textbox')
async def handle_textbox(q: Q):
    print('textbox works')

@on('#show_inputs')
async def hash_handle(q: Q):
    print('hash works')

@on('pricing.select_marks')
async def handle_events(q: Q):
    print('events work')

@app('/demo')
async def serve(q: Q):
    if not q.client.initialized:
        q.page['pricing'] = ui.plot_card(
            box='5 1 4 5',
            title='Interval',
            data=data(fields='product price', rows=[
                ['spam', 1.49],
                ['eggs', 2.49],
                ['ham', 1.99],
            ]),
            plot=ui.plot([ui.mark(type='interval', x='=product', y='=price', y_min=0)]),
            events=['select_marks']
        )
        q.client.initialized = True

    if q.args.show_inputs:
        q.page['example'].items = [
            ui.text(f'textbox={q.args.textbox}'),
            ui.button(name='show_form', label='Back', primary=True),
        ]
    else:
        q.page['example'] = ui.form_card(box='1 1 4 -1', items=[
            ui.textbox(name='textbox', value='Foo', label='Standard', trigger=True),
            ui.button(name='#show_inputs', label='Hash change'),
            ui.button(name='show_inputs', label='Submit', primary=True),
        ])

    await run_on(q)
    # await handle_on(q)
    await q.page.save()

https://github.com/h2oai/wave/discussions/1480 works as expected as well.

linux.zip macosx.zip win.zip

cc @Far0n @vopani @pascal-pfeiffer for feedback

Naming suggestions highly welcome!

Closes #1484 Closes #1008

Far0n commented 1 year ago

@mturoci tested on windows, but doesn't seem to work (no prints):

https://github.com/h2oai/wave/assets/6949295/af4596d0-47ca-4e15-bfb7-a377d37f72d6

edit: seems to be another more general issue: q.args is always empty. any idea why? (python 3.11 .. can that be an issue?)

Far0n commented 1 year ago

No issues under linux. Def. better than handle_on behavior imho. __wave_submission_name__ is also very handy.

One thing, empty textbox and toggles will still not work (even tho its not directly related to this PR I think)

INFO:     127.0.0.1:55938 - "POST / HTTP/1.1" 200 OK
q.args textbox:'Foo2', #show_inputs:False, show_inputs:False, __wave_submission_name__:'textbox'
textbox works
2023/08/10 19:16:20 * /d2857dc7-5d83-4245-a6c8-c8d197d1edeb {"d":[{"k":"example","d":{"view":"form","box":"1 1 4 -1","items":[{"textbox":{"name":"textbox","label":"Standard","value":"Foo","trigger":true}},{"button":{"name":"#show_inputs","label":"Hash change"}},{"button":{"name":"show_inputs","label":"Submit","primary":true}}]}}]}

INFO:     127.0.0.1:55942 - "POST / HTTP/1.1" 200 OK
q.args textbox:'', #show_inputs:False, show_inputs:False, __wave_submission_name__:'textbox'
2023/08/10 19:16:27 * /d2857dc7-5d83-4245-a6c8-c8d197d1edeb {"d":[{"k":"example","d":{"view":"form","box":"1 1 4 -1","items":[{"textbox":{"name":"textbox","label":"Standard","value":"Foo","trigger":true}},{"button":{"name":"#show_inputs","label":"Hash change"}},{"button":{"name":"show_inputs","label":"Submit","primary":true}}]}}]}
mturoci commented 1 year ago

One thing, empty textbox and toggles will still not work (even tho its not directly related to this PR I think)

Yep, didn't manage to add it yesterday. Will do today.

Thanks for the feedback @Far0n!

mturoci commented 1 year ago

tested on windows, but doesn't seem to work (no prints):

Interesting, will have a look.

mturoci commented 1 year ago

Windows fixed and zip updated in the PR description. @Far0n give it a try please and let me know if all is ok. It should now also include fixed for falsy values not triggering.

Far0n commented 1 year ago

Windows fixed and zip updated in the PR description. @Far0n give it a try please and let me know if all is ok. It should now also include fixed for falsy values not triggering.

yep, now both work.