Skip to content

Commit 50d4841

Browse files
Merge pull request #174 from martinRenou/remove_resize
Remove all resizing logic on the front-end
2 parents 742d758 + 53ea6b5 commit 50d4841

File tree

3 files changed

+21
-132
lines changed

3 files changed

+21
-132
lines changed

examples/ipympl.ipynb

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,26 @@
2626
{
2727
"cell_type": "code",
2828
"execution_count": null,
29-
"metadata": {
30-
"scrolled": false
31-
},
29+
"metadata": {},
3230
"outputs": [],
3331
"source": [
3432
"# Testing matplotlib interactions with a simple plot\n",
3533
"\n",
3634
"import matplotlib.pyplot as plt\n",
3735
"import numpy as np\n",
3836
"\n",
39-
"plt.figure(1)\n",
40-
"plt.plot(np.sin(np.linspace(0, 20, 100)))\n",
41-
"plt.show()"
37+
"fig = plt.figure()\n",
38+
"plt.plot(np.sin(np.linspace(0, 20, 100)))"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": null,
44+
"metadata": {},
45+
"outputs": [],
46+
"source": [
47+
"fig.canvas.toolbar_visible = False\n",
48+
"fig.canvas.header_visible = False"
4249
]
4350
},
4451
{
@@ -144,7 +151,7 @@
144151
" max=2.0\n",
145152
")\n",
146153
"\n",
147-
"fig = plt.figure(3)\n",
154+
"fig = plt.figure()\n",
148155
"\n",
149156
"x = np.linspace(0, 20, 500)\n",
150157
"\n",
@@ -156,23 +163,10 @@
156163
" fig.canvas.flush_events()\n",
157164
"\n",
158165
"slider.observe(update_lines, names='value')\n",
166+
"fig.canvas.toolbar_visible = False\n",
159167
"\n",
160168
"HBox([slider, fig.canvas])"
161169
]
162-
},
163-
{
164-
"cell_type": "code",
165-
"execution_count": null,
166-
"metadata": {},
167-
"outputs": [],
168-
"source": []
169-
},
170-
{
171-
"cell_type": "code",
172-
"execution_count": null,
173-
"metadata": {},
174-
"outputs": [],
175-
"source": []
176170
}
177171
],
178172
"metadata": {
@@ -191,9 +185,9 @@
191185
"name": "python",
192186
"nbconvert_exporter": "python",
193187
"pygments_lexer": "ipython3",
194-
"version": "3.7.3"
188+
"version": "3.8.1"
195189
}
196190
},
197191
"nbformat": 4,
198-
"nbformat_minor": 2
192+
"nbformat_minor": 4
199193
}

js/src/mpl_widget.js

Lines changed: 3 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
5555

5656
that.update_toolbar_position();
5757

58+
that.update_header_visible();
59+
that.update_toolbar_visible();
60+
5861
that.model_events();
5962

6063
that.send_initialization_message();
@@ -80,12 +83,10 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
8083

8184
update_header_visible: function() {
8285
this.header.style.display = this.model.get('header_visible') ? '': 'none';
83-
this.request_resize();
8486
},
8587

8688
update_toolbar_visible: function() {
8789
this.toolbar_view.el.style.display = this.model.get('toolbar_visible') ? '' : 'none';
88-
this.request_resize();
8990
},
9091

9192
update_toolbar_position: function() {
@@ -117,8 +118,6 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
117118
this.el.appendChild(this.toolbar_view.el);
118119
}
119120
}
120-
121-
this.request_resize();
122121
},
123122

124123
clear: function() {
@@ -227,60 +226,6 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
227226
this.figure.appendChild(this.footer);
228227
},
229228

230-
_calculate_decorations_size: function() {
231-
// Calculate the size of the decorations on the figure.
232-
var decorations_width = 0;
233-
var decorations_height = 0;
234-
235-
// Toolbar size
236-
var toolbar_position = this.model.get('toolbar_position');
237-
if (toolbar_position == 'top' || toolbar_position == 'bottom') {
238-
decorations_height += utils.get_full_size(this.toolbar_view.el).height;
239-
} else {
240-
decorations_width += utils.get_full_size(this.toolbar_view.el).width;
241-
}
242-
243-
// Label sizes
244-
decorations_height += utils.get_full_size(this.header).height;
245-
decorations_height += utils.get_full_size(this.footer).height;
246-
247-
// Margins on the canvas
248-
var canvas_div_margins = utils.get_margin_size(this.canvas_div);
249-
decorations_width += canvas_div_margins.width;
250-
decorations_height += canvas_div_margins.height;
251-
252-
// Margins on the figure div
253-
var figure_margins = utils.get_margin_size(this.figure);
254-
decorations_width += figure_margins.width;
255-
decorations_height += figure_margins.height;
256-
257-
return {
258-
width: decorations_width,
259-
height: decorations_height
260-
};
261-
},
262-
263-
request_resize: function() {
264-
// Ensure that the image already exists. We ignore the first calls to resize
265-
// because we want the widget to first adapt to the figure size set in
266-
// matplotlib.
267-
if (!this.image.src) {
268-
return;
269-
}
270-
271-
// Using the given widget size, figure out how big the canvas should be.
272-
var decorations_size = this._calculate_decorations_size();
273-
274-
var new_canvas_width = this.el.clientWidth - decorations_size.width;
275-
var new_canvas_height = this.el.clientHeight - decorations_size.height;
276-
277-
// Ensure that the canvas size is a positive number.
278-
new_canvas_width = new_canvas_width < 1 ? 1 : new_canvas_width;
279-
new_canvas_height = new_canvas_height < 1 ? 1 : new_canvas_height;
280-
281-
this.send_message('resize', {'width': new_canvas_width, 'height': new_canvas_height});
282-
},
283-
284229
_resize_canvas: function(width, height) {
285230
// Keep the size of the canvas, and rubber band canvas in sync.
286231
this.canvas.setAttribute('width', width * this.ratio);
@@ -292,13 +237,6 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
292237

293238
this.canvas_div.style.width = width + 'px';
294239
this.canvas_div.style.height = height + 'px';
295-
296-
// Figure out the widget size.
297-
var decorations_size = this._calculate_decorations_size();
298-
299-
// Reset the widget size to adapt to this figure.
300-
this.el.style.width = width + decorations_size.width + 'px';
301-
this.el.style.height = height + decorations_size.height + 'px';
302240
},
303241

304242
send_message: function(type, message = {}) {
@@ -431,17 +369,6 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
431369
}
432370
},
433371

434-
processPhosphorMessage: function(msg) {
435-
MPLCanvasView.__super__.processPhosphorMessage.apply(this, arguments);
436-
437-
switch (msg.type) {
438-
case 'resize':
439-
this.request_resize();
440-
break;
441-
}
442-
},
443-
444-
445372
mouse_event: function(name) {
446373
var that = this;
447374
var last_update = 0;

js/src/utils.js

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -41,41 +41,9 @@ function get_simple_keys(original) {
4141
}, {});
4242
}
4343

44-
/*
45-
* Return the total size of the margins for an element in both width and height.
46-
*/
47-
function get_margin_size(el) {
48-
var style = getComputedStyle(el);
49-
50-
var margin_width = parseFloat(style.marginLeft) + parseFloat(style.marginRight);
51-
var margin_height = parseFloat(style.marginTop) + parseFloat(style.marginBottom);
52-
53-
return {
54-
width: margin_width,
55-
height: margin_height,
56-
};
57-
}
58-
59-
60-
/*
61-
* Return the full size of an element, including margins.
62-
*/
63-
function get_full_size(el) {
64-
var margin_size = get_margin_size(el);
65-
66-
var full_width = el.scrollWidth + margin_size.width;
67-
var full_height = el.scrollHeight + margin_size.height;
68-
69-
return {
70-
width: full_width,
71-
height: full_height,
72-
};
73-
}
7444

7545
module.exports = {
7646
offset: offset,
7747
get_mouse_position: get_mouse_position,
78-
get_simple_keys: get_simple_keys,
79-
get_margin_size: get_margin_size,
80-
get_full_size: get_full_size,
48+
get_simple_keys: get_simple_keys
8149
}

0 commit comments

Comments
 (0)