Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ENH: Add the ability to block callback signals #20816

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 1 commit into from
Aug 12, 2021

Conversation

greglucas
Copy link
Contributor

PR Summary

CallbackRegistry objects gain a context manager .block() method that can be used to temporarily block callback signals from being processed.

with callbacks.block():
    callbacks.process("test")  # Does nothing
with callbacks.block(signal="test"):
    callbacks.process("test")  # Does nothing

Closes #20802
Would help with disabling norm update signals in #19553

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and run flake8 --docstring-convention=all).
  • [N/A] New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

Copy link
Member

@story645 story645 left a comment

Choose a reason for hiding this comment

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

I think this could be really useful for debugging interactive stuff, so tentatively 👍

@greglucas greglucas force-pushed the callback-blocking branch 2 times, most recently from a9680e7 to ad95b35 Compare August 10, 2021 23:13
Copy link
Member

@story645 story645 left a comment

Choose a reason for hiding this comment

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

minor nits, but liking the new docs :)

@timhoffm
Copy link
Member

What do you think about naming this blocked()? I like to use context manager names that are not imperative verbs and cannot be confused with simple functions. There is no general convention on this in the community, but I think with callbacks.blocked() reads quite nicely.

@greglucas
Copy link
Contributor Author

@timhoffm, apparently there is a preferred convention and your suggestion lines up with that :)
https://www.python.org/dev/peps/pep-0343/#examples
I changed to blocked and moved the change note over to "next what's new" and included a short example there.

@story645
Copy link
Member

story645 commented Aug 11, 2021

So is maybe annoying, but I think this needs a short API changes note too. only incompatible changes need API change notes.

Also, and I might be confused about the functionality here, but would it be possible for the what's new example to be slight less toy/use like one of the auto registrated callbacks or something?

CallbackRegistry objects now have a context manager .block()
method that can be used to temporarily block callback signals
from being processed.
@greglucas
Copy link
Contributor Author

I came up with another toy-like example, but with interactivity on the figure canvas and disabling key-press events. Did you have another example/area in mind to apply this? It looks like the widgets keep their callbacks private in _observers, so I guess this doesn't really help the end user currently over there.

@story645
Copy link
Member

Using fig.canvas.callbacks is perfect - I was unsure whether the functionality extended to the fig callbacks and this clears it right up & I think would be a common use case 😄

@timhoffm timhoffm added this to the v3.5.0 milestone Aug 12, 2021
@timhoffm timhoffm merged commit 495a985 into matplotlib:master Aug 12, 2021
@greglucas greglucas deleted the callback-blocking branch August 12, 2021 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add ability to disable callbacks temporarily
3 participants