-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathmpl_widget.js
454 lines (371 loc) · 14.8 KB
/
mpl_widget.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
var widgets = require('@jupyter-widgets/base');
var _ = require('lodash');
var toolbar = require('./toolbar_widget.js');
var utils = require('./utils.js');
require('./mpl_widget.css');
var version = require('../package.json').version;
var MPLCanvasModel = widgets.DOMWidgetModel.extend({
defaults: function() {
return _.extend(widgets.DOMWidgetModel.prototype.defaults(), {
_model_name: 'MPLCanvasModel',
_view_name: 'MPLCanvasView',
_model_module: 'jupyter-matplotlib',
_view_module: 'jupyter-matplotlib',
_model_module_version: '^'+ version,
_view_module_version: '^' + version,
toolbar: null,
toolbar_visible: true,
toolbar_position: 'horizontal'
});
}
}, {
serializers: _.extend({
toolbar: { deserialize: widgets.unpack_models }
}, widgets.DOMWidgetModel.serializers)
});
var MPLCanvasView = widgets.DOMWidgetView.extend({
render: function() {
this.canvas = undefined;
this.context = undefined;
this.rubberband_canvas = undefined;
this.rubberband_context = undefined;
this.image_mode = 'full';
this.figure = document.createElement('div');
this.figure.addEventListener('remove', this.close.bind(this));
this.figure.classList = 'jupyter-matplotlib-figure jupyter-widgets widget-container widget-box widget-vbox';
this._init_header();
this._init_canvas();
this._init_image();
this._init_footer();
this.waiting = false;
var that = this;
return this.create_child_view(this.model.get('toolbar')).then(function(toolbar_view) {
that.toolbar_view = toolbar_view;
that.update_toolbar_position();
that.model_events();
window.addEventListener('resize', that.request_resize.bind(that));
that.send_initialization_message();
});
},
model_events: function() {
this.model.on('msg:custom', this.on_comm_message.bind(this));
this.model.on('change:toolbar_visible', this.update_toolbar_visible.bind(this));
this.model.on('change:toolbar_position', this.update_toolbar_position.bind(this));
},
send_initialization_message: function() {
if (this.ratio != 1) {
this.send_message('set_dpi_ratio', {'dpi_ratio': this.ratio});
}
this.send_message('send_image_mode');
this.send_message('refresh');
this.send_message('initialized');
},
update_toolbar_visible: function() {
this.toolbar_view.el.style.display = this.model.get('toolbar_visible') ? '' : 'none';
this.request_resize();
},
update_toolbar_position: function() {
var toolbar_position = this.model.get('toolbar_position');
if (toolbar_position == 'top' || toolbar_position == 'bottom') {
this.el.classList = 'jupyter-widgets widget-container widget-box widget-vbox jupyter-matplotlib';
this.model.get('toolbar').set('orientation', 'horizontal');
this.clear();
if (toolbar_position == 'top') {
this.el.appendChild(this.toolbar_view.el);
this.el.appendChild(this.figure);
} else {
this.el.appendChild(this.figure);
this.el.appendChild(this.toolbar_view.el);
}
} else {
this.el.classList = 'jupyter-widgets widget-container widget-box widget-hbox jupyter-matplotlib';
this.model.get('toolbar').set('orientation', 'vertical');
this.clear();
if (toolbar_position == 'left') {
this.el.appendChild(this.toolbar_view.el);
this.el.appendChild(this.figure);
} else {
this.el.appendChild(this.figure);
this.el.appendChild(this.toolbar_view.el);
}
}
},
clear: function() {
while (this.el.firstChild) {
this.el.removeChild(this.el.firstChild);
}
},
_init_header: function() {
this.header = document.createElement('div');
this.header.style.textAlign = 'center';
this.header.classList = 'jupyter-widgets widget-label';
this.figure.appendChild(this.header);
},
_init_canvas: function() {
var canvas_div = this.canvas_div = document.createElement('div');
canvas_div.style.position = 'relative';
canvas_div.style.clear = 'both';
canvas_div.classList = 'jupyter-widgets jupyter-matplotlib-canvas_div';
canvas_div.addEventListener('keydown', this.key_event('key_press'));
canvas_div.addEventListener('keyup', this.key_event('key_release'));
// this is important to make the div 'focusable'
canvas_div.setAttribute('tabindex', 0);
this.figure.appendChild(canvas_div);
var canvas = this.canvas = document.createElement('canvas');
canvas.style.display = 'block';
canvas.style.position = 'absolute';
canvas.style.left = 0;
canvas.style.top = 0;
canvas.style.zIndex = 0;
this.context = canvas.getContext('2d');
var backingStore = this.context.backingStorePixelRatio ||
this.context.webkitBackingStorePixelRatio ||
this.context.mozBackingStorePixelRatio ||
this.context.msBackingStorePixelRatio ||
this.context.oBackingStorePixelRatio ||
this.context.backingStorePixelRatio || 1;
this.ratio = (window.devicePixelRatio || 1) / backingStore;
var rubberband_canvas = this.rubberband_canvas = document.createElement('canvas');
rubberband_canvas.style.display = 'block';
rubberband_canvas.style.position = 'absolute';
rubberband_canvas.style.left = 0;
rubberband_canvas.style.top = 0;
rubberband_canvas.style.zIndex = 1;
rubberband_canvas.addEventListener('mousedown', this.mouse_event('button_press'));
rubberband_canvas.addEventListener('mouseup', this.mouse_event('button_release'));
rubberband_canvas.addEventListener('mousemove', this.mouse_event('motion_notify'));
rubberband_canvas.addEventListener('mouseenter', this.mouse_event('figure_enter'));
rubberband_canvas.addEventListener('mouseleave', this.mouse_event('figure_leave'));
rubberband_canvas.addEventListener('wheel', this.mouse_event('scroll'));
canvas_div.appendChild(canvas);
canvas_div.appendChild(rubberband_canvas);
this.rubberband_context = rubberband_canvas.getContext('2d');
this.rubberband_context.strokeStyle = '#000000';
// Disable right mouse context menu.
this.rubberband_canvas.addEventListener('contextmenu', function(e) {
return false;
});
},
_init_image: function() {
var that = this;
this.image = document.createElement('img');
this.image.style.display = 'none';
this.figure.appendChild(this.image);
this.image.onload = function() {
if (that.image_mode == 'full') {
// Full images could contain transparency (where diff images
// almost always do), so we need to clear the canvas so that
// there is no ghosting.
that.context.clearRect(0, 0, that.canvas.width, that.canvas.height);
}
that.context.drawImage(that.image, 0, 0);
};
this.image.onunload = function() {
that.close();
}
},
_init_footer: function() {
this.footer = document.createElement('div');
this.footer.style.textAlign = 'center';
this.footer.classList = 'jupyter-widgets widget-label';
this.figure.appendChild(this.footer);
},
request_resize: function() {
// Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,
// which will in turn request a refresh of the image.
var rect = this.canvas_div.getBoundingClientRect();
this.send_message('resize', {'width': rect.width, 'height': rect.height});
},
_resize_canvas: function(width, height) {
// Keep the size of the canvas, and rubber band canvas in sync.
this.canvas.setAttribute('width', width * this.ratio);
this.canvas.setAttribute('height', height * this.ratio);
this.canvas.style.width = width + 'px';
this.rubberband_canvas.setAttribute('width', width);
this.rubberband_canvas.setAttribute('height', height);
},
send_message: function(type, message = {}) {
message['type'] = type;
this.send(message);
},
send_draw_message: function() {
if (!this.waiting) {
this.waiting = true;
this.send_message('draw');
}
},
handle_save: function() {
var save = document.createElement('a');
save.href = this.canvas.toDataURL();
save.download = this.header.textContent + '.png';
document.body.appendChild(save);
save.click();
document.body.removeChild(save);
},
handle_resize: function(msg) {
var size = msg['size'];
if (size[0] != this.canvas.width || size[1] != this.canvas.height) {
this._resize_canvas(size[0], size[1]);
this.send_message('refresh');
};
},
handle_rubberband: function(msg) {
var x0 = msg['x0'] / this.ratio;
var y0 = (this.canvas.height - msg['y0']) / this.ratio;
var x1 = msg['x1'] / this.ratio;
var y1 = (this.canvas.height - msg['y1']) / this.ratio;
x0 = Math.floor(x0) + 0.5;
y0 = Math.floor(y0) + 0.5;
x1 = Math.floor(x1) + 0.5;
y1 = Math.floor(y1) + 0.5;
var min_x = Math.min(x0, x1);
var min_y = Math.min(y0, y1);
var width = Math.abs(x1 - x0);
var height = Math.abs(y1 - y0);
this.rubberband_context.clearRect(
0, 0, this.canvas.width, this.canvas.height);
this.rubberband_context.strokeRect(min_x, min_y, width, height);
},
handle_figure_label: function(msg) {
// Updates the figure title.
this.header.textContent = msg['label'];
},
handle_message: function(msg) {
this.footer.textContent = msg['message'];
},
handle_cursor: function(msg) {
var cursor = msg['cursor'];
switch(cursor)
{
case 0:
cursor = 'pointer';
break;
case 1:
cursor = 'default';
break;
case 2:
cursor = 'crosshair';
break;
case 3:
cursor = 'move';
break;
}
this.rubberband_canvas.style.cursor = cursor;
},
handle_draw: function(msg) {
// Request the server to send over a new figure.
this.send_draw_message();
},
handle_binary: function(msg, dataviews) {
var url_creator = window.URL || window.webkitURL;
var buffer = new Uint8Array(dataviews[0].buffer);
var blob = new Blob([buffer], {type: 'image/png'});
var image_url = url_creator.createObjectURL(blob);
// Free the memory for the previous frames
if (this.image.src) {
url_creator.revokeObjectURL(this.image.src);
}
this.image.src = image_url;
// Tell Jupyter that the notebook contents must change.
this.send_message('ack');
this.waiting = false;
},
handle_image_mode: function(msg) {
this.image_mode = msg['mode'];
},
on_comm_message: function(evt, dataviews) {
var msg = JSON.parse(evt.data);
var msg_type = msg['type'];
// Call the 'handle_{type}' callback, which takes
// the figure and JSON message as its only arguments.
try {
var callback = this['handle_' + msg_type].bind(this);
} catch (e) {
console.log('No handler for the \'' + msg_type + '\' message type: ', msg);
return;
}
if (callback) {
try {
callback(msg, dataviews);
} catch (e) {
console.log('Exception inside the \'handler_' + msg_type + '\' callback:', e, e.stack, msg);
}
}
},
processPhosphorMessage: function(msg) {
MPLCanvasView.__super__.processPhosphorMessage.apply(this, arguments);
switch (msg.type) {
case 'resize':
// case 'after-show':
this.request_resize();
break;
}
},
mouse_event: function(name) {
var that = this;
return function(event) {
var canvas_pos = utils.get_mouse_position(event);
if (name === 'scroll') {
event['data'] = 'scroll'
if (event.deltaY < 0) {
event.step = 1;
} else {
event.step = -1;
}
}
if (name === 'button_press') {
that.canvas.focus();
that.canvas_div.focus();
}
var x = canvas_pos.x * that.ratio;
var y = canvas_pos.y * that.ratio;
that.send_message(name, {x: x, y: y, button: event.button,
step: event.step,
guiEvent: utils.get_simple_keys(event)});
/* This prevents the web browser from automatically changing to
* the text insertion cursor when the button is pressed. We want
* to control all of the cursor setting manually through the
* 'cursor' event from matplotlib */
event.preventDefault();
return false;
};
},
key_event: function(name) {
var that = this;
return function(event) {
event.stopPropagation();
event.preventDefault();
// Prevent repeat events
if (name == 'key_press') {
if (event.which === that._key)
return;
else
that._key = event.which;
}
if (name == 'key_release') {
that._key = null;
}
var value = '';
if (event.ctrlKey && event.which != 17)
value += 'ctrl+';
if (event.altKey && event.which != 18)
value += 'alt+';
if (event.shiftKey && event.which != 16)
value += 'shift+';
value += 'k';
value += event.which.toString();
that.send_message(name, {key: value, guiEvent: utils.get_simple_keys(event)});
return false;
};
},
close: function(){
this.send_message('closing');
this.trigger('close');
}
});
module.exports = {
MPLCanvasModel: MPLCanvasModel,
MPLCanvasView: MPLCanvasView,
ToolbarModel: toolbar.ToolbarModel,
ToolbarView: toolbar.ToolbarView
}