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