@@ -92,7 +92,7 @@ def get_diff_image(self):
92
92
# pixels can be compared in one numpy call, rather than
93
93
# needing to compare each plane separately.
94
94
buff = np .frombuffer (
95
- self ._renderer .buffer_rgba (), dtype = np .uint32 )
95
+ self .get_renderer () .buffer_rgba (), dtype = np .uint32 )
96
96
buff .shape = (
97
97
self ._renderer .height , self ._renderer .width )
98
98
@@ -129,7 +129,7 @@ def get_diff_image(self):
129
129
130
130
def get_renderer (self , cleared = None ):
131
131
# Mirrors super.get_renderer, but caches the old one
132
- # so that we can do things such as prodce a diff image
132
+ # so that we can do things such as produce a diff image
133
133
# in get_diff_image
134
134
_ , _ , w , h = self .figure .bbox .bounds
135
135
key = w , h , self .figure .dpi
@@ -200,6 +200,26 @@ def handle_event(self, event):
200
200
self .send_event ('figure_label' , label = figure_label )
201
201
self ._force_full = True
202
202
self .draw_idle ()
203
+ else :
204
+ handler = getattr (self , 'handle_{}' .format (e_type ), None )
205
+ if handler is None :
206
+ import warnings
207
+ warnings .warn ('Unhandled message type {}. {}' .format (e_type , event ))
208
+ else :
209
+ return handler (event )
210
+
211
+ def handle_resize (self , event ):
212
+ x , y = event .get ('width' , 800 ), event .get ('height' , 800 )
213
+ x , y = int (x ), int (y )
214
+ fig = self .figure
215
+ # An attempt at approximating the figure size in pixels.
216
+ fig .set_size_inches (x / fig .dpi , y / fig .dpi )
217
+
218
+ _ , _ , w , h = self .figure .bbox .bounds
219
+ # Acknowledge the resize, and force the viewer to update the canvas size to the
220
+ # figure's new size (which is hopefully identical or within a pixel or so).
221
+ self ._png_is_old = True
222
+ self .manager .resize (w , h )
203
223
204
224
def send_event (self , event_type , ** kwargs ):
205
225
self .manager ._send_event (event_type , ** kwargs )
@@ -216,7 +236,54 @@ def stop_event_loop(self):
216
236
backend_bases .FigureCanvasBase .stop_event_loop_default .__doc__
217
237
218
238
239
+ class NavigationToolbar2WebAgg (backend_bases .NavigationToolbar2 ):
240
+ _jquery_icon_classes = {
241
+ 'home' : 'ui-icon ui-icon-home' ,
242
+ 'back' : 'ui-icon ui-icon-circle-arrow-w' ,
243
+ 'forward' : 'ui-icon ui-icon-circle-arrow-e' ,
244
+ 'zoom_to_rect' : 'ui-icon ui-icon-search' ,
245
+ 'move' : 'ui-icon ui-icon-arrow-4' ,
246
+ 'download' : 'ui-icon ui-icon-disk' ,
247
+ None : None ,
248
+ }
249
+
250
+ # Use the standard toolbar items + download button
251
+ toolitems = [(text , tooltip_text , _jquery_icon_classes [image_file ], name_of_method )
252
+ for text , tooltip_text , image_file , name_of_method
253
+ in (backend_bases .NavigationToolbar2 .toolitems +
254
+ (('Download' , 'Download plot' , 'download' , 'download' ),))
255
+ if image_file in _jquery_icon_classes ]
256
+
257
+ def _init_toolbar (self ):
258
+ self .message = ''
259
+ self .cursor = 0
260
+
261
+ def set_message (self , message ):
262
+ if message != self .message :
263
+ self .canvas .send_event ("message" , message = message )
264
+ self .message = message
265
+
266
+ def set_cursor (self , cursor ):
267
+ if cursor != self .cursor :
268
+ self .canvas .send_event ("cursor" , cursor = cursor )
269
+ self .cursor = cursor
270
+
271
+ def dynamic_update (self ):
272
+ self .canvas .draw_idle ()
273
+
274
+ def draw_rubberband (self , event , x0 , y0 , x1 , y1 ):
275
+ self .canvas .send_event (
276
+ "rubberband" , x0 = x0 , y0 = y0 , x1 = x1 , y1 = y1 )
277
+
278
+ def release_zoom (self , event ):
279
+ super (NavigationToolbar2WebAgg , self ).release_zoom (event )
280
+ self .canvas .send_event (
281
+ "rubberband" , x0 = - 1 , y0 = - 1 , x1 = - 1 , y1 = - 1 )
282
+
283
+
219
284
class FigureManagerWebAgg (backend_bases .FigureManagerBase ):
285
+ ToolbarCls = NavigationToolbar2WebAgg
286
+
220
287
def __init__ (self , canvas , num ):
221
288
backend_bases .FigureManagerBase .__init__ (self , canvas , num )
222
289
@@ -228,7 +295,7 @@ def show(self):
228
295
pass
229
296
230
297
def _get_toolbar (self , canvas ):
231
- toolbar = NavigationToolbar2WebAgg (canvas )
298
+ toolbar = self . ToolbarCls (canvas )
232
299
return toolbar
233
300
234
301
def resize (self , w , h ):
@@ -275,7 +342,7 @@ def get_javascript(cls, stream=None):
275
342
output .write (fd .read ())
276
343
277
344
toolitems = []
278
- for name , tooltip , image , method in NavigationToolbar2WebAgg .toolitems :
345
+ for name , tooltip , image , method in cls . ToolbarCls .toolitems :
279
346
if name is None :
280
347
toolitems .append (['' , '' , '' , '' ])
281
348
else :
@@ -306,52 +373,3 @@ def _send_event(self, event_type, **kwargs):
306
373
payload .update (kwargs )
307
374
for s in self .web_sockets :
308
375
s .send_json (payload )
309
-
310
-
311
- class NavigationToolbar2WebAgg (backend_bases .NavigationToolbar2 ):
312
- _jquery_icon_classes = {
313
- 'home' : 'ui-icon ui-icon-home' ,
314
- 'back' : 'ui-icon ui-icon-circle-arrow-w' ,
315
- 'forward' : 'ui-icon ui-icon-circle-arrow-e' ,
316
- 'zoom_to_rect' : 'ui-icon ui-icon-search' ,
317
- 'move' : 'ui-icon ui-icon-arrow-4' ,
318
- 'download' : 'ui-icon ui-icon-disk' ,
319
- None : None
320
- }
321
-
322
-
323
- # Use the standard toolbar items + download button
324
- toolitems = [(text , tooltip_text , _jquery_icon_classes [image_file ], name_of_method )
325
- for text , tooltip_text , image_file , name_of_method
326
- in (backend_bases .NavigationToolbar2 .toolitems +
327
- (('Download' , 'Download plot' , 'download' , 'download' ),))
328
- if image_file in _jquery_icon_classes ]
329
-
330
- def _init_toolbar (self ):
331
- self .message = ''
332
- self .cursor = 0
333
-
334
- def set_message (self , message ):
335
- if message != self .message :
336
- self .canvas .send_event ("message" , message = message )
337
- self .message = message
338
-
339
- def set_cursor (self , cursor ):
340
- if cursor != self .cursor :
341
- self .canvas .send_event ("cursor" , cursor = cursor )
342
- self .cursor = cursor
343
-
344
- def dynamic_update (self ):
345
- self .canvas .draw_idle ()
346
-
347
- def draw_rubberband (self , event , x0 , y0 , x1 , y1 ):
348
- self .canvas .send_event (
349
- "rubberband" , x0 = x0 , y0 = y0 , x1 = x1 , y1 = y1 )
350
-
351
- def release_zoom (self , event ):
352
- super (NavigationToolbar2WebAgg , self ).release_zoom (event )
353
- self .canvas .send_event (
354
- "rubberband" , x0 = - 1 , y0 = - 1 , x1 = - 1 , y1 = - 1 )
355
-
356
- FigureCanvas = FigureCanvasWebAggCore
357
- FigureManager = FigureManagerWebAgg
0 commit comments