|
4 | 4 | "cell_type": "markdown",
|
5 | 5 | "metadata": {},
|
6 | 6 | "source": [
|
7 |
| - "## Draggable Label" |
| 7 | + "# Drag and Drop\n", |
| 8 | + "\n", |
| 9 | + "In this notebook we introduce the `DraggableBox` and `DropBox` widgets, that can be used to drag and drop widgets." |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "## Draggable Box" |
8 | 17 | ]
|
9 | 18 | },
|
10 | 19 | {
|
11 | 20 | "cell_type": "markdown",
|
12 | 21 | "metadata": {},
|
13 | 22 | "source": [
|
14 |
| - "`DraggableLabel` is a label that can be dragged and dropped to other fields." |
| 23 | + "`DraggableBox` is a widget that wraps other widgets and makes them draggable.\n", |
| 24 | + "\n", |
| 25 | + "For example we can build a custom `DraggableLabel` as follows:" |
15 | 26 | ]
|
16 | 27 | },
|
17 | 28 | {
|
|
20 | 31 | "metadata": {},
|
21 | 32 | "outputs": [],
|
22 | 33 | "source": [
|
23 |
| - "from ipywidgets import Label, DraggableBox, DropBox, Textarea, VBox" |
| 34 | + "from ipywidgets import Label, DraggableBox, Textarea" |
24 | 35 | ]
|
25 | 36 | },
|
26 | 37 | {
|
|
29 | 40 | "metadata": {},
|
30 | 41 | "outputs": [],
|
31 | 42 | "source": [
|
32 |
| - "def set_drag_data(box):\n", |
33 |
| - " box.drag_data['text/plain'] = box.children[0].value\n", |
34 |
| - "\n", |
35 | 43 | "def DraggableLabel(value, draggable=True):\n",
|
36 | 44 | " box = DraggableBox(Label(value))\n",
|
37 | 45 | " box.draggable = draggable\n",
|
|
51 | 59 | "cell_type": "markdown",
|
52 | 60 | "metadata": {},
|
53 | 61 | "source": [
|
54 |
| - "You can drag this label anywhere (could be your shell etc.), but also to a text area:" |
| 62 | + "You can drag this label anywhere (could be your shell, etc.), but also to a text area:" |
55 | 63 | ]
|
56 | 64 | },
|
57 | 65 | {
|
|
67 | 75 | "cell_type": "markdown",
|
68 | 76 | "metadata": {},
|
69 | 77 | "source": [
|
70 |
| - "## `on_drop` handler" |
71 |
| - ] |
72 |
| - }, |
73 |
| - { |
74 |
| - "cell_type": "markdown", |
75 |
| - "metadata": {}, |
76 |
| - "source": [ |
77 |
| - "`DraggableLabel` can also become the drop zone (you can drop other stuff on it), if you implement the `on_drop` handler." |
| 78 | + "## Drop Box\n", |
| 79 | + "\n", |
| 80 | + "`DropBox` is a widget that can receive other `DraggableBox` widgets." |
78 | 81 | ]
|
79 | 82 | },
|
80 | 83 | {
|
|
83 | 86 | "metadata": {},
|
84 | 87 | "outputs": [],
|
85 | 88 | "source": [
|
86 |
| - "l1 = DraggableLabel(\"Drag me\")\n", |
87 |
| - "l1" |
| 89 | + "from ipywidgets import DropBox\n", |
| 90 | + "\n", |
| 91 | + "\n", |
| 92 | + "box = DropBox(Label(\"Drop on me\"))\n", |
| 93 | + "box" |
88 | 94 | ]
|
89 | 95 | },
|
90 | 96 | {
|
91 | 97 | "cell_type": "markdown",
|
92 | 98 | "metadata": {},
|
93 | 99 | "source": [
|
94 |
| - "Now, drag this label on the label below." |
| 100 | + "`DropBox` can become the drop zone (you can drop other stuff on it) by implementing the `on_drop` handler:" |
95 | 101 | ]
|
96 | 102 | },
|
97 | 103 | {
|
|
100 | 106 | "metadata": {},
|
101 | 107 | "outputs": [],
|
102 | 108 | "source": [
|
103 |
| - "l2 = DropBox(Label(\"Drop on me\"))\n", |
104 | 109 | "def on_drop_handler(widget, data):\n",
|
105 | 110 | " \"\"\"\"Arguments:\n",
|
106 | 111 | " \n",
|
107 |
| - " widget : widget class\n", |
| 112 | + " widget : Widget class\n", |
108 | 113 | " widget on which something was dropped\n",
|
109 | 114 | " \n",
|
110 | 115 | " data : dict\n",
|
111 | 116 | " extra data sent from the dragged widget\"\"\"\n",
|
112 |
| - " print(data)\n", |
113 | 117 | " text = data['text/plain']\n",
|
114 | 118 | " widget.child.value = \"congrats, you dropped '{}'\".format(text)\n",
|
115 | 119 | "\n",
|
116 |
| - "l2.on_drop(on_drop_handler)\n", |
117 |
| - "l2" |
| 120 | + "box.on_drop(on_drop_handler)\n", |
| 121 | + "box" |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "markdown", |
| 126 | + "metadata": {}, |
| 127 | + "source": [ |
| 128 | + "Now, drag this label on the box above." |
| 129 | + ] |
| 130 | + }, |
| 131 | + { |
| 132 | + "cell_type": "code", |
| 133 | + "execution_count": null, |
| 134 | + "metadata": {}, |
| 135 | + "outputs": [], |
| 136 | + "source": [ |
| 137 | + "label = DraggableLabel(\"Drag me\")\n", |
| 138 | + "label" |
118 | 139 | ]
|
119 | 140 | },
|
120 | 141 | {
|
|
135 | 156 | "cell_type": "markdown",
|
136 | 157 | "metadata": {},
|
137 | 158 | "source": [
|
138 |
| - "If you have more specific needs for the drop behaviour you can also use DropBox widgets, which implements `on_drop` handlers." |
| 159 | + "If you have more specific needs for the drop behavior you can implement them in the DropBox `on_drop` handler." |
139 | 160 | ]
|
140 | 161 | },
|
141 | 162 | {
|
142 | 163 | "cell_type": "markdown",
|
143 | 164 | "metadata": {},
|
144 | 165 | "source": [
|
145 |
| - "This DropBox will replace elements with text of the dropped element (works also for stuff which is not widget):" |
| 166 | + "This DropBox creates new `Button` widgets using the text data of the `DraggableLabel` widget." |
146 | 167 | ]
|
147 | 168 | },
|
148 | 169 | {
|
|
151 | 172 | "metadata": {},
|
152 | 173 | "outputs": [],
|
153 | 174 | "source": [
|
154 |
| - "from ipywidgets import DropBox, Layout, Button\n", |
| 175 | + "from ipywidgets import Button, Layout\n", |
155 | 176 | "\n",
|
156 | 177 | "label = DraggableLabel(\"Drag me\", draggable=True)\n",
|
157 | 178 | "label"
|
|
170 | 191 | "metadata": {},
|
171 | 192 | "outputs": [],
|
172 | 193 | "source": [
|
173 |
| - "box = DropBox(Label(\"Drop here!\"),\n", |
174 |
| - " layout=Layout(width='200px', height='100px'))\n", |
175 | 194 | "def on_drop(widget, data):\n",
|
176 | 195 | " text = data['text/plain']\n",
|
177 | 196 | " widget.child = Button(description=text.upper())\n",
|
178 | 197 | "\n",
|
| 198 | + "box = DropBox(Label(\"Drop here!\"), layout=Layout(width='200px', height='100px'))\n", |
179 | 199 | "box.on_drop(on_drop)\n",
|
180 | 200 | "box"
|
181 | 201 | ]
|
|
184 | 204 | "cell_type": "markdown",
|
185 | 205 | "metadata": {},
|
186 | 206 | "source": [
|
187 |
| - "## Adding widgets to container with a handler" |
| 207 | + "## Adding widgets to a container with a handler" |
188 | 208 | ]
|
189 | 209 | },
|
190 | 210 | {
|
191 | 211 | "cell_type": "markdown",
|
192 | 212 | "metadata": {},
|
193 | 213 | "source": [
|
194 |
| - "You can also reproduce the Box example (adding elements to Box container) using `DropBox` with a custom handler:" |
| 214 | + "You can also reproduce the Box example (adding elements to a `Box` container) using a `DropBox` with a custom handler:" |
195 | 215 | ]
|
196 | 216 | },
|
197 | 217 | {
|
|
200 | 220 | "metadata": {},
|
201 | 221 | "outputs": [],
|
202 | 222 | "source": [
|
203 |
| - "from ipywidgets import DropBox, Layout, Label\n", |
204 |
| - "\n", |
205 | 223 | "label = DraggableLabel(\"Drag me\", draggable=True)\n",
|
206 | 224 | "label"
|
207 | 225 | ]
|
|
212 | 230 | "metadata": {},
|
213 | 231 | "outputs": [],
|
214 | 232 | "source": [
|
215 |
| - "box = DropBox(VBox([Label('Drop here')]), \n", |
216 |
| - " layout=Layout(width='200px', height='100px'))\n", |
| 233 | + "from ipywidgets import VBox\n", |
217 | 234 | "\n",
|
218 | 235 | "def on_drop(widget, data):\n",
|
219 | 236 | " source = data['widget']\n",
|
220 | 237 | " widget.child.children += (source, )\n",
|
221 | 238 | "\n",
|
| 239 | + "box = DropBox(VBox([Label('Drop here')]), layout=Layout(width='200px', height='100px'))\n", |
222 | 240 | "box.on_drop(on_drop)\n",
|
223 | 241 | "box"
|
224 | 242 | ]
|
|
227 | 245 | "cell_type": "markdown",
|
228 | 246 | "metadata": {},
|
229 | 247 | "source": [
|
230 |
| - "**Explanation**: Label widget sets data on the drop event of type `application/x-widget` that contains the widget id of the dragged widget." |
| 248 | + "**Explanation**: The `Label` widget sets data on the drop event of type `application/x-widget` that contains the widget id of the dragged widget." |
231 | 249 | ]
|
232 | 250 | },
|
233 | 251 | {
|
|
241 | 259 | "cell_type": "markdown",
|
242 | 260 | "metadata": {},
|
243 | 261 | "source": [
|
244 |
| - "You can also set custom data on `DraggableLabel` that can be retreived and used in `on_drop` event." |
| 262 | + "You can also set custom data on a `DraggableBox` widget that can be retrieved and used in `on_drop` event." |
245 | 263 | ]
|
246 | 264 | },
|
247 | 265 | {
|
|
252 | 270 | },
|
253 | 271 | "outputs": [],
|
254 | 272 | "source": [
|
255 |
| - "l = DraggableLabel(\"Drag me\", draggable=True)\n", |
256 |
| - "l.drag_data = {'application/custom-data' : 'Custom data'}\n", |
257 |
| - "l" |
| 273 | + "label = DraggableLabel(\"Drag me\", draggable=True)\n", |
| 274 | + "label.drag_data = {'application/custom-data' : 'Custom data'}\n", |
| 275 | + "label" |
258 | 276 | ]
|
259 | 277 | },
|
260 | 278 | {
|
|
263 | 281 | "metadata": {},
|
264 | 282 | "outputs": [],
|
265 | 283 | "source": [
|
266 |
| - "l2 = DropBox(Label(\"Drop here\"))\n", |
267 |
| - "\n", |
268 | 284 | "def on_drop_handler(widget, data):\n",
|
269 | 285 | " \"\"\"\"Arguments:\n",
|
270 | 286 | " \n",
|
|
277 | 293 | " text = data['text/plain']\n",
|
278 | 294 | " widget_id = data['widget'].model_id\n",
|
279 | 295 | " custom_data = data['application/custom-data']\n",
|
280 |
| - " widget.child.value = (\"you dropped widget ID '{}...' \"\n", |
281 |
| - " \"with text '{}' and custom data '{}'\"\n", |
282 |
| - " ).format(widget_id[:5], text, custom_data)\n", |
| 296 | + " value = \"you dropped widget ID '{}...' with text '{}' and custom data '{}'\".format(widget_id[:5], text, custom_data)\n", |
| 297 | + " widget.child.value = value\n", |
283 | 298 | "\n",
|
284 |
| - "l2.on_drop(on_drop_handler)\n", |
285 |
| - "l2" |
| 299 | + "box = DropBox(Label(\"Drop here\"))\n", |
| 300 | + "box.on_drop(on_drop_handler)\n", |
| 301 | + "box" |
286 | 302 | ]
|
287 | 303 | },
|
288 | 304 | {
|
|
296 | 312 | "cell_type": "markdown",
|
297 | 313 | "metadata": {},
|
298 | 314 | "source": [
|
299 |
| - "`DraggableBox` can be used to wrap any widget so that it can be dragged and dropped." |
| 315 | + "`DraggableBox` can be used to wrap any widget so that it can be dragged and dropped. For example sliders can also be dragged:" |
300 | 316 | ]
|
301 | 317 | },
|
302 | 318 | {
|
|
361 | 377 | "metadata": {},
|
362 | 378 | "outputs": [],
|
363 | 379 | "source": [
|
| 380 | + "import json\n", |
364 | 381 | "import bqplot.pyplot as plt\n",
|
365 |
| - "from ipywidgets import Label, GridspecLayout, DropBox, Layout\n", |
366 |
| - "import json" |
| 382 | + "from ipywidgets import Label, GridspecLayout, DropBox, Layout" |
367 | 383 | ]
|
368 | 384 | },
|
369 | 385 | {
|
|
395 | 411 | "metadata": {},
|
396 | 412 | "outputs": [],
|
397 | 413 | "source": [
|
398 |
| - "box = DropBox(Label(\"Drag data from the table and drop it here.\"), layout=Layout(height='500px', width='800px'))\n", |
399 | 414 | "def box_ondrop(widget, data):\n",
|
400 | 415 | " fig = plt.figure()\n",
|
401 | 416 | " y = json.loads(data['data/app'])\n",
|
402 | 417 | " plt.plot(y)\n",
|
403 | 418 | " widget.child = fig\n",
|
| 419 | + " \n", |
| 420 | + "box = DropBox(Label(\"Drag data from the table and drop it here.\"), layout=Layout(height='500px', width='800px'))\n", |
404 | 421 | "box.on_drop(box_ondrop)"
|
405 | 422 | ]
|
406 | 423 | },
|
|
454 | 471 | "metadata": {},
|
455 | 472 | "outputs": [],
|
456 | 473 | "source": [
|
457 |
| - "from ipywidgets import SelectMultiple, Layout, DraggableBox, DropBox, HBox\n", |
458 | 474 | "import bqplot as bq\n",
|
| 475 | + "from ipywidgets import SelectMultiple, Layout, DraggableBox, DropBox, HBox\n", |
459 | 476 | "\n",
|
460 | 477 | "select_list = SelectMultiple(\n",
|
461 | 478 | " options=['Apples', 'Oranges', 'Pears'],\n",
|
|
465 | 482 | ")\n",
|
466 | 483 | "select_box = DraggableBox(select_list, draggable=True)\n",
|
467 | 484 | "\n",
|
468 |
| - "fruits = {'Apples' : 5,\n", |
469 |
| - " 'Oranges' : 1,\n", |
470 |
| - " 'Pears': 3}\n", |
471 |
| - "\n", |
| 485 | + "fruits = {\n", |
| 486 | + " 'Apples' : 5,\n", |
| 487 | + " 'Oranges' : 1,\n", |
| 488 | + " 'Pears': 3\n", |
| 489 | + "}\n", |
472 | 490 | "\n",
|
473 | 491 | "fig = bq.Figure(marks=[], fig_margin = dict(left=50, right=0, top=0, bottom=70))\n",
|
474 | 492 | "fig.layout.height='300px'\n",
|
|
0 commit comments