|
41 | 41 | from contextlib import contextmanager
|
42 | 42 | from functools import partial
|
43 | 43 | import importlib
|
| 44 | +import inspect |
44 | 45 | import io
|
45 | 46 | import os
|
46 | 47 | import sys
|
@@ -126,120 +127,6 @@ def get_registered_canvas_class(format):
|
126 | 127 | return backend_class
|
127 | 128 |
|
128 | 129 |
|
129 |
| -class _Backend(object): |
130 |
| - # A backend can be defined by using the following pattern: |
131 |
| - # |
132 |
| - # @_Backend.export |
133 |
| - # class FooBackend(_Backend): |
134 |
| - # # override the attributes and methods documented below. |
135 |
| - |
136 |
| - # The following attributes and methods must be overridden by subclasses. |
137 |
| - |
138 |
| - # The `FigureCanvas` and `FigureManager` classes must be defined. |
139 |
| - FigureCanvas = None |
140 |
| - FigureManager = None |
141 |
| - |
142 |
| - # The following methods must be left as None for non-interactive backends. |
143 |
| - # For interactive backends, `trigger_manager_draw` should be a function |
144 |
| - # taking a manager as argument and triggering a canvas draw, and `mainloop` |
145 |
| - # should be a function taking no argument and starting the backend main |
146 |
| - # loop. |
147 |
| - trigger_manager_draw = None |
148 |
| - mainloop = None |
149 |
| - |
150 |
| - # The following methods will be automatically defined and exported, but |
151 |
| - # can be overridden. |
152 |
| - |
153 |
| - @classmethod |
154 |
| - def new_figure_manager(cls, num, *args, **kwargs): |
155 |
| - """Create a new figure manager instance. |
156 |
| - """ |
157 |
| - # This import needs to happen here due to circular imports. |
158 |
| - from matplotlib.figure import Figure |
159 |
| - fig_cls = kwargs.pop('FigureClass', Figure) |
160 |
| - fig = fig_cls(*args, **kwargs) |
161 |
| - return cls.new_figure_manager_given_figure(num, fig) |
162 |
| - |
163 |
| - @classmethod |
164 |
| - def new_figure_manager_given_figure(cls, num, figure): |
165 |
| - """Create a new figure manager instance for the given figure. |
166 |
| - """ |
167 |
| - canvas = cls.FigureCanvas(figure) |
168 |
| - manager = cls.FigureManager(canvas, num) |
169 |
| - return manager |
170 |
| - |
171 |
| - @classmethod |
172 |
| - def draw_if_interactive(cls): |
173 |
| - if cls.trigger_manager_draw is not None and is_interactive(): |
174 |
| - manager = Gcf.get_active() |
175 |
| - if manager: |
176 |
| - cls.trigger_manager_draw(manager) |
177 |
| - |
178 |
| - @classmethod |
179 |
| - def show(cls, block=None): |
180 |
| - """Show all figures. |
181 |
| -
|
182 |
| - `show` blocks by calling `mainloop` if *block* is ``True``, or if it |
183 |
| - is ``None`` and we are neither in IPython's ``%pylab`` mode, nor in |
184 |
| - `interactive` mode. |
185 |
| - """ |
186 |
| - if cls.mainloop is None: |
187 |
| - return |
188 |
| - managers = Gcf.get_all_fig_managers() |
189 |
| - if not managers: |
190 |
| - return |
191 |
| - for manager in managers: |
192 |
| - manager.show() |
193 |
| - if block is None: |
194 |
| - # Hack: Are we in IPython's pylab mode? |
195 |
| - from matplotlib import pyplot |
196 |
| - try: |
197 |
| - # IPython versions >= 0.10 tack the _needmain attribute onto |
198 |
| - # pyplot.show, and always set it to False, when in %pylab mode. |
199 |
| - ipython_pylab = not pyplot.show._needmain |
200 |
| - except AttributeError: |
201 |
| - ipython_pylab = False |
202 |
| - block = not ipython_pylab and not is_interactive() |
203 |
| - # TODO: The above is a hack to get the WebAgg backend working with |
204 |
| - # ipython's `%pylab` mode until proper integration is implemented. |
205 |
| - if get_backend() == "WebAgg": |
206 |
| - block = True |
207 |
| - if block: |
208 |
| - cls.mainloop() |
209 |
| - |
210 |
| - # This method is the one actually exporting the required methods. |
211 |
| - |
212 |
| - @staticmethod |
213 |
| - def export(cls): |
214 |
| - for name in ["FigureCanvas", |
215 |
| - "FigureManager", |
216 |
| - "new_figure_manager", |
217 |
| - "new_figure_manager_given_figure", |
218 |
| - "draw_if_interactive", |
219 |
| - "show"]: |
220 |
| - setattr(sys.modules[cls.__module__], name, getattr(cls, name)) |
221 |
| - |
222 |
| - # For back-compatibility, generate a shim `Show` class. |
223 |
| - |
224 |
| - class Show(ShowBase): |
225 |
| - def mainloop(self): |
226 |
| - return cls.mainloop() |
227 |
| - |
228 |
| - setattr(sys.modules[cls.__module__], "Show", Show) |
229 |
| - return cls |
230 |
| - |
231 |
| - |
232 |
| -class ShowBase(_Backend): |
233 |
| - """ |
234 |
| - Simple base class to generate a show() callable in backends. |
235 |
| -
|
236 |
| - Subclass must override mainloop() method. |
237 |
| - """ |
238 |
| - |
239 |
| - def __call__(self, block=None): |
240 |
| - return self.show(block=block) |
241 |
| - |
242 |
| - |
243 | 130 | class RendererBase(object):
|
244 | 131 | """An abstract base class to handle drawing/rendering operations.
|
245 | 132 |
|
@@ -3366,3 +3253,129 @@ def set_message(self, s):
|
3366 | 3253 | Message text
|
3367 | 3254 | """
|
3368 | 3255 | pass
|
| 3256 | + |
| 3257 | + |
| 3258 | +class _Backend(object): |
| 3259 | + # A backend can be defined by using the following pattern: |
| 3260 | + # |
| 3261 | + # @_Backend.export |
| 3262 | + # class FooBackend(_Backend): |
| 3263 | + # # override the attributes and methods documented below. |
| 3264 | + |
| 3265 | + # May be overridden by the subclass. |
| 3266 | + backend_version = "unknown" |
| 3267 | + # The `FigureCanvas` class must be overridden. |
| 3268 | + FigureCanvas = None |
| 3269 | + # For interactive backends, the `FigureManager` class must be overridden. |
| 3270 | + FigureManager = FigureManagerBase |
| 3271 | + # The following methods must be left as None for non-interactive backends. |
| 3272 | + # For interactive backends, `trigger_manager_draw` should be a function |
| 3273 | + # taking a manager as argument and triggering a canvas draw, and `mainloop` |
| 3274 | + # should be a function taking no argument and starting the backend main |
| 3275 | + # loop. |
| 3276 | + trigger_manager_draw = None |
| 3277 | + mainloop = None |
| 3278 | + |
| 3279 | + # The following methods will be automatically defined and exported, but |
| 3280 | + # can be overridden. |
| 3281 | + |
| 3282 | + @classmethod |
| 3283 | + def new_figure_manager(cls, num, *args, **kwargs): |
| 3284 | + """Create a new figure manager instance. |
| 3285 | + """ |
| 3286 | + # This import needs to happen here due to circular imports. |
| 3287 | + from matplotlib.figure import Figure |
| 3288 | + fig_cls = kwargs.pop('FigureClass', Figure) |
| 3289 | + fig = fig_cls(*args, **kwargs) |
| 3290 | + return cls.new_figure_manager_given_figure(num, fig) |
| 3291 | + |
| 3292 | + @classmethod |
| 3293 | + def new_figure_manager_given_figure(cls, num, figure): |
| 3294 | + """Create a new figure manager instance for the given figure. |
| 3295 | + """ |
| 3296 | + canvas = cls.FigureCanvas(figure) |
| 3297 | + manager = cls.FigureManager(canvas, num) |
| 3298 | + return manager |
| 3299 | + |
| 3300 | + @classmethod |
| 3301 | + def draw_if_interactive(cls): |
| 3302 | + if cls.trigger_manager_draw is not None and is_interactive(): |
| 3303 | + manager = Gcf.get_active() |
| 3304 | + if manager: |
| 3305 | + cls.trigger_manager_draw(manager) |
| 3306 | + |
| 3307 | + @classmethod |
| 3308 | + def show(cls, block=None): |
| 3309 | + """Show all figures. |
| 3310 | +
|
| 3311 | + `show` blocks by calling `mainloop` if *block* is ``True``, or if it |
| 3312 | + is ``None`` and we are neither in IPython's ``%pylab`` mode, nor in |
| 3313 | + `interactive` mode. |
| 3314 | + """ |
| 3315 | + if cls.mainloop is None: |
| 3316 | + frame = inspect.currentframe() |
| 3317 | + while frame: |
| 3318 | + if frame.f_code.co_filename in [ |
| 3319 | + "<stdin>", "<ipython console>"]: |
| 3320 | + warnings.warn("""\ |
| 3321 | +Your currently selected backend does not support show(). |
| 3322 | +Please select a GUI backend in your matplotlibrc file ('{}') |
| 3323 | +or with matplotlib.use()""".format(matplotlib.matplotlib_fname())) |
| 3324 | + break |
| 3325 | + else: |
| 3326 | + frame = frame.f_back |
| 3327 | + return |
| 3328 | + managers = Gcf.get_all_fig_managers() |
| 3329 | + if not managers: |
| 3330 | + return |
| 3331 | + for manager in managers: |
| 3332 | + manager.show() |
| 3333 | + if block is None: |
| 3334 | + # Hack: Are we in IPython's pylab mode? |
| 3335 | + from matplotlib import pyplot |
| 3336 | + try: |
| 3337 | + # IPython versions >= 0.10 tack the _needmain attribute onto |
| 3338 | + # pyplot.show, and always set it to False, when in %pylab mode. |
| 3339 | + ipython_pylab = not pyplot.show._needmain |
| 3340 | + except AttributeError: |
| 3341 | + ipython_pylab = False |
| 3342 | + block = not ipython_pylab and not is_interactive() |
| 3343 | + # TODO: The above is a hack to get the WebAgg backend working with |
| 3344 | + # ipython's `%pylab` mode until proper integration is implemented. |
| 3345 | + if get_backend() == "WebAgg": |
| 3346 | + block = True |
| 3347 | + if block: |
| 3348 | + cls.mainloop() |
| 3349 | + |
| 3350 | + # This method is the one actually exporting the required methods. |
| 3351 | + |
| 3352 | + @staticmethod |
| 3353 | + def export(cls): |
| 3354 | + for name in ["backend_version", |
| 3355 | + "FigureCanvas", |
| 3356 | + "FigureManager", |
| 3357 | + "new_figure_manager", |
| 3358 | + "new_figure_manager_given_figure", |
| 3359 | + "draw_if_interactive", |
| 3360 | + "show"]: |
| 3361 | + setattr(sys.modules[cls.__module__], name, getattr(cls, name)) |
| 3362 | + |
| 3363 | + # For back-compatibility, generate a shim `Show` class. |
| 3364 | + |
| 3365 | + class Show(ShowBase): |
| 3366 | + def mainloop(self): |
| 3367 | + return cls.mainloop() |
| 3368 | + |
| 3369 | + setattr(sys.modules[cls.__module__], "Show", Show) |
| 3370 | + return cls |
| 3371 | + |
| 3372 | + |
| 3373 | +class ShowBase(_Backend): |
| 3374 | + """ |
| 3375 | + Simple base class to generate a show() callable in backends. |
| 3376 | +
|
| 3377 | + Subclass must override mainloop() method. |
| 3378 | + """ |
| 3379 | + |
| 3380 | + def __call__(self, block=None): |
| 3381 | + return self.show(block=block) |
0 commit comments