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

Skip to content

Commit 38208bb

Browse files
committed
Cleanups to webagg & friends.
FigureCanvasWebAgg and FigureCanvasNbAgg empty subclasses of FigureCanvasWebAggCore. They only differed in that WebAggCore did not define the (common) timer class, which is easily fixed (but the TimerTornado definition needs to be moved above the FigureCanvasWegAggCore definition), and that WebAgg explicitly redefined `show` to use the module's `show`, whereas WebAggCore's `show` uses `pyplot.show`... but `pyplot.show` is defined as calling the backend module's `show`, so it comes down to the same. Still it's useful for them to be subclasses rather than straight aliases, to keep the possibility of a canvas->manager mapping. No need to explicitly mark WebAggCore as `supports_blit`, as that's autodetected now (one can easily check that `FigureCanvasWebAggCore.supports_blit` is still True). Saving `pgf` to a BytesIO actually works fine nowadays, and can indeed by manually tested on WebAgg.
1 parent cf4f8e1 commit 38208bb

File tree

4 files changed

+47
-50
lines changed

4 files changed

+47
-50
lines changed

lib/matplotlib/backends/backend_nbagg.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def connection_info():
5757
'download': 'fa fa-floppy-o icon-save',
5858
None: None
5959
}
60+
FigureCanvasNbAgg = FigureCanvasWebAggCore # Backcompat alias.
6061

6162

6263
class NavigationIPy(NavigationToolbar2WebAgg):
@@ -142,7 +143,7 @@ def remove_comm(self, comm_id):
142143

143144

144145
class FigureCanvasNbAgg(FigureCanvasWebAggCore):
145-
_timer_cls = TimerTornado
146+
pass
146147

147148

148149
class CommSocket:

lib/matplotlib/backends/backend_webagg.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,7 @@ def run(self):
4848

4949

5050
class FigureCanvasWebAgg(core.FigureCanvasWebAggCore):
51-
_timer_cls = TimerTornado
52-
53-
def show(self):
54-
# show the figure window
55-
global show # placates pyflakes: created by @_Backend.export below
56-
show()
51+
pass
5752

5853

5954
class WebAggApplication(tornado.web.Application):

lib/matplotlib/backends/backend_webagg_core.py

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,43 @@ def _handle_key(key):
117117
return key
118118

119119

120+
class TimerTornado(backend_bases.TimerBase):
121+
def __init__(self, *args, **kwargs):
122+
self._timer = None
123+
super().__init__(*args, **kwargs)
124+
125+
def _timer_start(self):
126+
self._timer_stop()
127+
if self._single:
128+
ioloop = tornado.ioloop.IOLoop.instance()
129+
self._timer = ioloop.add_timeout(
130+
datetime.timedelta(milliseconds=self.interval),
131+
self._on_timer)
132+
else:
133+
self._timer = tornado.ioloop.PeriodicCallback(
134+
self._on_timer,
135+
max(self.interval, 1e-6))
136+
self._timer.start()
137+
138+
def _timer_stop(self):
139+
if self._timer is None:
140+
return
141+
elif self._single:
142+
ioloop = tornado.ioloop.IOLoop.instance()
143+
ioloop.remove_timeout(self._timer)
144+
else:
145+
self._timer.stop()
146+
self._timer = None
147+
148+
def _timer_set_interval(self):
149+
# Only stop and restart it if the timer has already been started
150+
if self._timer is not None:
151+
self._timer_stop()
152+
self._timer_start()
153+
154+
120155
class FigureCanvasWebAggCore(backend_agg.FigureCanvasAgg):
121-
supports_blit = True
156+
_timer_cls = TimerTornado
122157

123158
def __init__(self, *args, **kwargs):
124159
super().__init__(*args, **kwargs)
@@ -478,8 +513,7 @@ def get_javascript(cls, stream=None):
478513
for filetype, ext in sorted(FigureCanvasWebAggCore.
479514
get_supported_filetypes_grouped().
480515
items()):
481-
if ext[0] != 'pgf': # pgf does not support BytesIO
482-
extensions.append(ext[0])
516+
extensions.append(ext[0])
483517
output.write("mpl.extensions = {0};\n\n".format(
484518
json.dumps(extensions)))
485519

@@ -499,41 +533,6 @@ def _send_event(self, event_type, **kwargs):
499533
s.send_json(payload)
500534

501535

502-
class TimerTornado(backend_bases.TimerBase):
503-
def __init__(self, *args, **kwargs):
504-
self._timer = None
505-
super().__init__(*args, **kwargs)
506-
507-
def _timer_start(self):
508-
self._timer_stop()
509-
if self._single:
510-
ioloop = tornado.ioloop.IOLoop.instance()
511-
self._timer = ioloop.add_timeout(
512-
datetime.timedelta(milliseconds=self.interval),
513-
self._on_timer)
514-
else:
515-
self._timer = tornado.ioloop.PeriodicCallback(
516-
self._on_timer,
517-
max(self.interval, 1e-6))
518-
self._timer.start()
519-
520-
def _timer_stop(self):
521-
if self._timer is None:
522-
return
523-
elif self._single:
524-
ioloop = tornado.ioloop.IOLoop.instance()
525-
ioloop.remove_timeout(self._timer)
526-
else:
527-
self._timer.stop()
528-
self._timer = None
529-
530-
def _timer_set_interval(self):
531-
# Only stop and restart it if the timer has already been started
532-
if self._timer is not None:
533-
self._timer_stop()
534-
self._timer_start()
535-
536-
537536
@_Backend.export
538537
class _BackendWebAggCoreAgg(_Backend):
539538
FigureCanvas = FigureCanvasWebAggCore

lib/matplotlib/tests/test_backends_interactive.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def _get_testable_interactive_backends():
7373
# early. Also, gtk3 redefines key_press_event with a different signature, so
7474
# we directly invoke it from the superclass instead.
7575
def _test_interactive_impl():
76-
import importlib.util
76+
import importlib
7777
import io
7878
import json
7979
import sys
@@ -113,8 +113,9 @@ def check_alt_backend(alt_backend):
113113
mpl.use(alt_backend, force=True)
114114
fig = plt.figure()
115115
assert_equal(
116-
type(fig.canvas).__module__,
117-
"matplotlib.backends.backend_{}".format(alt_backend))
116+
type(fig.canvas),
117+
importlib.import_module(
118+
f"matplotlib.backends.backend_{alt_backend}").FigureCanvas)
118119

119120
if importlib.util.find_spec("cairocffi"):
120121
check_alt_backend(backend[:-3] + "cairo")
@@ -124,8 +125,9 @@ def check_alt_backend(alt_backend):
124125

125126
fig, ax = plt.subplots()
126127
assert_equal(
127-
type(fig.canvas).__module__,
128-
"matplotlib.backends.backend_{}".format(backend))
128+
type(fig.canvas),
129+
importlib.import_module(
130+
f"matplotlib.backends.backend_{backend}").FigureCanvas)
129131

130132
ax.plot([0, 1], [2, 3])
131133

0 commit comments

Comments
 (0)