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

Improved callback_context #1952

Merged
merged 16 commits into from
Apr 22, 2022
Merged

Improved callback_context #1952

merged 16 commits into from
Apr 22, 2022

Conversation

AnnMarieW
Copy link
Collaborator

@AnnMarieW AnnMarieW commented Feb 28, 2022

closes # #1818
#1054

See also: Community Forum Discussion

This PR adds features to dash.callback_context to make it easier to use:

  • simplifies the syntax for determining which input triggered a callback.
  • adds ctx as an alias for dash.callback_context
  • adds ability access the ctx dictionary with the dot notation
  • simplifies the callback function signature when there are many inputs.
  • makes the dict id easier to access in callbacks using pattern matching.

Determining which input triggered a callback

The new features will replace boilerplate such as:

button_id = dash.callback_context.triggered[0]['prop_id'].split('.')[0]

👉 See the Announcement in the forum for details and examples.

@AnnMarieW AnnMarieW changed the title Add triggered_id to callback_context Improved callback_context Mar 4, 2022
dash/__init__.py Outdated
@@ -19,7 +19,7 @@
from . import html # noqa: F401,E402
from . import dash_table # noqa: F401,E402
from .version import __version__ # noqa: F401,E402
from ._callback_context import callback_context # noqa: F401,E402
from ._callback_context import callback_context, ctx # noqa: F401,E402
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be a separate instance? Can we just say ctx = callback_context after all the imports, and remove the extra instantiation?

@alexcjohnson
Copy link
Collaborator

alexcjohnson commented Apr 21, 2022

As just discussed on slack, this looks great! Aside from my comment above, this just needs:

  • a changelog entry
  • resolving conflicts
  • flattening the docstring indentation here and here

@AnnMarieW
Copy link
Collaborator Author

@alexcjohnson I completed the following:

As just discussed on slack, this looks great! Aside from my comment above, this just needs:

  • a changelog entry
  • resolving conflicts
  • flattening the docstring indentation here and here

Also, I removed the extra callback_context instance per your comment.
One test in test-39 fails, but it's unrelated to any changes in this PR -- I'm not sure what's wrong.

Copy link
Collaborator

@alexcjohnson alexcjohnson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic! 💃

@MattesR
Copy link

MattesR commented Jun 2, 2022

IDK whether this is the right spot. I'd just want to mention that there's a typo in this example:
example app using the new ctx.triggered_ids to get the index of the button that triggered the callback:

it says prop_id=ctx.triggered_ids.first() but it should be prop_id=ctx.triggered_id.first(). Found out when trying to update my code with the new ctx.

ctx has the attributes 'triggered_id', and 'triggered_prop_ids' according to dir(ctx)

@AnnMarieW
Copy link
Collaborator Author

AnnMarieW commented Jun 3, 2022

@MattesR Thanks for your comment. I think the examples in the initial write-up for this PR were confusing because the names changed during review. For example triggered_ids was renamed to triggered_prop_ids. Then a new attribute was added: ctx.triggered_id, which is the component id of the the Input that triggered the callback.

I just updated the comment to remove all the examples here and point people to the ones in the announcement on the community forum.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants