|
| 1 | +from enum import Enum |
| 2 | + |
| 3 | + |
| 4 | +class BackendFilter(Enum): |
| 5 | + """ |
| 6 | + Filter used with :meth:`~matplotlib.backends.registry.BackendRegistry.list_builtin` |
| 7 | +
|
| 8 | + .. versionadded:: 3.9 |
| 9 | + """ |
| 10 | + INTERACTIVE = 0 |
| 11 | + NON_INTERACTIVE = 1 |
| 12 | + |
| 13 | + |
| 14 | +class BackendRegistry: |
| 15 | + """ |
| 16 | + Registry of backends available within Matplotlib. |
| 17 | +
|
| 18 | + This is the single source of truth for available backends. |
| 19 | +
|
| 20 | + All use of ``BackendRegistry`` should be via the singleton instance |
| 21 | + ``backend_registry`` which can be imported from ``matplotlib.backends``. |
| 22 | +
|
| 23 | + .. versionadded:: 3.9 |
| 24 | + """ |
| 25 | + # Built-in backends are those which are included in the Matplotlib repo. |
| 26 | + # A backend with name 'name' is located in the module |
| 27 | + # f'matplotlib.backends.backend_{name.lower()}' |
| 28 | + |
| 29 | + # The capitalized forms are needed for ipython at present; this may |
| 30 | + # change for later versions. |
| 31 | + _BUILTIN_INTERACTIVE = [ |
| 32 | + "GTK3Agg", "GTK3Cairo", "GTK4Agg", "GTK4Cairo", |
| 33 | + "MacOSX", |
| 34 | + "nbAgg", |
| 35 | + "QtAgg", "QtCairo", "Qt5Agg", "Qt5Cairo", |
| 36 | + "TkAgg", "TkCairo", |
| 37 | + "WebAgg", |
| 38 | + "WX", "WXAgg", "WXCairo", |
| 39 | + ] |
| 40 | + _BUILTIN_NOT_INTERACTIVE = [ |
| 41 | + "agg", "cairo", "pdf", "pgf", "ps", "svg", "template", |
| 42 | + ] |
| 43 | + _GUI_FRAMEWORK_TO_BACKEND_MAPPING = { |
| 44 | + "qt": "qtagg", |
| 45 | + "gtk3": "gtk3agg", |
| 46 | + "gtk4": "gtk4agg", |
| 47 | + "wx": "wxagg", |
| 48 | + "tk": "tkagg", |
| 49 | + "macosx": "macosx", |
| 50 | + "headless": "agg", |
| 51 | + } |
| 52 | + |
| 53 | + def backend_for_gui_framework(self, framework): |
| 54 | + """ |
| 55 | + Return the name of the backend corresponding to the specified GUI framework. |
| 56 | +
|
| 57 | + Parameters |
| 58 | + ---------- |
| 59 | + framework : str |
| 60 | + GUI framework such as "qt". |
| 61 | +
|
| 62 | + Returns |
| 63 | + ------- |
| 64 | + str |
| 65 | + Backend name. |
| 66 | + """ |
| 67 | + return self._GUI_FRAMEWORK_TO_BACKEND_MAPPING.get(framework) |
| 68 | + |
| 69 | + def list_builtin(self, filter_=None): |
| 70 | + """ |
| 71 | + Return list of backends that are built into Matplotlib. |
| 72 | +
|
| 73 | + Parameters |
| 74 | + ---------- |
| 75 | + filter_ : `~.BackendFilter`, optional |
| 76 | + Filter to apply to returned backends. For example, to return only |
| 77 | + non-interactive backends use `.BackendFilter.NON_INTERACTIVE`. |
| 78 | +
|
| 79 | + Returns |
| 80 | + ------- |
| 81 | + list of str |
| 82 | + Backend names. |
| 83 | + """ |
| 84 | + if filter_ == BackendFilter.INTERACTIVE: |
| 85 | + return self._BUILTIN_INTERACTIVE |
| 86 | + elif filter_ == BackendFilter.NON_INTERACTIVE: |
| 87 | + return self._BUILTIN_NOT_INTERACTIVE |
| 88 | + |
| 89 | + return self._BUILTIN_INTERACTIVE + self._BUILTIN_NOT_INTERACTIVE |
| 90 | + |
| 91 | + |
| 92 | +# Singleton |
| 93 | +backend_registry = BackendRegistry() |
0 commit comments