Skip to content

feat: Improved handle_on alternative #1484. #2113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Sep 19, 2023
Merged

feat: Improved handle_on alternative #1484. #2113

merged 14 commits into from
Sep 19, 2023

Conversation

mturoci
Copy link
Collaborator

@mturoci mturoci commented Aug 10, 2023

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

Main features

  • Trigger handler based on "what changed" rather than "what matched first".
  • Trigger even if the value submitted is False or '' - this was a bug in the original handle_on.
  • Expand pattern matching beyond # routing to regular interactions (btns, etc.) as well.

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()

#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
Copy link

Far0n commented Aug 10, 2023

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

cmd_40xXcon9lZ.mp4

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
Copy link

Far0n commented Aug 10, 2023

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
Copy link
Collaborator Author

mturoci commented Aug 11, 2023

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
Copy link
Collaborator Author

mturoci commented Aug 11, 2023

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

Interesting, will have a look.

@mturoci
Copy link
Collaborator Author

mturoci commented Aug 11, 2023

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
Copy link

Far0n commented Aug 11, 2023

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.

@mturoci mturoci merged commit 1674675 into main Sep 19, 2023
@mturoci mturoci deleted the feat/issue-1484 branch September 19, 2023 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants