Show More
@@ -1,202 +1,239 b'' | |||
|
1 | 1 | { |
|
2 | 2 | "cells": [ |
|
3 | 3 | { |
|
4 | 4 | "cell_type": "markdown", |
|
5 | 5 | "metadata": {}, |
|
6 | 6 | "source": [ |
|
7 | 7 | "# Variable Inspector Widget" |
|
8 | 8 | ] |
|
9 | 9 | }, |
|
10 | 10 | { |
|
11 | 11 | "cell_type": "markdown", |
|
12 | 12 | "metadata": {}, |
|
13 | 13 | "source": [ |
|
14 | 14 | "## A short example implementation" |
|
15 | 15 | ] |
|
16 | 16 | }, |
|
17 | 17 | { |
|
18 | 18 | "cell_type": "markdown", |
|
19 | 19 | "metadata": {}, |
|
20 | 20 | "source": [ |
|
21 | 21 | "This notebook demonstrates how one can use the widgets already built-in to IPython to create a working variable inspector much like the ones seen in popular commercial scientific computing environments." |
|
22 | 22 | ] |
|
23 | 23 | }, |
|
24 | 24 | { |
|
25 | 25 | "cell_type": "code", |
|
26 | 26 | "execution_count": null, |
|
27 | 27 | "metadata": { |
|
28 | 28 | "collapsed": false |
|
29 | 29 | }, |
|
30 | 30 | "outputs": [], |
|
31 | 31 | "source": [ |
|
32 | 32 | "from IPython.html import widgets # Loads the Widget framework.\n", |
|
33 | 33 | "from IPython.core.magics.namespace import NamespaceMagics # Used to query namespace.\n", |
|
34 | 34 | "\n", |
|
35 | 35 | "# For this example, hide these names, just to avoid polluting the namespace further\n", |
|
36 | 36 | "get_ipython().user_ns_hidden['widgets'] = widgets\n", |
|
37 | 37 | "get_ipython().user_ns_hidden['NamespaceMagics'] = NamespaceMagics" |
|
38 | 38 | ] |
|
39 | 39 | }, |
|
40 | 40 | { |
|
41 | 41 | "cell_type": "code", |
|
42 | 42 | "execution_count": null, |
|
43 | 43 | "metadata": { |
|
44 | 44 | "collapsed": false |
|
45 | 45 | }, |
|
46 | 46 | "outputs": [], |
|
47 | 47 | "source": [ |
|
48 | 48 | "class VariableInspectorWindow(object):\n", |
|
49 | 49 | " instance = None\n", |
|
50 | 50 | " \n", |
|
51 | 51 | " def __init__(self, ipython):\n", |
|
52 | 52 | " \"\"\"Public constructor.\"\"\"\n", |
|
53 | 53 | " if VariableInspectorWindow.instance is not None:\n", |
|
54 | 54 | " raise Exception(\"\"\"Only one instance of the Variable Inspector can exist at a \n", |
|
55 | 55 | " time. Call close() on the active instance before creating a new instance.\n", |
|
56 | 56 | " If you have lost the handle to the active instance, you can re-obtain it\n", |
|
57 | 57 | " via `VariableInspectorWindow.instance`.\"\"\")\n", |
|
58 | 58 | " \n", |
|
59 | 59 | " VariableInspectorWindow.instance = self\n", |
|
60 | 60 | " self.closed = False\n", |
|
61 | 61 | " self.namespace = NamespaceMagics()\n", |
|
62 | 62 | " self.namespace.shell = ipython.kernel.shell\n", |
|
63 | 63 | " \n", |
|
64 |
" self._ |
|
|
65 |
" self._ |
|
|
66 | " self._popout.button_text = self._popout.description\n", | |
|
64 | " self._box = widgets.Box()\n", | |
|
65 | " self._box._dom_classes = ['inspector']\n", | |
|
66 | " self._box.background_color = '#fff'\n", | |
|
67 | " self._box.border_color = '#ccc'\n", | |
|
68 | " self._box.border_width = 1\n", | |
|
69 | " self._box.border_radius = 5\n", | |
|
67 | 70 | "\n", |
|
68 | 71 | " self._modal_body = widgets.VBox()\n", |
|
69 | 72 | " self._modal_body.overflow_y = 'scroll'\n", |
|
70 | 73 | "\n", |
|
71 | 74 | " self._modal_body_label = widgets.HTML(value = 'Not hooked')\n", |
|
72 | 75 | " self._modal_body.children = [self._modal_body_label]\n", |
|
73 | 76 | "\n", |
|
74 |
" self._ |
|
|
77 | " self._box.children = [\n", | |
|
75 | 78 | " self._modal_body, \n", |
|
76 | 79 | " ]\n", |
|
77 | 80 | " \n", |
|
78 | 81 | " self._ipython = ipython\n", |
|
79 | 82 | " self._ipython.events.register('post_run_cell', self._fill)\n", |
|
80 | 83 | " \n", |
|
81 | 84 | " def close(self):\n", |
|
82 | 85 | " \"\"\"Close and remove hooks.\"\"\"\n", |
|
83 | 86 | " if not self.closed:\n", |
|
84 | 87 | " self._ipython.events.unregister('post_run_cell', self._fill)\n", |
|
85 |
" self._ |
|
|
88 | " self._box.close()\n", | |
|
86 | 89 | " self.closed = True\n", |
|
87 | 90 | " VariableInspectorWindow.instance = None\n", |
|
88 | 91 | "\n", |
|
89 | 92 | " def _fill(self):\n", |
|
90 | 93 | " \"\"\"Fill self with variable information.\"\"\"\n", |
|
91 | 94 | " values = self.namespace.who_ls()\n", |
|
92 | 95 | " self._modal_body_label.value = '<table class=\"table table-bordered table-striped\"><tr><th>Name</th><th>Type</th><th>Value</th></tr><tr><td>' + \\\n", |
|
93 | 96 | " '</td></tr><tr><td>'.join(['{0}</td><td>{1}</td><td>{2}'.format(v, type(eval(v)).__name__, str(eval(v))) for v in values]) + \\\n", |
|
94 | 97 | " '</td></tr></table>'\n", |
|
95 | 98 | "\n", |
|
96 | 99 | " def _ipython_display_(self):\n", |
|
97 | 100 | " \"\"\"Called when display() or pyout is used to display the Variable \n", |
|
98 | 101 | " Inspector.\"\"\"\n", |
|
99 |
" self._ |
|
|
102 | " self._box._ipython_display_()\n" | |
|
100 | 103 | ] |
|
101 | 104 | }, |
|
102 | 105 | { |
|
103 | 106 | "cell_type": "code", |
|
104 | 107 | "execution_count": null, |
|
105 | 108 | "metadata": { |
|
106 | 109 | "collapsed": false |
|
107 | 110 | }, |
|
108 | 111 | "outputs": [], |
|
109 | 112 | "source": [ |
|
110 | 113 | "inspector = VariableInspectorWindow(get_ipython())\n", |
|
111 | 114 | "inspector" |
|
112 | 115 | ] |
|
113 | 116 | }, |
|
114 | 117 | { |
|
115 | 118 | "cell_type": "markdown", |
|
116 | 119 | "metadata": {}, |
|
117 | 120 | "source": [ |
|
121 | "Pop the inspector out of the widget area using Javascript. To close the inspector, click the close button on the widget area that it was spawned from." | |
|
122 | ] | |
|
123 | }, | |
|
124 | { | |
|
125 | "cell_type": "code", | |
|
126 | "execution_count": null, | |
|
127 | "metadata": { | |
|
128 | "collapsed": false | |
|
129 | }, | |
|
130 | "outputs": [], | |
|
131 | "source": [ | |
|
132 | "%%javascript\n", | |
|
133 | "$('div.inspector')\n", | |
|
134 | " .detach()\n", | |
|
135 | " .prependTo($('body'))\n", | |
|
136 | " .css({\n", | |
|
137 | " 'z-index': 999, \n", | |
|
138 | " position: 'fixed',\n", | |
|
139 | " 'box-shadow': '5px 5px 12px -3px black',\n", | |
|
140 | " opacity: 0.9\n", | |
|
141 | " })\n", | |
|
142 | " .draggable();" | |
|
143 | ] | |
|
144 | }, | |
|
145 | { | |
|
146 | "cell_type": "markdown", | |
|
147 | "metadata": {}, | |
|
148 | "source": [ | |
|
118 | 149 | "# Test" |
|
119 | 150 | ] |
|
120 | 151 | }, |
|
121 | 152 | { |
|
122 | 153 | "cell_type": "code", |
|
123 | 154 | "execution_count": null, |
|
124 | 155 | "metadata": { |
|
125 | 156 | "collapsed": false |
|
126 | 157 | }, |
|
127 | 158 | "outputs": [], |
|
128 | 159 | "source": [ |
|
129 | 160 | "a = 5" |
|
130 | 161 | ] |
|
131 | 162 | }, |
|
132 | 163 | { |
|
133 | 164 | "cell_type": "code", |
|
134 | 165 | "execution_count": null, |
|
135 | 166 | "metadata": { |
|
136 | 167 | "collapsed": false |
|
137 | 168 | }, |
|
138 | 169 | "outputs": [], |
|
139 | 170 | "source": [ |
|
140 | 171 | "b = 3.0" |
|
141 | 172 | ] |
|
142 | 173 | }, |
|
143 | 174 | { |
|
144 | 175 | "cell_type": "code", |
|
145 | 176 | "execution_count": null, |
|
146 | 177 | "metadata": { |
|
147 | 178 | "collapsed": false |
|
148 | 179 | }, |
|
149 | 180 | "outputs": [], |
|
150 | 181 | "source": [ |
|
151 | 182 | "c = a * b" |
|
152 | 183 | ] |
|
153 | 184 | }, |
|
154 | 185 | { |
|
155 | 186 | "cell_type": "code", |
|
156 | 187 | "execution_count": null, |
|
157 | 188 | "metadata": { |
|
158 | 189 | "collapsed": false |
|
159 | 190 | }, |
|
160 | 191 | "outputs": [], |
|
161 | 192 | "source": [ |
|
162 | 193 | "d = \"String\"" |
|
163 | 194 | ] |
|
164 | 195 | }, |
|
165 | 196 | { |
|
166 | 197 | "cell_type": "code", |
|
167 | 198 | "execution_count": null, |
|
168 | 199 | "metadata": { |
|
169 | 200 | "collapsed": false |
|
170 | 201 | }, |
|
171 | 202 | "outputs": [], |
|
172 | 203 | "source": [ |
|
173 | 204 | "del b" |
|
174 | 205 | ] |
|
175 | 206 | }, |
|
176 | 207 | { |
|
177 | 208 | "cell_type": "code", |
|
178 | 209 | "execution_count": null, |
|
179 | 210 | "metadata": { |
|
180 | 211 | "collapsed": false |
|
181 | 212 | }, |
|
182 | 213 | "outputs": [], |
|
183 | 214 | "source": [ |
|
184 | 215 | "inspector.close()" |
|
185 | 216 | ] |
|
186 | 217 | } |
|
187 | 218 | ], |
|
188 | 219 | "metadata": { |
|
189 | 220 | "kernelspec": { |
|
221 | "display_name": "IPython (Python 2)", | |
|
222 | "name": "python2" | |
|
223 | }, | |
|
224 | "language_info": { | |
|
190 | 225 | "codemirror_mode": { |
|
191 | "name": "python", | |
|
226 | "name": "ipython", | |
|
192 | 227 | "version": 2 |
|
193 | 228 | }, |
|
194 | "display_name": "Python 2", | |
|
195 |
" |
|
|
196 |
"name": "python |
|
|
197 | }, | |
|
198 | "signature": "sha256:474731659fb14b86672d1dafb2b497fa280082ab40a8a82fe2cde1b6d9b88a6e" | |
|
229 | "file_extension": ".py", | |
|
230 | "mimetype": "text/x-python", | |
|
231 | "name": "python", | |
|
232 | "nbconvert_exporter": "python", | |
|
233 | "pygments_lexer": "ipython2", | |
|
234 | "version": "2.7.6" | |
|
235 | } | |
|
199 | 236 | }, |
|
200 | 237 | "nbformat": 4, |
|
201 | 238 | "nbformat_minor": 0 |
|
202 | 239 | } No newline at end of file |
@@ -1,775 +1,584 b'' | |||
|
1 | 1 | { |
|
2 | 2 | "cells": [ |
|
3 | 3 | { |
|
4 | 4 | "cell_type": "markdown", |
|
5 | 5 | "metadata": {}, |
|
6 | 6 | "source": [ |
|
7 | 7 | "[Index](Index.ipynb) - [Back](Widget Events.ipynb) - [Next](Custom Widget - Hello World.ipynb)" |
|
8 | 8 | ] |
|
9 | 9 | }, |
|
10 | 10 | { |
|
11 | 11 | "cell_type": "code", |
|
12 | 12 | "execution_count": null, |
|
13 | 13 | "metadata": { |
|
14 | 14 | "collapsed": false |
|
15 | 15 | }, |
|
16 | 16 | "outputs": [], |
|
17 | 17 | "source": [ |
|
18 | 18 | "%%html\n", |
|
19 | 19 | "<style>\n", |
|
20 | 20 | ".example-container { background: #999999; padding: 2px; min-height: 100px; }\n", |
|
21 | 21 | ".example-container.sm { min-height: 50px; }\n", |
|
22 | 22 | ".example-box { background: #9999FF; width: 50px; height: 50px; text-align: center; vertical-align: middle; color: white; font-weight: bold; margin: 2px;}\n", |
|
23 | 23 | ".example-box.med { width: 65px; height: 65px; } \n", |
|
24 | 24 | ".example-box.lrg { width: 80px; height: 80px; } \n", |
|
25 | 25 | "</style>" |
|
26 | 26 | ] |
|
27 | 27 | }, |
|
28 | 28 | { |
|
29 | 29 | "cell_type": "code", |
|
30 | 30 | "execution_count": null, |
|
31 | 31 | "metadata": { |
|
32 | 32 | "collapsed": false |
|
33 | 33 | }, |
|
34 | 34 | "outputs": [], |
|
35 | 35 | "source": [ |
|
36 | 36 | "from IPython.html import widgets\n", |
|
37 | 37 | "from IPython.display import display" |
|
38 | 38 | ] |
|
39 | 39 | }, |
|
40 | 40 | { |
|
41 | 41 | "cell_type": "markdown", |
|
42 | 42 | "metadata": { |
|
43 | 43 | "slideshow": { |
|
44 | 44 | "slide_type": "slide" |
|
45 | 45 | } |
|
46 | 46 | }, |
|
47 | 47 | "source": [ |
|
48 | 48 | "# Widget Styling" |
|
49 | 49 | ] |
|
50 | 50 | }, |
|
51 | 51 | { |
|
52 | 52 | "cell_type": "markdown", |
|
53 | 53 | "metadata": {}, |
|
54 | 54 | "source": [ |
|
55 | 55 | "## Basic styling" |
|
56 | 56 | ] |
|
57 | 57 | }, |
|
58 | 58 | { |
|
59 | 59 | "cell_type": "markdown", |
|
60 | 60 | "metadata": {}, |
|
61 | 61 | "source": [ |
|
62 | 62 | "The widgets distributed with IPython can be styled by setting the following traits:\n", |
|
63 | 63 | "\n", |
|
64 | 64 | "- width \n", |
|
65 | 65 | "- height \n", |
|
66 | 66 | "- fore_color \n", |
|
67 | 67 | "- back_color \n", |
|
68 | 68 | "- border_color \n", |
|
69 | 69 | "- border_width \n", |
|
70 | 70 | "- border_style \n", |
|
71 | 71 | "- font_style \n", |
|
72 | 72 | "- font_weight \n", |
|
73 | 73 | "- font_size \n", |
|
74 | 74 | "- font_family \n", |
|
75 | 75 | "\n", |
|
76 | 76 | "The example below shows how a `Button` widget can be styled:" |
|
77 | 77 | ] |
|
78 | 78 | }, |
|
79 | 79 | { |
|
80 | 80 | "cell_type": "code", |
|
81 | 81 | "execution_count": null, |
|
82 | 82 | "metadata": { |
|
83 | 83 | "collapsed": false |
|
84 | 84 | }, |
|
85 | 85 | "outputs": [], |
|
86 | 86 | "source": [ |
|
87 | 87 | "button = widgets.Button(\n", |
|
88 | 88 | " description='Hello World!',\n", |
|
89 | 89 | " width=100, # Integers are interpreted as pixel measurements.\n", |
|
90 | 90 | " height='2em', # em is valid HTML unit of measurement.\n", |
|
91 | 91 | " color='lime', # Colors can be set by name,\n", |
|
92 | 92 | " background_color='#0022FF', # and also by color code.\n", |
|
93 | 93 | " border_color='red')\n", |
|
94 | 94 | "display(button)" |
|
95 | 95 | ] |
|
96 | 96 | }, |
|
97 | 97 | { |
|
98 | 98 | "cell_type": "markdown", |
|
99 | 99 | "metadata": { |
|
100 | 100 | "slideshow": { |
|
101 | 101 | "slide_type": "slide" |
|
102 | 102 | } |
|
103 | 103 | }, |
|
104 | 104 | "source": [ |
|
105 | 105 | "## Parent/child relationships" |
|
106 | 106 | ] |
|
107 | 107 | }, |
|
108 | 108 | { |
|
109 | 109 | "cell_type": "markdown", |
|
110 | 110 | "metadata": {}, |
|
111 | 111 | "source": [ |
|
112 | 112 | "To display widget A inside widget B, widget A must be a child of widget B. Widgets that can contain other widgets have a **`children` attribute**. This attribute can be **set via a keyword argument** in the widget's constructor **or after construction**. Calling display on an **object with children automatically displays those children**, too." |
|
113 | 113 | ] |
|
114 | 114 | }, |
|
115 | 115 | { |
|
116 | 116 | "cell_type": "code", |
|
117 | 117 | "execution_count": null, |
|
118 | 118 | "metadata": { |
|
119 | 119 | "collapsed": false |
|
120 | 120 | }, |
|
121 | 121 | "outputs": [], |
|
122 | 122 | "source": [ |
|
123 | 123 | "from IPython.display import display\n", |
|
124 | 124 | "\n", |
|
125 | 125 | "float_range = widgets.FloatSlider()\n", |
|
126 | 126 | "string = widgets.Text(value='hi')\n", |
|
127 | 127 | "container = widgets.Box(children=[float_range, string])\n", |
|
128 | 128 | "\n", |
|
129 | 129 | "container.border_color = 'red'\n", |
|
130 | 130 | "container.border_style = 'dotted'\n", |
|
131 | 131 | "container.border_width = 3\n", |
|
132 | 132 | "display(container) # Displays the `container` and all of it's children." |
|
133 | 133 | ] |
|
134 | 134 | }, |
|
135 | 135 | { |
|
136 | 136 | "cell_type": "markdown", |
|
137 | 137 | "metadata": {}, |
|
138 | 138 | "source": [ |
|
139 | 139 | "### After the parent is displayed" |
|
140 | 140 | ] |
|
141 | 141 | }, |
|
142 | 142 | { |
|
143 | 143 | "cell_type": "markdown", |
|
144 | 144 | "metadata": { |
|
145 | 145 | "slideshow": { |
|
146 | 146 | "slide_type": "slide" |
|
147 | 147 | } |
|
148 | 148 | }, |
|
149 | 149 | "source": [ |
|
150 | 150 | "Children **can be added to parents** after the parent has been displayed. The **parent is responsible for rendering its children**." |
|
151 | 151 | ] |
|
152 | 152 | }, |
|
153 | 153 | { |
|
154 | 154 | "cell_type": "code", |
|
155 | 155 | "execution_count": null, |
|
156 | 156 | "metadata": { |
|
157 | 157 | "collapsed": false |
|
158 | 158 | }, |
|
159 | 159 | "outputs": [], |
|
160 | 160 | "source": [ |
|
161 | 161 | "container = widgets.Box()\n", |
|
162 | 162 | "container.border_color = 'red'\n", |
|
163 | 163 | "container.border_style = 'dotted'\n", |
|
164 | 164 | "container.border_width = 3\n", |
|
165 | 165 | "display(container)\n", |
|
166 | 166 | "\n", |
|
167 | 167 | "int_range = widgets.IntSlider()\n", |
|
168 | 168 | "container.children=[int_range]" |
|
169 | 169 | ] |
|
170 | 170 | }, |
|
171 | 171 | { |
|
172 | 172 | "cell_type": "markdown", |
|
173 | 173 | "metadata": { |
|
174 | 174 | "slideshow": { |
|
175 | 175 | "slide_type": "slide" |
|
176 | 176 | } |
|
177 | 177 | }, |
|
178 | 178 | "source": [ |
|
179 | 179 | "## Fancy boxes" |
|
180 | 180 | ] |
|
181 | 181 | }, |
|
182 | 182 | { |
|
183 | 183 | "cell_type": "markdown", |
|
184 | 184 | "metadata": {}, |
|
185 | 185 | "source": [ |
|
186 | 186 | "If you need to display a more complicated set of widgets, there are **specialized containers** that you can use. To display **multiple sets of widgets**, you can use an **`Accordion` or a `Tab` in combination with one `Box` per set of widgets** (as seen below). The \"pages\" of these widgets are their children. To set the titles of the pages, one must **call `set_title` after the widget has been displayed**." |
|
187 | 187 | ] |
|
188 | 188 | }, |
|
189 | 189 | { |
|
190 | 190 | "cell_type": "markdown", |
|
191 | 191 | "metadata": {}, |
|
192 | 192 | "source": [ |
|
193 | 193 | "### Accordion" |
|
194 | 194 | ] |
|
195 | 195 | }, |
|
196 | 196 | { |
|
197 | 197 | "cell_type": "code", |
|
198 | 198 | "execution_count": null, |
|
199 | 199 | "metadata": { |
|
200 | 200 | "collapsed": false |
|
201 | 201 | }, |
|
202 | 202 | "outputs": [], |
|
203 | 203 | "source": [ |
|
204 | 204 | "name1 = widgets.Text(description='Location:')\n", |
|
205 | 205 | "zip1 = widgets.BoundedIntText(description='Zip:', min=0, max=99999)\n", |
|
206 | 206 | "page1 = widgets.Box(children=[name1, zip1])\n", |
|
207 | 207 | "\n", |
|
208 | 208 | "name2 = widgets.Text(description='Location:')\n", |
|
209 | 209 | "zip2 = widgets.BoundedIntText(description='Zip:', min=0, max=99999)\n", |
|
210 | 210 | "page2 = widgets.Box(children=[name2, zip2])\n", |
|
211 | 211 | "\n", |
|
212 | 212 | "accord = widgets.Accordion(children=[page1, page2])\n", |
|
213 | 213 | "display(accord)\n", |
|
214 | 214 | "\n", |
|
215 | 215 | "accord.set_title(0, 'From')\n", |
|
216 | 216 | "accord.set_title(1, 'To')" |
|
217 | 217 | ] |
|
218 | 218 | }, |
|
219 | 219 | { |
|
220 | 220 | "cell_type": "markdown", |
|
221 | 221 | "metadata": { |
|
222 | 222 | "slideshow": { |
|
223 | 223 | "slide_type": "slide" |
|
224 | 224 | } |
|
225 | 225 | }, |
|
226 | 226 | "source": [ |
|
227 | 227 | "### TabWidget" |
|
228 | 228 | ] |
|
229 | 229 | }, |
|
230 | 230 | { |
|
231 | 231 | "cell_type": "code", |
|
232 | 232 | "execution_count": null, |
|
233 | 233 | "metadata": { |
|
234 | 234 | "collapsed": false |
|
235 | 235 | }, |
|
236 | 236 | "outputs": [], |
|
237 | 237 | "source": [ |
|
238 | 238 | "name = widgets.Text(description='Name:')\n", |
|
239 | 239 | "color = widgets.Dropdown(description='Color:', values=['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'])\n", |
|
240 | 240 | "page1 = widgets.Box(children=[name, color])\n", |
|
241 | 241 | "\n", |
|
242 | 242 | "age = widgets.IntSlider(description='Age:', min=0, max=120, value=50)\n", |
|
243 | 243 | "gender = widgets.RadioButtons(description='Gender:', values=['male', 'female'])\n", |
|
244 | 244 | "page2 = widgets.Box(children=[age, gender])\n", |
|
245 | 245 | "\n", |
|
246 | 246 | "tabs = widgets.Tab(children=[page1, page2])\n", |
|
247 | 247 | "display(tabs)\n", |
|
248 | 248 | "\n", |
|
249 | 249 | "tabs.set_title(0, 'Name')\n", |
|
250 | 250 | "tabs.set_title(1, 'Details')" |
|
251 | 251 | ] |
|
252 | 252 | }, |
|
253 | 253 | { |
|
254 | 254 | "cell_type": "markdown", |
|
255 | 255 | "metadata": { |
|
256 | 256 | "slideshow": { |
|
257 | 257 | "slide_type": "slide" |
|
258 | 258 | } |
|
259 | 259 | }, |
|
260 | 260 | "source": [ |
|
261 | "### Popup" | |
|
262 | ] | |
|
263 | }, | |
|
264 | { | |
|
265 | "cell_type": "markdown", | |
|
266 | "metadata": {}, | |
|
267 | "source": [ | |
|
268 | "Unlike the other two special containers, the `Popup` is only **designed to display one set of widgets**. The `Popup` can be used to **display widgets outside of the widget area**. " | |
|
269 | ] | |
|
270 | }, | |
|
271 | { | |
|
272 | "cell_type": "code", | |
|
273 | "execution_count": null, | |
|
274 | "metadata": { | |
|
275 | "collapsed": false | |
|
276 | }, | |
|
277 | "outputs": [], | |
|
278 | "source": [ | |
|
279 | "counter = widgets.IntText(description='Counter:')\n", | |
|
280 | "popup = widgets.Popup(children=[counter], description='Popup Demo', button_text='Popup Button')\n", | |
|
281 | "display(popup)" | |
|
282 | ] | |
|
283 | }, | |
|
284 | { | |
|
285 | "cell_type": "code", | |
|
286 | "execution_count": null, | |
|
287 | "metadata": { | |
|
288 | "collapsed": false | |
|
289 | }, | |
|
290 | "outputs": [], | |
|
291 | "source": [ | |
|
292 | "counter.value += 1" | |
|
293 | ] | |
|
294 | }, | |
|
295 | { | |
|
296 | "cell_type": "code", | |
|
297 | "execution_count": null, | |
|
298 | "metadata": { | |
|
299 | "collapsed": false | |
|
300 | }, | |
|
301 | "outputs": [], | |
|
302 | "source": [] | |
|
303 | }, | |
|
304 | { | |
|
305 | "cell_type": "code", | |
|
306 | "execution_count": null, | |
|
307 | "metadata": { | |
|
308 | "collapsed": false | |
|
309 | }, | |
|
310 | "outputs": [], | |
|
311 | "source": [] | |
|
312 | }, | |
|
313 | { | |
|
314 | "cell_type": "code", | |
|
315 | "execution_count": null, | |
|
316 | "metadata": { | |
|
317 | "collapsed": false | |
|
318 | }, | |
|
319 | "outputs": [], | |
|
320 | "source": [] | |
|
321 | }, | |
|
322 | { | |
|
323 | "cell_type": "code", | |
|
324 | "execution_count": null, | |
|
325 | "metadata": { | |
|
326 | "collapsed": false | |
|
327 | }, | |
|
328 | "outputs": [], | |
|
329 | "source": [] | |
|
330 | }, | |
|
331 | { | |
|
332 | "cell_type": "code", | |
|
333 | "execution_count": null, | |
|
334 | "metadata": { | |
|
335 | "collapsed": false | |
|
336 | }, | |
|
337 | "outputs": [], | |
|
338 | "source": [] | |
|
339 | }, | |
|
340 | { | |
|
341 | "cell_type": "code", | |
|
342 | "execution_count": null, | |
|
343 | "metadata": { | |
|
344 | "collapsed": false | |
|
345 | }, | |
|
346 | "outputs": [], | |
|
347 | "source": [] | |
|
348 | }, | |
|
349 | { | |
|
350 | "cell_type": "code", | |
|
351 | "execution_count": null, | |
|
352 | "metadata": { | |
|
353 | "collapsed": false | |
|
354 | }, | |
|
355 | "outputs": [], | |
|
356 | "source": [] | |
|
357 | }, | |
|
358 | { | |
|
359 | "cell_type": "code", | |
|
360 | "execution_count": null, | |
|
361 | "metadata": { | |
|
362 | "collapsed": false | |
|
363 | }, | |
|
364 | "outputs": [], | |
|
365 | "source": [] | |
|
366 | }, | |
|
367 | { | |
|
368 | "cell_type": "code", | |
|
369 | "execution_count": null, | |
|
370 | "metadata": { | |
|
371 | "collapsed": false | |
|
372 | }, | |
|
373 | "outputs": [], | |
|
374 | "source": [] | |
|
375 | }, | |
|
376 | { | |
|
377 | "cell_type": "code", | |
|
378 | "execution_count": null, | |
|
379 | "metadata": { | |
|
380 | "collapsed": false | |
|
381 | }, | |
|
382 | "outputs": [], | |
|
383 | "source": [] | |
|
384 | }, | |
|
385 | { | |
|
386 | "cell_type": "code", | |
|
387 | "execution_count": null, | |
|
388 | "metadata": { | |
|
389 | "collapsed": false | |
|
390 | }, | |
|
391 | "outputs": [], | |
|
392 | "source": [] | |
|
393 | }, | |
|
394 | { | |
|
395 | "cell_type": "code", | |
|
396 | "execution_count": null, | |
|
397 | "metadata": { | |
|
398 | "collapsed": false | |
|
399 | }, | |
|
400 | "outputs": [], | |
|
401 | "source": [] | |
|
402 | }, | |
|
403 | { | |
|
404 | "cell_type": "code", | |
|
405 | "execution_count": null, | |
|
406 | "metadata": { | |
|
407 | "collapsed": false | |
|
408 | }, | |
|
409 | "outputs": [], | |
|
410 | "source": [] | |
|
411 | }, | |
|
412 | { | |
|
413 | "cell_type": "code", | |
|
414 | "execution_count": null, | |
|
415 | "metadata": { | |
|
416 | "collapsed": false | |
|
417 | }, | |
|
418 | "outputs": [], | |
|
419 | "source": [] | |
|
420 | }, | |
|
421 | { | |
|
422 | "cell_type": "code", | |
|
423 | "execution_count": null, | |
|
424 | "metadata": { | |
|
425 | "collapsed": false | |
|
426 | }, | |
|
427 | "outputs": [], | |
|
428 | "source": [ | |
|
429 | "counter.value += 1" | |
|
430 | ] | |
|
431 | }, | |
|
432 | { | |
|
433 | "cell_type": "code", | |
|
434 | "execution_count": null, | |
|
435 | "metadata": { | |
|
436 | "collapsed": false | |
|
437 | }, | |
|
438 | "outputs": [], | |
|
439 | "source": [ | |
|
440 | "popup.close()" | |
|
441 | ] | |
|
442 | }, | |
|
443 | { | |
|
444 | "cell_type": "markdown", | |
|
445 | "metadata": { | |
|
446 | "slideshow": { | |
|
447 | "slide_type": "slide" | |
|
448 | } | |
|
449 | }, | |
|
450 | "source": [ | |
|
451 | 261 | "# Alignment" |
|
452 | 262 | ] |
|
453 | 263 | }, |
|
454 | 264 | { |
|
455 | 265 | "cell_type": "markdown", |
|
456 | 266 | "metadata": {}, |
|
457 | 267 | "source": [ |
|
458 | 268 | "Most widgets have a **`description` attribute**, which allows a label for the widget to be defined.\n", |
|
459 | 269 | "The label of the widget **has a fixed minimum width**.\n", |
|
460 | 270 | "The text of the label is **always right aligned and the widget is left aligned**:" |
|
461 | 271 | ] |
|
462 | 272 | }, |
|
463 | 273 | { |
|
464 | 274 | "cell_type": "code", |
|
465 | 275 | "execution_count": null, |
|
466 | 276 | "metadata": { |
|
467 | 277 | "collapsed": false |
|
468 | 278 | }, |
|
469 | 279 | "outputs": [], |
|
470 | 280 | "source": [ |
|
471 | 281 | "display(widgets.Text(description=\"a:\"))\n", |
|
472 | 282 | "display(widgets.Text(description=\"aa:\"))\n", |
|
473 | 283 | "display(widgets.Text(description=\"aaa:\"))" |
|
474 | 284 | ] |
|
475 | 285 | }, |
|
476 | 286 | { |
|
477 | 287 | "cell_type": "markdown", |
|
478 | 288 | "metadata": { |
|
479 | 289 | "slideshow": { |
|
480 | 290 | "slide_type": "slide" |
|
481 | 291 | } |
|
482 | 292 | }, |
|
483 | 293 | "source": [ |
|
484 | 294 | "If a **label is longer** than the minimum width, the **widget is shifted to the right**:" |
|
485 | 295 | ] |
|
486 | 296 | }, |
|
487 | 297 | { |
|
488 | 298 | "cell_type": "code", |
|
489 | 299 | "execution_count": null, |
|
490 | 300 | "metadata": { |
|
491 | 301 | "collapsed": false |
|
492 | 302 | }, |
|
493 | 303 | "outputs": [], |
|
494 | 304 | "source": [ |
|
495 | 305 | "display(widgets.Text(description=\"a:\"))\n", |
|
496 | 306 | "display(widgets.Text(description=\"aa:\"))\n", |
|
497 | 307 | "display(widgets.Text(description=\"aaa:\"))\n", |
|
498 | 308 | "display(widgets.Text(description=\"aaaaaaaaaaaaaaaaaa:\"))" |
|
499 | 309 | ] |
|
500 | 310 | }, |
|
501 | 311 | { |
|
502 | 312 | "cell_type": "markdown", |
|
503 | 313 | "metadata": { |
|
504 | 314 | "slideshow": { |
|
505 | 315 | "slide_type": "slide" |
|
506 | 316 | } |
|
507 | 317 | }, |
|
508 | 318 | "source": [ |
|
509 | 319 | "If a `description` is **not set** for the widget, the **label is not displayed**:" |
|
510 | 320 | ] |
|
511 | 321 | }, |
|
512 | 322 | { |
|
513 | 323 | "cell_type": "code", |
|
514 | 324 | "execution_count": null, |
|
515 | 325 | "metadata": { |
|
516 | 326 | "collapsed": false |
|
517 | 327 | }, |
|
518 | 328 | "outputs": [], |
|
519 | 329 | "source": [ |
|
520 | 330 | "display(widgets.Text(description=\"a:\"))\n", |
|
521 | 331 | "display(widgets.Text(description=\"aa:\"))\n", |
|
522 | 332 | "display(widgets.Text(description=\"aaa:\"))\n", |
|
523 | 333 | "display(widgets.Text())" |
|
524 | 334 | ] |
|
525 | 335 | }, |
|
526 | 336 | { |
|
527 | 337 | "cell_type": "markdown", |
|
528 | 338 | "metadata": { |
|
529 | 339 | "slideshow": { |
|
530 | 340 | "slide_type": "slide" |
|
531 | 341 | } |
|
532 | 342 | }, |
|
533 | 343 | "source": [ |
|
534 | 344 | "## Flex boxes" |
|
535 | 345 | ] |
|
536 | 346 | }, |
|
537 | 347 | { |
|
538 | 348 | "cell_type": "markdown", |
|
539 | 349 | "metadata": {}, |
|
540 | 350 | "source": [ |
|
541 | 351 | "Widgets can be aligned using the `FlexBox`, `HBox`, and `VBox` widgets." |
|
542 | 352 | ] |
|
543 | 353 | }, |
|
544 | 354 | { |
|
545 | 355 | "cell_type": "markdown", |
|
546 | 356 | "metadata": { |
|
547 | 357 | "slideshow": { |
|
548 | 358 | "slide_type": "slide" |
|
549 | 359 | } |
|
550 | 360 | }, |
|
551 | 361 | "source": [ |
|
552 | 362 | "### Application to widgets" |
|
553 | 363 | ] |
|
554 | 364 | }, |
|
555 | 365 | { |
|
556 | 366 | "cell_type": "markdown", |
|
557 | 367 | "metadata": {}, |
|
558 | 368 | "source": [ |
|
559 | 369 | "Widgets display vertically by default:" |
|
560 | 370 | ] |
|
561 | 371 | }, |
|
562 | 372 | { |
|
563 | 373 | "cell_type": "code", |
|
564 | 374 | "execution_count": null, |
|
565 | 375 | "metadata": { |
|
566 | 376 | "collapsed": false |
|
567 | 377 | }, |
|
568 | 378 | "outputs": [], |
|
569 | 379 | "source": [ |
|
570 | 380 | "buttons = [widgets.Button(description=str(i)) for i in range(3)]\n", |
|
571 | 381 | "display(*buttons)" |
|
572 | 382 | ] |
|
573 | 383 | }, |
|
574 | 384 | { |
|
575 | 385 | "cell_type": "markdown", |
|
576 | 386 | "metadata": { |
|
577 | 387 | "slideshow": { |
|
578 | 388 | "slide_type": "slide" |
|
579 | 389 | } |
|
580 | 390 | }, |
|
581 | 391 | "source": [ |
|
582 | 392 | "### Using hbox" |
|
583 | 393 | ] |
|
584 | 394 | }, |
|
585 | 395 | { |
|
586 | 396 | "cell_type": "markdown", |
|
587 | 397 | "metadata": {}, |
|
588 | 398 | "source": [ |
|
589 | 399 | "To make widgets display horizontally, you need to **child them to a `HBox` widget**." |
|
590 | 400 | ] |
|
591 | 401 | }, |
|
592 | 402 | { |
|
593 | 403 | "cell_type": "code", |
|
594 | 404 | "execution_count": null, |
|
595 | 405 | "metadata": { |
|
596 | 406 | "collapsed": false |
|
597 | 407 | }, |
|
598 | 408 | "outputs": [], |
|
599 | 409 | "source": [ |
|
600 | 410 | "container = widgets.HBox(children=buttons)\n", |
|
601 | 411 | "display(container)" |
|
602 | 412 | ] |
|
603 | 413 | }, |
|
604 | 414 | { |
|
605 | 415 | "cell_type": "markdown", |
|
606 | 416 | "metadata": {}, |
|
607 | 417 | "source": [ |
|
608 | 418 | "By setting the width of the container to 100% and its `pack` to `center`, you can center the buttons." |
|
609 | 419 | ] |
|
610 | 420 | }, |
|
611 | 421 | { |
|
612 | 422 | "cell_type": "code", |
|
613 | 423 | "execution_count": null, |
|
614 | 424 | "metadata": { |
|
615 | 425 | "collapsed": false |
|
616 | 426 | }, |
|
617 | 427 | "outputs": [], |
|
618 | 428 | "source": [ |
|
619 | 429 | "container.width = '100%'\n", |
|
620 | 430 | "container.pack = 'center'" |
|
621 | 431 | ] |
|
622 | 432 | }, |
|
623 | 433 | { |
|
624 | 434 | "cell_type": "markdown", |
|
625 | 435 | "metadata": { |
|
626 | 436 | "slideshow": { |
|
627 | 437 | "slide_type": "slide" |
|
628 | 438 | } |
|
629 | 439 | }, |
|
630 | 440 | "source": [ |
|
631 | 441 | "## Visibility" |
|
632 | 442 | ] |
|
633 | 443 | }, |
|
634 | 444 | { |
|
635 | 445 | "cell_type": "markdown", |
|
636 | 446 | "metadata": {}, |
|
637 | 447 | "source": [ |
|
638 | 448 | "Sometimes it is necessary to **hide or show widgets** in place, **without having to re-display** the widget.\n", |
|
639 | 449 | "The `visible` property of widgets can be used to hide or show **widgets that have already been displayed** (as seen below). The `visible` property can be:\n", |
|
640 | 450 | "* `True` - the widget is displayed\n", |
|
641 | 451 | "* `False` - the widget is hidden, and the empty space where the widget would be is collapsed\n", |
|
642 | 452 | "* `None` - the widget is hidden, and the empty space where the widget would be is shown" |
|
643 | 453 | ] |
|
644 | 454 | }, |
|
645 | 455 | { |
|
646 | 456 | "cell_type": "code", |
|
647 | 457 | "execution_count": null, |
|
648 | 458 | "metadata": { |
|
649 | 459 | "collapsed": false |
|
650 | 460 | }, |
|
651 | 461 | "outputs": [], |
|
652 | 462 | "source": [ |
|
653 | 463 | "w1 = widgets.Latex(value=\"First line\")\n", |
|
654 | 464 | "w2 = widgets.Latex(value=\"Second line\")\n", |
|
655 | 465 | "w3 = widgets.Latex(value=\"Third line\")\n", |
|
656 | 466 | "display(w1, w2, w3)" |
|
657 | 467 | ] |
|
658 | 468 | }, |
|
659 | 469 | { |
|
660 | 470 | "cell_type": "code", |
|
661 | 471 | "execution_count": null, |
|
662 | 472 | "metadata": { |
|
663 | 473 | "collapsed": true |
|
664 | 474 | }, |
|
665 | 475 | "outputs": [], |
|
666 | 476 | "source": [ |
|
667 | 477 | "w2.visible=None" |
|
668 | 478 | ] |
|
669 | 479 | }, |
|
670 | 480 | { |
|
671 | 481 | "cell_type": "code", |
|
672 | 482 | "execution_count": null, |
|
673 | 483 | "metadata": { |
|
674 | 484 | "collapsed": false |
|
675 | 485 | }, |
|
676 | 486 | "outputs": [], |
|
677 | 487 | "source": [ |
|
678 | 488 | "w2.visible=False" |
|
679 | 489 | ] |
|
680 | 490 | }, |
|
681 | 491 | { |
|
682 | 492 | "cell_type": "code", |
|
683 | 493 | "execution_count": null, |
|
684 | 494 | "metadata": { |
|
685 | 495 | "collapsed": false |
|
686 | 496 | }, |
|
687 | 497 | "outputs": [], |
|
688 | 498 | "source": [ |
|
689 | 499 | "w2.visible=True" |
|
690 | 500 | ] |
|
691 | 501 | }, |
|
692 | 502 | { |
|
693 | 503 | "cell_type": "markdown", |
|
694 | 504 | "metadata": { |
|
695 | 505 | "slideshow": { |
|
696 | 506 | "slide_type": "slide" |
|
697 | 507 | } |
|
698 | 508 | }, |
|
699 | 509 | "source": [ |
|
700 | 510 | "### Another example" |
|
701 | 511 | ] |
|
702 | 512 | }, |
|
703 | 513 | { |
|
704 | 514 | "cell_type": "markdown", |
|
705 | 515 | "metadata": {}, |
|
706 | 516 | "source": [ |
|
707 | 517 | "In the example below, a form is rendered, which conditionally displays widgets depending on the state of other widgets. Try toggling the student checkbox." |
|
708 | 518 | ] |
|
709 | 519 | }, |
|
710 | 520 | { |
|
711 | 521 | "cell_type": "code", |
|
712 | 522 | "execution_count": null, |
|
713 | 523 | "metadata": { |
|
714 | 524 | "collapsed": false |
|
715 | 525 | }, |
|
716 | 526 | "outputs": [], |
|
717 | 527 | "source": [ |
|
718 | 528 | "form = widgets.VBox()\n", |
|
719 | 529 | "first = widgets.Text(description=\"First Name:\")\n", |
|
720 | 530 | "last = widgets.Text(description=\"Last Name:\")\n", |
|
721 | 531 | "\n", |
|
722 | 532 | "student = widgets.Checkbox(description=\"Student:\", value=False)\n", |
|
723 | 533 | "school_info = widgets.VBox(visible=False, children=[\n", |
|
724 | 534 | " widgets.Text(description=\"School:\"),\n", |
|
725 | 535 | " widgets.IntText(description=\"Grade:\", min=0, max=12)\n", |
|
726 | 536 | " ])\n", |
|
727 | 537 | "\n", |
|
728 | 538 | "pet = widgets.Text(description=\"Pet's Name:\")\n", |
|
729 | 539 | "form.children = [first, last, student, school_info, pet]\n", |
|
730 | 540 | "display(form)\n", |
|
731 | 541 | "\n", |
|
732 | 542 | "def on_student_toggle(name, value):\n", |
|
733 | 543 | " if value:\n", |
|
734 | 544 | " school_info.visible = True\n", |
|
735 | 545 | " else:\n", |
|
736 | 546 | " school_info.visible = False\n", |
|
737 | 547 | "student.on_trait_change(on_student_toggle, 'value')\n" |
|
738 | 548 | ] |
|
739 | 549 | }, |
|
740 | 550 | { |
|
741 | 551 | "cell_type": "markdown", |
|
742 | 552 | "metadata": {}, |
|
743 | 553 | "source": [ |
|
744 | 554 | "[Index](Index.ipynb) - [Back](Widget Events.ipynb) - [Next](Custom Widget - Hello World.ipynb)" |
|
745 | 555 | ] |
|
746 | 556 | } |
|
747 | 557 | ], |
|
748 | 558 | "metadata": { |
|
749 | 559 | "cell_tags": [ |
|
750 | 560 | [ |
|
751 | 561 | "<None>", |
|
752 | 562 | null |
|
753 | 563 | ] |
|
754 | 564 | ], |
|
755 | 565 | "kernelspec": { |
|
756 | "display_name": "Python 2", | |
|
566 | "display_name": "IPython (Python 2)", | |
|
757 | 567 | "name": "python2" |
|
758 | 568 | }, |
|
759 | 569 | "language_info": { |
|
760 | 570 | "codemirror_mode": { |
|
761 | 571 | "name": "ipython", |
|
762 | 572 | "version": 2 |
|
763 | 573 | }, |
|
764 | 574 | "file_extension": ".py", |
|
765 | 575 | "mimetype": "text/x-python", |
|
766 | 576 | "name": "python", |
|
767 | 577 | "nbconvert_exporter": "python", |
|
768 | 578 | "pygments_lexer": "ipython2", |
|
769 |
"version": "2.7. |
|
|
770 |
} |
|
|
771 | "signature": "sha256:198630bf2c2eb00401b60a395ebc75049099864b62f0faaf416da02f9808c40b" | |
|
579 | "version": "2.7.6" | |
|
580 | } | |
|
772 | 581 | }, |
|
773 | 582 | "nbformat": 4, |
|
774 | 583 | "nbformat_minor": 0 |
|
775 | 584 | } No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now