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

Skip to content

[MNT]: Move Matplotlib backend mapping from IPython and support backends self-registering #27663

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
ianthomas23 opened this issue Jan 17, 2024 · 5 comments · Fixed by #27948
Closed

Comments

@ianthomas23
Copy link
Member

Summary

I propose to move the Matplotlib backend mapping that is currently in IPython into Matplotlib, and extend it to support backends registering themselves via entry_points. This was originally discussed in #19482 and Tom summarised and proposed a solution in #19482 (comment).

Proposed fix

Background

IPython supports the use of the %matplotlib magic in two ways:

  • %matplotlib <backend_name> to use the named Matplotlib backend.
  • %matplotlib --list to list available backends.

IPython contains hard-coded information about which backends exist, and which GUI interactive framework they support. The current information consists of backends that are built-into mpl itself (e.g. qt, notebook, webagg) and those in external libraries (ipympl and inline) which are Jupyter-based.

Problems

  • This is unnecessarily difficult to maintain as new backends require changes in IPython.
  • There is no support in IPython for backends specified using "module://whatever.backend.module"

Proposal

  1. Keep the same %matplotlib magic functionality in IPython but defer responsibility for the backend naming and interactive framework identification to Matplotlib, with a version check of Matplotlib to fallback to the existing behaviour if necessary.
  2. Add a backend registry to Matplotlib. Whether this is literally a BackendRegistry singleton or not is an implementation detail.
  3. The registry will allow use of backends for both IPython and matplotlib.use in three different categories:
    a. Backends that are built into the Matplotlib code base.
    b. Backends that can be dynamically loaded using "module://backend.name" such as MplCairo.
    c. Backends that register themselves using an entry_point. Initially this will be ipympl and matplotlib-inline.
  4. Need to handle error situations of multiple backends registering under the same name, or registering with the same name as a built-in backend.
  5. Matplotlib needs a new list_backends function for IPython’s %matplotlib --list. This will include all backends in items 3a and 3c above, as well as any “module://…” backends (3b) that have already been loaded. The latter is so that if you use %matplotlib module://… in IPython a subsequent %matplotlib –list will include the backend you are using.
  6. The list_backends function may as well be public within Matplotlib as it is a feature that is requested every so often. It will have to be clearly stated that this contains “backends that mpl is aware of” rather than an exhaustive list.

This isn’t the full set of functionality listed at #19482 (comment) as it does not include use of "m://…" form for example.

Changes will be required in the following projects:

  1. Matplotlib = new registry, looking up entry points, docs.
  2. Matplotlib-inline = add entry point.
  3. Ipympl = add entry point.
  4. IPython = call Matplotlib backend registry for backend info, docs.

Changes 1-3 will need to be published in releases before 4 can be merged and released.

@tacaswell
Copy link
Member

Can we put this on the agenda for the meeting tomorrow?

@ianthomas23
Copy link
Member Author

Yes, I can attend the meeting.

@tacaswell
Copy link
Member

attn @anntzer

@anntzer
Copy link
Contributor

anntzer commented Jan 17, 2024

I'll try to attend tomorrow as well.

@ianthomas23
Copy link
Member Author

Following discussion at the weekly meeting (https://hackmd.io/l9vkn_T4RSmk147H_ZPPBA#ipython--Matplotlib-integration) I am going ahead with an implementation of this and we will see what problems come up.

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

Successfully merging a pull request may close this issue.

4 participants