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

Allow functions that don't accept parameters as event callbacks #260

Closed
mofojed opened this issue Feb 6, 2024 · 0 comments · Fixed by #271
Closed

Allow functions that don't accept parameters as event callbacks #260

mofojed opened this issue Feb 6, 2024 · 0 comments · Fixed by #271
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@mofojed
Copy link
Member

mofojed commented Feb 6, 2024

As of change #236 , you must now provide lambdas that accept the event positional argument. However, we should allow you to pass in a function that doesn't accept any positional arguments without crashing as well. E.g. this snippet should work:

import deephaven.ui as ui
from deephaven.ui import use_state


@ui.component
def counter():
    count, set_count = use_state(0)
    return [
      # Pass in a function that accepts the event argument
      ui.action_button(f"Foo {count}", on_press=lambda e: set_count(count + 1)),
      # Pass in a function that does not have any positional arguments
      ui.action_button(f"Bar {count}", on_press=lambda: set_count(count + 1))
    ]


c = counter()
@mofojed mofojed added enhancement New feature or request triage labels Feb 6, 2024
@vbabich vbabich added this to the February 2024 milestone Feb 6, 2024
@vbabich vbabich removed the triage label Feb 6, 2024
@jnumainville jnumainville self-assigned this Feb 6, 2024
jnumainville added a commit that referenced this issue Feb 8, 2024
Fixes #260 

This snippet now works:
```
import deephaven.ui as ui
from deephaven.ui import use_state


@ui.component
def counter():
    count, set_count = use_state(0)
    return [
      # Pass in a function that accepts the event argument
      ui.action_button(f"Foo {count}", on_press=lambda e: set_count(count + 1)),
      # Pass in a function that does not have any positional arguments
      ui.action_button(f"Bar {count}", on_press=lambda: set_count(count + 1))
    ]


c = counter()
```

This implementation is more robust than that though. It supports:
1. Dropping any number of positional args. For example, if the callback
supports 3 positional args but only two are defined, the third will be
not be passed in.
3. Dropping any unused keyword args. 
4. If `*args` or `**kwargs` are used, all of the relevant args are
passed in.
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 a pull request may close this issue.

3 participants