|
13 | 13 | ) |
14 | 14 |
|
15 | 15 | from matplotlib import rcParams |
16 | | -from matplotlib.figure import Figure |
17 | 16 | from matplotlib import is_interactive |
18 | 17 | from matplotlib.backends.backend_webagg_core import (FigureManagerWebAgg, |
19 | 18 | FigureCanvasWebAggCore, |
20 | 19 | NavigationToolbar2WebAgg, |
21 | 20 | TimerTornado) |
22 | | -from matplotlib.backend_bases import ShowBase, NavigationToolbar2, cursors |
| 21 | +from matplotlib.backend_bases import NavigationToolbar2, cursors, _Backend |
| 22 | +from matplotlib._pylab_helpers import Gcf |
23 | 23 |
|
24 | 24 | from ._version import js_semver |
25 | 25 |
|
|
32 | 32 | } |
33 | 33 |
|
34 | 34 |
|
35 | | -class Show(ShowBase): |
36 | | - |
37 | | - def __call__(self, block=None): |
38 | | - from matplotlib._pylab_helpers import Gcf |
39 | | - |
40 | | - managers = Gcf.get_all_fig_managers() |
41 | | - if not managers: |
42 | | - return |
43 | | - |
44 | | - interactive = is_interactive() |
45 | | - |
46 | | - for manager in managers: |
47 | | - manager.show() |
48 | | - |
49 | | - # plt.figure adds an event which puts the figure in focus |
50 | | - # in the activeQue. Disable this behaviour, as it results in |
51 | | - # figures being put as the active figure after they have been |
52 | | - # shown, even in non-interactive mode. |
53 | | - if hasattr(manager, '_cidgcf'): |
54 | | - manager.canvas.mpl_disconnect(manager._cidgcf) |
55 | | - |
56 | | - if not interactive and manager in Gcf._activeQue: |
57 | | - Gcf._activeQue.remove(manager) |
58 | | - |
59 | | - |
60 | | -show = Show() |
61 | | - |
62 | | - |
63 | | -def draw_if_interactive(): |
64 | | - import matplotlib._pylab_helpers as pylab_helpers |
65 | | - |
66 | | - if is_interactive(): |
67 | | - manager = pylab_helpers.Gcf.get_active() |
68 | | - if manager is not None: |
69 | | - manager.show() |
70 | | - |
71 | | - |
72 | 35 | def connection_info(): |
73 | 36 | """ |
74 | 37 | Return a string showing the figure and connection status for |
@@ -260,33 +223,47 @@ def destroy(self): |
260 | 223 | self.canvas.close() |
261 | 224 |
|
262 | 225 |
|
263 | | -def new_figure_manager(num, *args, **kwargs): |
264 | | - """ |
265 | | - Create a new figure manager instance |
266 | | - """ |
267 | | - figure_class = kwargs.pop('FigureClass', Figure) |
268 | | - this_fig = figure_class(*args, **kwargs) |
269 | | - return new_figure_manager_given_figure(num, this_fig) |
| 226 | +@_Backend.export |
| 227 | +class _Backend_ipympl(_Backend): |
| 228 | + FigureCanvas = Canvas |
| 229 | + FigureManager = FigureManager |
270 | 230 |
|
| 231 | + @staticmethod |
| 232 | + def new_figure_manager_given_figure(num, figure): |
| 233 | + canvas = Canvas(figure) |
| 234 | + if 'nbagg.transparent' in rcParams and rcParams['nbagg.transparent']: |
| 235 | + figure.patch.set_alpha(0) |
| 236 | + manager = FigureManager(canvas, num) |
| 237 | + if is_interactive(): |
| 238 | + manager.show() |
| 239 | + figure.canvas.draw_idle() |
271 | 240 |
|
272 | | -def new_figure_manager_given_figure(num, figure): |
273 | | - """ |
274 | | - Create a new figure manager instance for the given figure. |
275 | | - """ |
276 | | - from matplotlib._pylab_helpers import Gcf |
| 241 | + def destroy(event): |
| 242 | + canvas.mpl_disconnect(cid) |
| 243 | + Gcf.destroy(manager) |
277 | 244 |
|
278 | | - def closer(event): |
279 | | - Gcf.destroy(num) |
| 245 | + cid = canvas.mpl_connect('close_event', destroy) |
| 246 | + return manager |
280 | 247 |
|
281 | | - canvas = Canvas(figure) |
282 | | - if 'nbagg.transparent' in rcParams and rcParams['nbagg.transparent']: |
283 | | - figure.patch.set_alpha(0) |
284 | | - manager = FigureManager(canvas, num) |
| 248 | + @staticmethod |
| 249 | + def show(block=None): |
| 250 | + # TODO: something to do when keyword block==False ? |
285 | 251 |
|
286 | | - if is_interactive(): |
287 | | - manager.show() |
288 | | - figure.canvas.draw_idle() |
| 252 | + managers = Gcf.get_all_fig_managers() |
| 253 | + if not managers: |
| 254 | + return |
| 255 | + |
| 256 | + interactive = is_interactive() |
| 257 | + |
| 258 | + for manager in managers: |
| 259 | + manager.show() |
289 | 260 |
|
290 | | - canvas.mpl_connect('close_event', closer) |
| 261 | + # plt.figure adds an event which makes the figure in focus the |
| 262 | + # active one. Disable this behaviour, as it results in |
| 263 | + # figures being put as the active figure after they have been |
| 264 | + # shown, even in non-interactive mode. |
| 265 | + if hasattr(manager, '_cidgcf'): |
| 266 | + manager.canvas.mpl_disconnect(manager._cidgcf) |
291 | 267 |
|
292 | | - return manager |
| 268 | + if not interactive: |
| 269 | + Gcf.figs.pop(manager.num, None) |
0 commit comments