##// END OF EJS Templates
Added flex properties example to alignment tutorial.
Jonathan Frederic -
Show More
@@ -1,322 +1,378 b''
1 {
1 {
2 "metadata": {
2 "metadata": {
3 "cell_tags": [
3 "cell_tags": [
4 [
4 [
5 "<None>",
5 "<None>",
6 null
6 null
7 ]
7 ]
8 ],
8 ],
9 "name": ""
9 "name": ""
10 },
10 },
11 "nbformat": 3,
11 "nbformat": 3,
12 "nbformat_minor": 0,
12 "nbformat_minor": 0,
13 "worksheets": [
13 "worksheets": [
14 {
14 {
15 "cells": [
15 "cells": [
16 {
16 {
17 "cell_type": "code",
17 "cell_type": "code",
18 "collapsed": false,
18 "collapsed": false,
19 "input": [
19 "input": [
20 "from IPython.html import widgets # Widget definitions\n",
20 "from IPython.html import widgets # Widget definitions\n",
21 "from IPython.display import display # Used to display widgets in the notebook\n",
21 "from IPython.display import display # Used to display widgets in the notebook\n",
22 "\n",
22 "\n",
23 "# Enable widgets in this notebook\n",
23 "# Enable widgets in this notebook\n",
24 "widgets.init_widget_js()"
24 "widgets.init_widget_js()"
25 ],
25 ],
26 "language": "python",
26 "language": "python",
27 "metadata": {},
27 "metadata": {},
28 "outputs": [
28 "outputs": [
29 {
29 {
30 "javascript": [
30 "javascript": [
31 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/button.js\");"
31 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/button.js\");"
32 ],
32 ],
33 "metadata": {},
33 "metadata": {},
34 "output_type": "display_data"
34 "output_type": "display_data"
35 },
35 },
36 {
36 {
37 "javascript": [
37 "javascript": [
38 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/int_range.js\");"
38 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/int_range.js\");"
39 ],
39 ],
40 "metadata": {},
40 "metadata": {},
41 "output_type": "display_data"
41 "output_type": "display_data"
42 },
42 },
43 {
43 {
44 "javascript": [
44 "javascript": [
45 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/string.js\");"
45 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/string.js\");"
46 ],
46 ],
47 "metadata": {},
47 "metadata": {},
48 "output_type": "display_data"
48 "output_type": "display_data"
49 },
49 },
50 {
50 {
51 "javascript": [
51 "javascript": [
52 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/multicontainer.js\");"
52 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/multicontainer.js\");"
53 ],
53 ],
54 "metadata": {},
54 "metadata": {},
55 "output_type": "display_data"
55 "output_type": "display_data"
56 },
56 },
57 {
57 {
58 "javascript": [
58 "javascript": [
59 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/bool.js\");"
59 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/bool.js\");"
60 ],
60 ],
61 "metadata": {},
61 "metadata": {},
62 "output_type": "display_data"
62 "output_type": "display_data"
63 },
63 },
64 {
64 {
65 "javascript": [
65 "javascript": [
66 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/int.js\");"
66 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/int.js\");"
67 ],
67 ],
68 "metadata": {},
68 "metadata": {},
69 "output_type": "display_data"
69 "output_type": "display_data"
70 },
70 },
71 {
71 {
72 "javascript": [
72 "javascript": [
73 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/selection.js\");"
73 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/selection.js\");"
74 ],
74 ],
75 "metadata": {},
75 "metadata": {},
76 "output_type": "display_data"
76 "output_type": "display_data"
77 },
77 },
78 {
78 {
79 "javascript": [
79 "javascript": [
80 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/float.js\");"
80 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/float.js\");"
81 ],
81 ],
82 "metadata": {},
82 "metadata": {},
83 "output_type": "display_data"
83 "output_type": "display_data"
84 },
84 },
85 {
85 {
86 "javascript": [
86 "javascript": [
87 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/float_range.js\");"
87 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/float_range.js\");"
88 ],
88 ],
89 "metadata": {},
89 "metadata": {},
90 "output_type": "display_data"
90 "output_type": "display_data"
91 },
91 },
92 {
92 {
93 "javascript": [
93 "javascript": [
94 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/container.js\");"
94 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/container.js\");"
95 ],
95 ],
96 "metadata": {},
96 "metadata": {},
97 "output_type": "display_data"
97 "output_type": "display_data"
98 }
98 }
99 ],
99 ],
100 "prompt_number": 1
100 "prompt_number": 1
101 },
101 },
102 {
102 {
103 "cell_type": "heading",
103 "cell_type": "heading",
104 "level": 1,
104 "level": 1,
105 "metadata": {},
105 "metadata": {},
106 "source": [
106 "source": [
107 "Alignment"
107 "Alignment"
108 ]
108 ]
109 },
109 },
110 {
110 {
111 "cell_type": "markdown",
111 "cell_type": "markdown",
112 "metadata": {},
112 "metadata": {},
113 "source": [
113 "source": [
114 "Most widgets have a `description` property which allows a label for the widget to be defined. The label of the widget has a fixed minimum width. The text of the label is always right aligned and the widget is left aligned (as seen below) "
114 "Most widgets have a `description` property which allows a label for the widget to be defined. The label of the widget has a fixed minimum width. The text of the label is always right aligned and the widget is left aligned (as seen below) "
115 ]
115 ]
116 },
116 },
117 {
117 {
118 "cell_type": "code",
118 "cell_type": "code",
119 "collapsed": false,
119 "collapsed": false,
120 "input": [
120 "input": [
121 "display(widgets.StringWidget(description=\"a:\"))\n",
121 "display(widgets.StringWidget(description=\"a:\"))\n",
122 "display(widgets.StringWidget(description=\"aa:\"))\n",
122 "display(widgets.StringWidget(description=\"aa:\"))\n",
123 "display(widgets.StringWidget(description=\"aaa:\"))"
123 "display(widgets.StringWidget(description=\"aaa:\"))"
124 ],
124 ],
125 "language": "python",
125 "language": "python",
126 "metadata": {},
126 "metadata": {},
127 "outputs": [],
127 "outputs": [],
128 "prompt_number": 2
128 "prompt_number": 2
129 },
129 },
130 {
130 {
131 "cell_type": "markdown",
131 "cell_type": "markdown",
132 "metadata": {},
132 "metadata": {},
133 "source": [
133 "source": [
134 "If a label is longer than the minimum width, the widget is shifted to the right (as seen below)."
134 "If a label is longer than the minimum width, the widget is shifted to the right (as seen below)."
135 ]
135 ]
136 },
136 },
137 {
137 {
138 "cell_type": "code",
138 "cell_type": "code",
139 "collapsed": false,
139 "collapsed": false,
140 "input": [
140 "input": [
141 "display(widgets.StringWidget(description=\"a:\"))\n",
141 "display(widgets.StringWidget(description=\"a:\"))\n",
142 "display(widgets.StringWidget(description=\"aa:\"))\n",
142 "display(widgets.StringWidget(description=\"aa:\"))\n",
143 "display(widgets.StringWidget(description=\"aaa:\"))\n",
143 "display(widgets.StringWidget(description=\"aaa:\"))\n",
144 "display(widgets.StringWidget(description=\"aaaaaaaaaaaaaaaaaa:\"))"
144 "display(widgets.StringWidget(description=\"aaaaaaaaaaaaaaaaaa:\"))"
145 ],
145 ],
146 "language": "python",
146 "language": "python",
147 "metadata": {},
147 "metadata": {},
148 "outputs": [],
148 "outputs": [],
149 "prompt_number": 3
149 "prompt_number": 3
150 },
150 },
151 {
151 {
152 "cell_type": "markdown",
152 "cell_type": "markdown",
153 "metadata": {},
153 "metadata": {},
154 "source": [
154 "source": [
155 "If a `description` is not set for the widget, the label is not displayed (as seen below)."
155 "If a `description` is not set for the widget, the label is not displayed (as seen below)."
156 ]
156 ]
157 },
157 },
158 {
158 {
159 "cell_type": "code",
159 "cell_type": "code",
160 "collapsed": false,
160 "collapsed": false,
161 "input": [
161 "input": [
162 "display(widgets.StringWidget(description=\"a:\"))\n",
162 "display(widgets.StringWidget(description=\"a:\"))\n",
163 "display(widgets.StringWidget(description=\"aa:\"))\n",
163 "display(widgets.StringWidget(description=\"aa:\"))\n",
164 "display(widgets.StringWidget(description=\"aaa:\"))\n",
164 "display(widgets.StringWidget(description=\"aaa:\"))\n",
165 "display(widgets.StringWidget())"
165 "display(widgets.StringWidget())"
166 ],
166 ],
167 "language": "python",
167 "language": "python",
168 "metadata": {},
168 "metadata": {},
169 "outputs": [],
169 "outputs": [],
170 "prompt_number": 4
170 "prompt_number": 4
171 },
171 },
172 {
172 {
173 "cell_type": "heading",
173 "cell_type": "heading",
174 "level": 1,
174 "level": 1,
175 "metadata": {},
175 "metadata": {},
176 "source": [
176 "source": [
177 "Custom Alignment"
177 "Custom Alignment"
178 ]
178 ]
179 },
179 },
180 {
180 {
181 "cell_type": "markdown",
181 "cell_type": "markdown",
182 "metadata": {},
182 "metadata": {},
183 "source": [
183 "source": [
184 "`ContainerWidget`s allow for custom alignment of widgets. The `hbox` and `vbox` methods (parameterless) cause the `ContainerWidget` to both horizontally and vertically align its children. The following example compares `vbox` to `hbox`."
184 "`ContainerWidget`s allow for custom alignment of widgets. The `hbox` and `vbox` methods (parameterless) cause the `ContainerWidget` to both horizontally and vertically align its children. The following example compares `vbox` to `hbox`."
185 ]
185 ]
186 },
186 },
187 {
187 {
188 "cell_type": "code",
188 "cell_type": "code",
189 "collapsed": false,
189 "collapsed": false,
190 "input": [
190 "input": [
191 "child_style = {\n",
191 "child_style = {\n",
192 " 'background': '#77CC77',\n",
192 " 'background': '#77CC77',\n",
193 " 'padding': '25px',\n",
193 " 'padding': '25px',\n",
194 " 'margin': '5px',\n",
194 " 'margin': '5px',\n",
195 " 'font-size': 'xx-large',\n",
195 " 'font-size': 'xx-large',\n",
196 " 'color': 'white',\n",
196 " 'color': 'white',\n",
197 "}\n",
197 "}\n",
198 "\n",
198 "\n",
199 "def make_container(title):\n",
199 "def make_container(title):\n",
200 " display(widgets.StringWidget(default_view_name='LabelView', value='<h2><br>' + title + '</h2>'))\n",
200 " display(widgets.StringWidget(default_view_name='LabelView', value='<h2><br>' + title + '</h2>'))\n",
201 " container = widgets.ContainerWidget()\n",
201 " container = widgets.ContainerWidget()\n",
202 " container.set_css('background', '#999999')\n",
202 " container.set_css('background', '#999999')\n",
203 " display(container)\n",
203 " display(container)\n",
204 " return container\n",
204 " return container\n",
205 "\n",
205 "\n",
206 "def fill_container(container):\n",
206 "def fill_container(container):\n",
207 " components = []\n",
207 " components = []\n",
208 " for i in range(3):\n",
208 " for i in range(3):\n",
209 " components.append(widgets.StringWidget(parent=container, default_view_name='LabelView', value=\"ABC\"[i]))\n",
209 " components.append(widgets.StringWidget(parent=container, default_view_name='LabelView', value=\"ABC\"[i]))\n",
210 " components[i].set_css(child_style)\n",
210 " components[i].set_css(child_style)\n",
211 " display(components[i])\n",
211 " display(components[i])\n",
212 " \n",
212 " \n",
213 "container = make_container('VBox')\n",
213 "container = make_container('VBox')\n",
214 "container.vbox()\n",
214 "container.vbox()\n",
215 "fill_container(container)\n",
215 "fill_container(container)\n",
216 "\n",
216 "\n",
217 "container = make_container('HBox')\n",
217 "container = make_container('HBox')\n",
218 "container.hbox()\n",
218 "container.hbox()\n",
219 "fill_container(container)\n"
219 "fill_container(container)\n"
220 ],
220 ],
221 "language": "python",
221 "language": "python",
222 "metadata": {},
222 "metadata": {},
223 "outputs": [],
223 "outputs": [],
224 "prompt_number": 5
224 "prompt_number": 5
225 },
225 },
226 {
226 {
227 "cell_type": "markdown",
227 "cell_type": "markdown",
228 "metadata": {},
228 "metadata": {},
229 "source": [
229 "source": [
230 "The `ContainerWidget` `pack_start`, `pack_center`, and `pack_end` methods (parameterless) adjust the alignment of the widgets on the axis that they are being rendered on. Below is an example of the different alignments."
230 "The `ContainerWidget` `pack_start`, `pack_center`, and `pack_end` methods (parameterless) adjust the alignment of the widgets on the axis that they are being rendered on. Below is an example of the different alignments."
231 ]
231 ]
232 },
232 },
233 {
233 {
234 "cell_type": "code",
234 "cell_type": "code",
235 "collapsed": false,
235 "collapsed": false,
236 "input": [
236 "input": [
237 "container = make_container('HBox Pack Start')\n",
237 "container = make_container('HBox Pack Start')\n",
238 "container.hbox()\n",
238 "container.hbox()\n",
239 "container.pack_start()\n",
239 "container.pack_start()\n",
240 "fill_container(container)\n",
240 "fill_container(container)\n",
241 " \n",
241 " \n",
242 "container = make_container('HBox Pack Center')\n",
242 "container = make_container('HBox Pack Center')\n",
243 "container.hbox()\n",
243 "container.hbox()\n",
244 "container.pack_center()\n",
244 "container.pack_center()\n",
245 "fill_container(container)\n",
245 "fill_container(container)\n",
246 " \n",
246 " \n",
247 "container = make_container('HBox Pack End')\n",
247 "container = make_container('HBox Pack End')\n",
248 "container.hbox()\n",
248 "container.hbox()\n",
249 "container.pack_end()\n",
249 "container.pack_end()\n",
250 "fill_container(container)"
250 "fill_container(container)"
251 ],
251 ],
252 "language": "python",
252 "language": "python",
253 "metadata": {},
253 "metadata": {},
254 "outputs": [],
254 "outputs": [],
255 "prompt_number": 6
255 "prompt_number": 6
256 },
256 },
257 {
257 {
258 "cell_type": "markdown",
258 "cell_type": "markdown",
259 "metadata": {},
259 "metadata": {},
260 "source": [
260 "source": [
261 "The `ContainerWidget` `flex0`, `flex1`, and `flex2` methods (parameterless) modify the containers flexibility. Changing a container flexibility affects how and if the container will occupy the remaining space. Setting `flex0` has the same result as setting no flex. Below is an example of different flex configurations. The number on the boxes correspond to the applied flex."
262 ]
263 },
264 {
265 "cell_type": "code",
266 "collapsed": false,
267 "input": [
268 "def fill_container(container, flexes):\n",
269 " components = []\n",
270 " for i in range(len(flexes)):\n",
271 " components.append(widgets.ContainerWidget(parent=container))\n",
272 " components[i].set_css(child_style)\n",
273 " \n",
274 " label = widgets.StringWidget(parent=components[i], default_view_name='LabelView', value=str(flexes[i]))\n",
275 " \n",
276 " if flexes[i] == 0:\n",
277 " components[i].flex0()\n",
278 " elif flexes[i] == 1:\n",
279 " components[i].flex1()\n",
280 " elif flexes[i] == 2:\n",
281 " components[i].flex2()\n",
282 " display(components[i])\n",
283 " \n",
284 "container = make_container('Different Flex Configurations')\n",
285 "container.hbox()\n",
286 "fill_container(container, [0, 0, 0])\n",
287 " \n",
288 "container = make_container('')\n",
289 "container.hbox()\n",
290 "fill_container(container, [0, 0, 1])\n",
291 " \n",
292 "container = make_container('')\n",
293 "container.hbox()\n",
294 "fill_container(container, [0, 1, 1])\n",
295 " \n",
296 "container = make_container('')\n",
297 "container.hbox()\n",
298 "fill_container(container, [0, 2, 2])\n",
299 " \n",
300 "container = make_container('')\n",
301 "container.hbox()\n",
302 "fill_container(container, [0, 1, 2])\n",
303 " \n",
304 "container = make_container('')\n",
305 "container.hbox()\n",
306 "fill_container(container, [1, 1, 2])"
307 ],
308 "language": "python",
309 "metadata": {},
310 "outputs": [],
311 "prompt_number": 7
312 },
313 {
314 "cell_type": "markdown",
315 "metadata": {},
316 "source": [
261 "The `ContainerWidget` `align_start`, `align_center`, and `align_end` methods (parameterless) adjust the alignment of the widgets on the axis perpindicular to the one that they are being rendered on. Below is an example of the different alignments."
317 "The `ContainerWidget` `align_start`, `align_center`, and `align_end` methods (parameterless) adjust the alignment of the widgets on the axis perpindicular to the one that they are being rendered on. Below is an example of the different alignments."
262 ]
318 ]
263 },
319 },
264 {
320 {
265 "cell_type": "code",
321 "cell_type": "code",
266 "collapsed": false,
322 "collapsed": false,
267 "input": [
323 "input": [
268 "def fill_container(container):\n",
324 "def fill_container(container):\n",
269 " components = []\n",
325 " components = []\n",
270 " for i in range(3):\n",
326 " for i in range(3):\n",
271 " components.append(widgets.StringWidget(parent=container, default_view_name='LabelView', value=\"ABC\"[i]))\n",
327 " components.append(widgets.StringWidget(parent=container, default_view_name='LabelView', value=\"ABC\"[i]))\n",
272 " components[i].set_css(child_style)\n",
328 " components[i].set_css(child_style)\n",
273 " components[i].set_css('height', str((i+1) * 50) + 'px')\n",
329 " components[i].set_css('height', str((i+1) * 50) + 'px')\n",
274 " display(components[i])\n",
330 " display(components[i])\n",
275 "\n",
331 "\n",
276 "container = make_container('HBox Align Start')\n",
332 "container = make_container('HBox Align Start')\n",
277 "container.hbox()\n",
333 "container.hbox()\n",
278 "container.align_start()\n",
334 "container.align_start()\n",
279 "fill_container(container)\n",
335 "fill_container(container)\n",
280 " \n",
336 " \n",
281 "container = make_container('HBox Align Center')\n",
337 "container = make_container('HBox Align Center')\n",
282 "container.hbox()\n",
338 "container.hbox()\n",
283 "container.align_center()\n",
339 "container.align_center()\n",
284 "fill_container(container)\n",
340 "fill_container(container)\n",
285 " \n",
341 " \n",
286 "container = make_container('HBox Align End')\n",
342 "container = make_container('HBox Align End')\n",
287 "container.hbox()\n",
343 "container.hbox()\n",
288 "container.align_end()\n",
344 "container.align_end()\n",
289 "fill_container(container)"
345 "fill_container(container)"
290 ],
346 ],
291 "language": "python",
347 "language": "python",
292 "metadata": {},
348 "metadata": {},
293 "outputs": [],
349 "outputs": [],
294 "prompt_number": 7
350 "prompt_number": 8
295 },
351 },
296 {
352 {
297 "cell_type": "markdown",
353 "cell_type": "markdown",
298 "metadata": {},
354 "metadata": {},
299 "source": [
355 "source": [
300 "By default the widget area is a `vbox`; however, there are many uses for a `hbox`. The example below uses a `hbox` to display a set of vertical sliders, like an equalizer."
356 "By default the widget area is a `vbox`; however, there are many uses for a `hbox`. The example below uses a `hbox` to display a set of vertical sliders, like an equalizer."
301 ]
357 ]
302 },
358 },
303 {
359 {
304 "cell_type": "code",
360 "cell_type": "code",
305 "collapsed": false,
361 "collapsed": false,
306 "input": [
362 "input": [
307 "container = widgets.ContainerWidget()\n",
363 "container = widgets.ContainerWidget()\n",
308 "container.hbox()\n",
364 "container.hbox()\n",
309 "for i in range(15):\n",
365 "for i in range(15):\n",
310 " widgets.FloatRangeWidget(orientation='vertical', parent=container, description=str(i+1), value=50.0)\n",
366 " widgets.FloatRangeWidget(orientation='vertical', parent=container, description=str(i+1), value=50.0)\n",
311 "display(container)"
367 "display(container)"
312 ],
368 ],
313 "language": "python",
369 "language": "python",
314 "metadata": {},
370 "metadata": {},
315 "outputs": [],
371 "outputs": [],
316 "prompt_number": 8
372 "prompt_number": 9
317 }
373 }
318 ],
374 ],
319 "metadata": {}
375 "metadata": {}
320 }
376 }
321 ]
377 ]
322 } No newline at end of file
378 }
General Comments 0
You need to be logged in to leave comments. Login now