Skip to content
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

Improve event registration #1095

Merged
merged 13 commits into from
Jun 29, 2023
Merged

Improve event registration #1095

merged 13 commits into from
Jun 29, 2023

Conversation

falkoschindler
Copy link
Contributor

@falkoschindler falkoschindler commented Jun 27, 2023

This PR attempts to improve the generic event registration with the .on() method.

@falkoschindler falkoschindler marked this pull request as draft June 27, 2023 12:34
@falkoschindler falkoschindler added the enhancement New feature or request label Jun 27, 2023
@falkoschindler falkoschindler added this to the 1.3.0 milestone Jun 27, 2023
@falkoschindler falkoschindler self-assigned this Jun 27, 2023
@falkoschindler
Copy link
Contributor Author

I found a way to define, which event arguments should be included, that is easy to use and backward-compatible:

  1. A list of strings names the attributes of the JavaScript event object:

    ui.button().on('click', handle_click, ['clientX', 'clientY'])
  2. An empty list requests no attributes:

    ui.button().on('click', handle_click, [])
  3. The value None represents all attributes (the default):

    ui.button().on('click', handle_click, None)
  4. If the event is called with multiple arguments like QTable's "row-click" (evt, row, index) => void, you can define a list of argument definitions:

    ui.table(...).on('rowClick', handle_row_click, [[], ['name'], None])

    In this example the "row-click" event will omit all arguments of the first evt argument, send only the "name" attribute of the row argument and send the full index.

If the retrieved list of event arguments has length 1, the argument is automatically unpacked. So you can write

ui.button().on('click', lambda e: print(e.args['clientX'], flush=True))

instead of

ui.button().on('click', lambda e: print(e.args[0]['clientX'], flush=True))

Note that by default all JSON-serializable attributes of all arguments are sent. This is to simplify registering for new events and discovering their attributes. If bandwidth is an issue, the arguments should be limited to what is actually needed on the server.

@falkoschindler falkoschindler marked this pull request as ready for review June 28, 2023 09:23
@rodja
Copy link
Member

rodja commented Jun 29, 2023

Looks super cool. I think we should add some in-detail documentation about the .on event registration system including your explanations from #1095 (comment).

@falkoschindler falkoschindler merged commit d9ab5e7 into v1.3 Jun 29, 2023
6 checks passed
@falkoschindler falkoschindler deleted the events branch June 29, 2023 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve access to event arguments .on Event for Element doesn't pass information to callback about sender
2 participants