Skip to content

Commit 8cabd22

Browse files
committed
Implement hold_canvas context manager
1 parent 759519a commit 8cabd22

File tree

3 files changed

+27
-33
lines changed

3 files changed

+27
-33
lines changed

examples/numpy_heatmap.ipynb

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"outputs": [],
88
"source": [
99
"import numpy as np\n",
10-
"from ipycanvas import Canvas"
10+
"from ipycanvas import Canvas, hold_canvas"
1111
]
1212
},
1313
{
@@ -44,7 +44,8 @@
4444
"source": [
4545
"n_pixels = 10\n",
4646
"\n",
47-
"canvas = Canvas(size=(z.shape[0] * n_pixels, z.shape[1] * n_pixels))"
47+
"canvas = Canvas(size=(z.shape[0] * n_pixels, z.shape[1] * n_pixels))\n",
48+
"canvas"
4849
]
4950
},
5051
{
@@ -56,34 +57,17 @@
5657
"min = np.min(z)\n",
5758
"max = np.max(z)\n",
5859
"\n",
59-
"canvas.caching = True\n",
60-
"r = 0\n",
61-
"for row in z:\n",
62-
" c = 0\n",
63-
" for value in row:\n",
64-
" canvas.fill_style = colormap(value, min, max)\n",
65-
" canvas.fill_rect(r * n_pixels, c * n_pixels, n_pixels, n_pixels)\n",
66-
" \n",
67-
" c += 1\n",
68-
" r += 1"
69-
]
70-
},
71-
{
72-
"cell_type": "code",
73-
"execution_count": null,
74-
"metadata": {},
75-
"outputs": [],
76-
"source": [
77-
"canvas"
78-
]
79-
},
80-
{
81-
"cell_type": "code",
82-
"execution_count": null,
83-
"metadata": {},
84-
"outputs": [],
85-
"source": [
86-
"canvas.flush()"
60+
"# Holding the canvas during the entire drawing process\n",
61+
"with hold_canvas(canvas):\n",
62+
" r = 0\n",
63+
" for row in z:\n",
64+
" c = 0\n",
65+
" for value in row:\n",
66+
" canvas.fill_style = colormap(value, min, max)\n",
67+
" canvas.fill_rect(r * n_pixels, c * n_pixels, n_pixels, n_pixels)\n",
68+
"\n",
69+
" c += 1\n",
70+
" r += 1"
8771
]
8872
}
8973
],

ipycanvas/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Copyright (c) Martin Renou.
55
# Distributed under the terms of the Modified BSD License.
66

7-
from .canvas import Canvas
8-
from ._version import __version__, version_info
7+
from .canvas import Canvas, hold_canvas # noqa
8+
from ._version import __version__, version_info # noqa
99

10-
from .nbextension import _jupyter_nbextension_paths
10+
from .nbextension import _jupyter_nbextension_paths # noqa

ipycanvas/canvas.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# Copyright (c) Martin Renou.
55
# Distributed under the terms of the Modified BSD License.
66

7+
from contextlib import contextmanager
8+
79
from ipywidgets import Color, DOMWidget
810

911
from traitlets import Float, Tuple, Unicode, observe
@@ -133,3 +135,11 @@ def _send_command(self, command):
133135
self._commands_cache.append(command)
134136
else:
135137
self.send(command)
138+
139+
140+
@contextmanager
141+
def hold_canvas(canvas):
142+
"""Hold any drawing on the canvas, and perform only one draw command at the end."""
143+
canvas.caching = True
144+
yield
145+
canvas.flush()

0 commit comments

Comments
 (0)