-
-
Notifications
You must be signed in to change notification settings - Fork 396
Import hooks to set up inline backend when matplotlib is loaded #82
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| """Import hooks to activate our inline backend when matplotlib is loaded | ||
| """ | ||
|
|
||
| import imp | ||
| import sys | ||
|
|
||
| def set_inline_backend(mpl): | ||
| from IPython.core.pylabtools import backends, configure_inline_support | ||
| from .kernelapp import IPKernelApp | ||
| mpl.interactive(True) | ||
| mpl.use(backends['inline']) | ||
| configure_inline_support(IPKernelApp.instance().shell, backends['inline']) | ||
|
|
||
| class Py2MPLFinderShim(object): | ||
| @classmethod | ||
| def find_module(cls, fullname, path=None): | ||
| if (fullname != 'matplotlib') or (path is not None): | ||
| return None | ||
|
|
||
| try: | ||
| res = imp.find_module('matplotlib') | ||
| except ImportError: | ||
| return None | ||
| else: | ||
| return Py2MPLLoaderShim(*res) | ||
|
|
||
| class Py2MPLLoaderShim(object): | ||
| def __init__(self, file, pathname, description): | ||
| self.file = file | ||
| self.pathname = pathname | ||
| self.description = description | ||
|
|
||
| def load_module(self, fullname): | ||
| # fullname should only ever be 'matplotlib' here | ||
| m = imp.load_module(fullname, self.file, self.pathname, self.description) | ||
| set_inline_backend(m) | ||
| return m | ||
|
|
||
| try: | ||
| from importlib.machinery import PathFinder | ||
| except ImportError: | ||
| # Python 2 | ||
| finder_shim = Py2MPLFinderShim | ||
| else: | ||
| class Py3MPLFinderShim(PathFinder): | ||
| # On Python 3.4 and above, find_spec() will be called | ||
| @classmethod | ||
| def find_spec(cls, fullname, path=None, target=None): | ||
| if (fullname != 'matplotlib') or (path is not None): | ||
| return None | ||
|
|
||
| spec = PathFinder.find_spec(fullname, path=None) | ||
| if spec is None: | ||
| return None | ||
|
|
||
| if spec.loader is not None: | ||
| spec.loader = Py3MPLLoaderShim(spec.loader) | ||
| return spec | ||
|
|
||
| # On Python 3.3 or below, find_module() will be called | ||
| @classmethod | ||
| def find_module(cls, fullname, path=None): | ||
| print(fullname, path) | ||
| if (fullname != 'matplotlib') or (path is not None): | ||
| return None | ||
|
|
||
| real_loader = PathFinder.find_module(fullname, path=None) | ||
| if real_loader is None: | ||
| return None | ||
| return Py3MPLLoaderShim(real_loader) | ||
|
|
||
| class Py3MPLLoaderShim(object): | ||
| def __init__(self, real_loader): | ||
| self.real_loader = real_loader | ||
|
|
||
| def load_module(self, fullname): | ||
| # fullname should only ever be 'matplotlib' here) | ||
| m = self.real_loader.load_module(fullname) | ||
| set_inline_backend(m) | ||
| return m | ||
|
|
||
|
|
||
| finder_shim = Py3MPLFinderShim | ||
|
|
||
| def install_import_hook(): | ||
| sys.meta_path.insert(0, finder_shim) | ||
|
|
||
| def remove_import_hook(): | ||
| sys.meta_path.remove(finder_shim) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting the hook on
matplotlibmakes it impossible forto work. Is there something else we can look for that wouldn't preempt explicit backend selection? I've used
pyplotin my own version of this, but I'm not sure if it's sufficiently general.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the only problem is that the import hook loads our backend (and pyplot) in order to set up the config changes and post-execute hooks that the inline backends need. If we can delay that stuff (the call to
configure_inline_support()) until the backend is actually loaded, I think it would be fine to set the backend on loadingmatplotlib, because the setting isn't committed untilmatplotlib.backendsis loaded.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was also discussion at scipy with @WeatherGod to delay actually forcing the back end until the first figure is created.