##// END OF EJS Templates
Merge pull request #2362 from hmeine/master...
Bussonnier Matthias -
r8362:9e2ba1f0 merge
parent child Browse files
Show More
@@ -1,275 +1,259 b''
1 1 {
2 2 "metadata": {
3 3 "name": "Animations_and_Progress"
4 4 },
5 5 "nbformat": 3,
6 6 "nbformat_minor": 0,
7 7 "worksheets": [
8 8 {
9 9 "cells": [
10 10 {
11 11 "cell_type": "heading",
12 12 "level": 1,
13 13 "metadata": {},
14 14 "source": [
15 15 "Simple animations, progress bars, and clearing output"
16 16 ]
17 17 },
18 18 {
19 19 "cell_type": "markdown",
20 20 "metadata": {},
21 21 "source": [
22 22 "Sometimes you want to print progress in-place, but don't want\n",
23 23 "to keep growing the output area. In terminals, there is the carriage-return\n",
24 24 "(`'\\r'`) for overwriting a single line, but the notebook frontend does not support this\n",
25 25 "behavior (yet).\n",
26 26 "\n",
27 27 "What the notebook *does* support is explicit `clear_output`, and you can use this to replace previous\n",
28 28 "output (specifying stdout/stderr or the special IPython display outputs)."
29 29 ]
30 30 },
31 31 {
32 32 "cell_type": "markdown",
33 33 "metadata": {},
34 34 "source": [
35 35 "A simple example printing our progress iterating through a list:"
36 36 ]
37 37 },
38 38 {
39 39 "cell_type": "code",
40 40 "collapsed": true,
41 41 "input": [
42 42 "import sys\n",
43 43 "import time"
44 44 ],
45 45 "language": "python",
46 46 "metadata": {},
47 47 "outputs": []
48 48 },
49 49 {
50 50 "cell_type": "code",
51 51 "collapsed": false,
52 52 "input": [
53 53 "from IPython.display import clear_output\n",
54 54 "for i in range(10):\n",
55 55 " time.sleep(0.25)\n",
56 56 " clear_output()\n",
57 57 " print i\n",
58 58 " sys.stdout.flush()"
59 59 ],
60 60 "language": "python",
61 61 "metadata": {},
62 62 "outputs": []
63 63 },
64 64 {
65 65 "cell_type": "markdown",
66 66 "metadata": {},
67 67 "source": [
68 68 "The AsyncResult object has a special `wait_interactive()` method, which prints its progress interactively,\n",
69 69 "so you can watch as your parallel computation completes.\n",
70 70 "\n",
71 71 "**This example assumes you have an IPython cluster running, which you can start from the [cluster panel](/#tab2)**"
72 72 ]
73 73 },
74 74 {
75 75 "cell_type": "code",
76 76 "collapsed": false,
77 77 "input": [
78 78 "from IPython import parallel\n",
79 79 "rc = parallel.Client()\n",
80 80 "view = rc.load_balanced_view()\n",
81 81 "\n",
82 82 "amr = view.map_async(time.sleep, [0.5]*100)\n",
83 83 "\n",
84 84 "amr.wait_interactive()"
85 85 ],
86 86 "language": "python",
87 87 "metadata": {},
88 88 "outputs": []
89 89 },
90 90 {
91 91 "cell_type": "markdown",
92 92 "metadata": {},
93 93 "source": [
94 94 "You can also use `clear_output()` to clear figures and plots.\n",
95 95 "\n",
96 96 "This time, we need to make sure we are using inline pylab (**requires matplotlib**)"
97 97 ]
98 98 },
99 99 {
100 100 "cell_type": "code",
101 101 "collapsed": false,
102 102 "input": [
103 103 "%pylab inline"
104 104 ],
105 105 "language": "python",
106 106 "metadata": {},
107 107 "outputs": []
108 108 },
109 109 {
110 110 "cell_type": "code",
111 111 "collapsed": false,
112 112 "input": [
113 113 "from scipy.special import jn\n",
114 114 "x = np.linspace(0,5)\n",
115 115 "f, ax = plt.subplots()\n",
116 116 "ax.set_title(\"Bessel functions\")\n",
117 117 "\n",
118 118 "for n in range(1,10):\n",
119 119 " time.sleep(1)\n",
120 120 " ax.plot(x, jn(x,n))\n",
121 121 " clear_output()\n",
122 122 " display(f)\n",
123 123 "\n",
124 124 "# close the figure at the end, so we don't get a duplicate\n",
125 125 "# of the last plot\n",
126 126 "plt.close()"
127 127 ],
128 128 "language": "python",
129 129 "metadata": {},
130 130 "outputs": []
131 131 },
132 132 {
133 133 "cell_type": "heading",
134 134 "level": 2,
135 135 "metadata": {},
136 136 "source": [
137 137 "A Javascript Progress Bar"
138 138 ]
139 139 },
140 140 {
141 141 "cell_type": "markdown",
142 142 "metadata": {},
143 143 "source": [
144 144 "`clear_output()` is still something of a hack, and if you want to do a progress bar in the notebook\n",
145 145 "it is better to just use Javascript/HTML if you can.\n",
146 146 "\n",
147 147 "Here is a simple progress bar using HTML/Javascript:"
148 148 ]
149 149 },
150 150 {
151 151 "cell_type": "code",
152 152 "collapsed": false,
153 153 "input": [
154 154 "import uuid\n",
155 155 "from IPython.display import HTML, Javascript, display\n",
156 156 "\n",
157 157 "divid = str(uuid.uuid4())\n",
158 158 "\n",
159 159 "pb = HTML(\n",
160 160 "\"\"\"\n",
161 161 "<div style=\"border: 1px solid black; width:500px\">\n",
162 162 " <div id=\"%s\" style=\"background-color:blue; width:0%%\">&nbsp;</div>\n",
163 163 "</div> \n",
164 164 "\"\"\" % divid)\n",
165 165 "display(pb)\n",
166 166 "for i in range(1,101):\n",
167 167 " time.sleep(0.1)\n",
168 168 " \n",
169 169 " display(Javascript(\"$('div#%s').width('%i%%')\" % (divid, i)))"
170 170 ],
171 171 "language": "python",
172 172 "metadata": {},
173 173 "outputs": []
174 174 },
175 175 {
176 176 "cell_type": "markdown",
177 177 "metadata": {},
178 178 "source": [
179 179 "The above simply makes a div that is a box, and a blue div inside it with a unique ID \n",
180 180 "(so that the javascript won't collide with other similar progress bars on the same page). \n",
181 181 "\n",
182 182 "Then, at every progress point, we run a simple jQuery call to resize the blue box to\n",
183 183 "the appropriate fraction of the width of its containing box, and voil\u00e0 a nice\n",
184 184 "HTML/Javascript progress bar!"
185 185 ]
186 186 },
187 187 {
188 188 "cell_type": "heading",
189 189 "level": 2,
190 190 "metadata": {},
191 191 "source": [
192 192 "ProgressBar class"
193 193 ]
194 194 },
195 195 {
196 196 "cell_type": "markdown",
197 197 "metadata": {},
198 198 "source": [
199 199 "And finally, here is a progress bar *class* extracted from [PyMC](http://code.google.com/p/pymc/), which will work in regular Python as well as in the IPython Notebook"
200 200 ]
201 201 },
202 202 {
203 203 "cell_type": "code",
204 204 "collapsed": true,
205 205 "input": [
206 206 "import sys, time\n",
207 "try:\n",
208 " from IPython.display import clear_output\n",
209 " have_ipython = True\n",
210 "except ImportError:\n",
211 " have_ipython = False\n",
212 207 "\n",
213 208 "class ProgressBar:\n",
214 209 " def __init__(self, iterations):\n",
215 210 " self.iterations = iterations\n",
216 211 " self.prog_bar = '[]'\n",
217 212 " self.fill_char = '*'\n",
218 " self.width = 40\n",
213 " self.width = 50\n",
219 214 " self.__update_amount(0)\n",
220 " if have_ipython:\n",
221 " self.animate = self.animate_ipython\n",
222 " else:\n",
223 " self.animate = self.animate_noipython\n",
224 215 "\n",
225 " def animate_ipython(self, iter):\n",
216 " def animate(self, iter):\n",
226 217 " print '\\r', self,\n",
227 218 " sys.stdout.flush()\n",
228 219 " self.update_iteration(iter + 1)\n",
229 220 "\n",
230 221 " def update_iteration(self, elapsed_iter):\n",
231 222 " self.__update_amount((elapsed_iter / float(self.iterations)) * 100.0)\n",
232 223 " self.prog_bar += ' %d of %s complete' % (elapsed_iter, self.iterations)\n",
233 224 "\n",
234 225 " def __update_amount(self, new_amount):\n",
235 226 " percent_done = int(round((new_amount / 100.0) * 100.0))\n",
236 227 " all_full = self.width - 2\n",
237 228 " num_hashes = int(round((percent_done / 100.0) * all_full))\n",
238 229 " self.prog_bar = '[' + self.fill_char * num_hashes + ' ' * (all_full - num_hashes) + ']'\n",
239 230 " pct_place = (len(self.prog_bar) // 2) - len(str(percent_done))\n",
240 231 " pct_string = '%d%%' % percent_done\n",
241 232 " self.prog_bar = self.prog_bar[0:pct_place] + \\\n",
242 233 " (pct_string + self.prog_bar[pct_place + len(pct_string):])\n",
243 234 "\n",
244 235 " def __str__(self):\n",
245 236 " return str(self.prog_bar)"
246 237 ],
247 238 "language": "python",
248 239 "metadata": {},
249 240 "outputs": []
250 241 },
251 242 {
252 243 "cell_type": "code",
253 244 "collapsed": false,
254 245 "input": [
255 246 "p = ProgressBar(1000)\n",
256 247 "for i in range(1001):\n",
248 " time.sleep(0.002)\n",
257 249 " p.animate(i)"
258 250 ],
259 251 "language": "python",
260 252 "metadata": {},
261 253 "outputs": []
262 },
263 {
264 "cell_type": "code",
265 "collapsed": false,
266 "input": [],
267 "language": "python",
268 "metadata": {},
269 "outputs": []
270 254 }
271 255 ],
272 256 "metadata": {}
273 257 }
274 258 ]
275 259 } No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now