Show More
@@ -774,6 +774,19 b' class ProgressBar(DisplayObject):' | |||||
774 | self._progress = value |
|
774 | self._progress = value | |
775 | self.update() |
|
775 | self.update() | |
776 |
|
776 | |||
|
777 | def __iter__(self): | |||
|
778 | self.display() | |||
|
779 | self._progress = -1 # First iteration is 0 | |||
|
780 | return self | |||
|
781 | ||||
|
782 | def __next__(self): | |||
|
783 | """Returns current value and increments display by one.""" | |||
|
784 | self.progress += 1 | |||
|
785 | if self.progress < self.total: | |||
|
786 | return self.progress | |||
|
787 | else: | |||
|
788 | raise StopIteration() | |||
|
789 | ||||
777 | class JSON(DisplayObject): |
|
790 | class JSON(DisplayObject): | |
778 | """JSON expects a JSON-able dict or list |
|
791 | """JSON expects a JSON-able dict or list | |
779 |
|
792 |
@@ -11,6 +11,7 b' import nose.tools as nt' | |||||
11 |
|
11 | |||
12 | from IPython.core import display |
|
12 | from IPython.core import display | |
13 | from IPython.core.getipython import get_ipython |
|
13 | from IPython.core.getipython import get_ipython | |
|
14 | from IPython.utils.io import capture_output | |||
14 | from IPython.utils.tempdir import NamedFileInTemporaryDirectory |
|
15 | from IPython.utils.tempdir import NamedFileInTemporaryDirectory | |
15 | from IPython import paths as ipath |
|
16 | from IPython import paths as ipath | |
16 | from IPython.testing.tools import AssertPrints, AssertNotPrints |
|
17 | from IPython.testing.tools import AssertPrints, AssertNotPrints | |
@@ -196,11 +197,19 b' def test_displayobject_repr():' | |||||
196 |
|
197 | |||
197 | def test_progress(): |
|
198 | def test_progress(): | |
198 | p = display.ProgressBar(10) |
|
199 | p = display.ProgressBar(10) | |
199 |
nt.assert_ |
|
200 | nt.assert_in('0/10',repr(p)) | |
200 | p.html_width = '100%' |
|
201 | p.html_width = '100%' | |
201 | p.progress = 5 |
|
202 | p.progress = 5 | |
202 | nt.assert_equal(p._repr_html_(), "<progress style='width:100%' max='10' value='5'></progress>") |
|
203 | nt.assert_equal(p._repr_html_(), "<progress style='width:100%' max='10' value='5'></progress>") | |
203 |
|
204 | |||
|
205 | def test_progress_iter(): | |||
|
206 | with capture_output(display=False) as captured: | |||
|
207 | for i in display.ProgressBar(5): | |||
|
208 | out = captured.stdout | |||
|
209 | nt.assert_in('{0}/5'.format(i), out) | |||
|
210 | out = captured.stdout | |||
|
211 | nt.assert_in('5/5', out) | |||
|
212 | ||||
204 | def test_json(): |
|
213 | def test_json(): | |
205 | d = {'a': 5} |
|
214 | d = {'a': 5} | |
206 | lis = [d] |
|
215 | lis = [d] |
@@ -21,10 +21,8 b'' | |||||
21 | }, |
|
21 | }, | |
22 | { |
|
22 | { | |
23 | "cell_type": "code", |
|
23 | "cell_type": "code", | |
24 |
"execution_count": 1 |
|
24 | "execution_count": 1, | |
25 | "metadata": { |
|
25 | "metadata": {}, | |
26 | "collapsed": true |
|
|||
27 | }, |
|
|||
28 | "outputs": [], |
|
26 | "outputs": [], | |
29 | "source": [ |
|
27 | "source": [ | |
30 | "from IPython.display import display, update_display" |
|
28 | "from IPython.display import display, update_display" | |
@@ -32,7 +30,7 b'' | |||||
32 | }, |
|
30 | }, | |
33 | { |
|
31 | { | |
34 | "cell_type": "code", |
|
32 | "cell_type": "code", | |
35 |
"execution_count": |
|
33 | "execution_count": 2, | |
36 | "metadata": {}, |
|
34 | "metadata": {}, | |
37 | "outputs": [ |
|
35 | "outputs": [ | |
38 | { |
|
36 | { | |
@@ -50,7 +48,7 b'' | |||||
50 | "<DisplayHandle display_id=update-me>" |
|
48 | "<DisplayHandle display_id=update-me>" | |
51 | ] |
|
49 | ] | |
52 | }, |
|
50 | }, | |
53 |
"execution_count": |
|
51 | "execution_count": 2, | |
54 | "metadata": {}, |
|
52 | "metadata": {}, | |
55 | "output_type": "execute_result" |
|
53 | "output_type": "execute_result" | |
56 | } |
|
54 | } | |
@@ -70,7 +68,7 b'' | |||||
70 | }, |
|
68 | }, | |
71 | { |
|
69 | { | |
72 | "cell_type": "code", |
|
70 | "cell_type": "code", | |
73 |
"execution_count": |
|
71 | "execution_count": 3, | |
74 | "metadata": {}, |
|
72 | "metadata": {}, | |
75 | "outputs": [ |
|
73 | "outputs": [ | |
76 | { |
|
74 | { | |
@@ -97,10 +95,8 b'' | |||||
97 | }, |
|
95 | }, | |
98 | { |
|
96 | { | |
99 | "cell_type": "code", |
|
97 | "cell_type": "code", | |
100 |
"execution_count": |
|
98 | "execution_count": 4, | |
101 | "metadata": { |
|
99 | "metadata": {}, | |
102 | "collapsed": true |
|
|||
103 | }, |
|
|||
104 | "outputs": [], |
|
100 | "outputs": [], | |
105 | "source": [ |
|
101 | "source": [ | |
106 | "handle.update('z')" |
|
102 | "handle.update('z')" | |
@@ -116,7 +112,7 b'' | |||||
116 | }, |
|
112 | }, | |
117 | { |
|
113 | { | |
118 | "cell_type": "code", |
|
114 | "cell_type": "code", | |
119 |
"execution_count": |
|
115 | "execution_count": 5, | |
120 | "metadata": {}, |
|
116 | "metadata": {}, | |
121 | "outputs": [ |
|
117 | "outputs": [ | |
122 | { |
|
118 | { | |
@@ -131,10 +127,10 b'' | |||||
131 | { |
|
127 | { | |
132 | "data": { |
|
128 | "data": { | |
133 | "text/plain": [ |
|
129 | "text/plain": [ | |
134 |
"<DisplayHandle display_id= |
|
130 | "<DisplayHandle display_id=118a56127f42348f8893440da7181c57>" | |
135 | ] |
|
131 | ] | |
136 | }, |
|
132 | }, | |
137 |
"execution_count": |
|
133 | "execution_count": 5, | |
138 | "metadata": {}, |
|
134 | "metadata": {}, | |
139 | "output_type": "execute_result" |
|
135 | "output_type": "execute_result" | |
140 | } |
|
136 | } | |
@@ -154,7 +150,7 b'' | |||||
154 | }, |
|
150 | }, | |
155 | { |
|
151 | { | |
156 | "cell_type": "code", |
|
152 | "cell_type": "code", | |
157 |
"execution_count": |
|
153 | "execution_count": 6, | |
158 | "metadata": {}, |
|
154 | "metadata": {}, | |
159 | "outputs": [ |
|
155 | "outputs": [ | |
160 | { |
|
156 | { | |
@@ -173,7 +169,7 b'' | |||||
173 | }, |
|
169 | }, | |
174 | { |
|
170 | { | |
175 | "cell_type": "code", |
|
171 | "cell_type": "code", | |
176 |
"execution_count": |
|
172 | "execution_count": 7, | |
177 | "metadata": {}, |
|
173 | "metadata": {}, | |
178 | "outputs": [ |
|
174 | "outputs": [ | |
179 | { |
|
175 | { | |
@@ -200,10 +196,8 b'' | |||||
200 | }, |
|
196 | }, | |
201 | { |
|
197 | { | |
202 | "cell_type": "code", |
|
198 | "cell_type": "code", | |
203 |
"execution_count": |
|
199 | "execution_count": 8, | |
204 | "metadata": { |
|
200 | "metadata": {}, | |
205 | "collapsed": true |
|
|||
206 | }, |
|
|||
207 | "outputs": [], |
|
201 | "outputs": [], | |
208 | "source": [ |
|
202 | "source": [ | |
209 | "update_display('z', display_id='here')" |
|
203 | "update_display('z', display_id='here')" | |
@@ -222,16 +216,16 b'' | |||||
222 | }, |
|
216 | }, | |
223 | { |
|
217 | { | |
224 | "cell_type": "code", |
|
218 | "cell_type": "code", | |
225 |
"execution_count": |
|
219 | "execution_count": 9, | |
226 | "metadata": {}, |
|
220 | "metadata": {}, | |
227 | "outputs": [ |
|
221 | "outputs": [ | |
228 | { |
|
222 | { | |
229 | "data": { |
|
223 | "data": { | |
230 | "text/html": [ |
|
224 | "text/html": [ | |
231 |
"<progress style='width: |
|
225 | "<progress style='width:60ex' max='10' value='10'></progress>" | |
232 | ], |
|
226 | ], | |
233 | "text/plain": [ |
|
227 | "text/plain": [ | |
234 | "<IPython.core.display.ProgressBar object>" |
|
228 | "[============================================================] 10/10" | |
235 | ] |
|
229 | ] | |
236 | }, |
|
230 | }, | |
237 | "metadata": {}, |
|
231 | "metadata": {}, | |
@@ -254,16 +248,16 b'' | |||||
254 | }, |
|
248 | }, | |
255 | { |
|
249 | { | |
256 | "cell_type": "code", |
|
250 | "cell_type": "code", | |
257 |
"execution_count": 1 |
|
251 | "execution_count": 10, | |
258 | "metadata": {}, |
|
252 | "metadata": {}, | |
259 | "outputs": [ |
|
253 | "outputs": [ | |
260 | { |
|
254 | { | |
261 | "data": { |
|
255 | "data": { | |
262 | "text/html": [ |
|
256 | "text/html": [ | |
263 |
"<progress style='width: |
|
257 | "<progress style='width:60ex' max='10' value='10'></progress>" | |
264 | ], |
|
258 | ], | |
265 | "text/plain": [ |
|
259 | "text/plain": [ | |
266 | "<IPython.core.display.ProgressBar object>" |
|
260 | "[============================================================] 10/10" | |
267 | ] |
|
261 | ] | |
268 | }, |
|
262 | }, | |
269 | "metadata": {}, |
|
263 | "metadata": {}, | |
@@ -285,6 +279,36 b'' | |||||
285 | "cell_type": "markdown", |
|
279 | "cell_type": "markdown", | |
286 | "metadata": {}, |
|
280 | "metadata": {}, | |
287 | "source": [ |
|
281 | "source": [ | |
|
282 | "The ProgressBar also has an update built into iteration:" | |||
|
283 | ] | |||
|
284 | }, | |||
|
285 | { | |||
|
286 | "cell_type": "code", | |||
|
287 | "execution_count": 11, | |||
|
288 | "metadata": {}, | |||
|
289 | "outputs": [ | |||
|
290 | { | |||
|
291 | "data": { | |||
|
292 | "text/html": [ | |||
|
293 | "<progress style='width:60ex' max='10' value='10'></progress>" | |||
|
294 | ], | |||
|
295 | "text/plain": [ | |||
|
296 | "[============================================================] 10/10" | |||
|
297 | ] | |||
|
298 | }, | |||
|
299 | "metadata": {}, | |||
|
300 | "output_type": "display_data" | |||
|
301 | } | |||
|
302 | ], | |||
|
303 | "source": [ | |||
|
304 | "for i in ProgressBar(10):\n", | |||
|
305 | " time.sleep(0.25)" | |||
|
306 | ] | |||
|
307 | }, | |||
|
308 | { | |||
|
309 | "cell_type": "markdown", | |||
|
310 | "metadata": {}, | |||
|
311 | "source": [ | |||
288 | "We would encourage any updatable-display objects that track their own display_ids to follow-suit with `.display()` and `.update()` or `.update_display()` methods." |
|
312 | "We would encourage any updatable-display objects that track their own display_ids to follow-suit with `.display()` and `.update()` or `.update_display()` methods." | |
289 | ] |
|
313 | ] | |
290 | } |
|
314 | } |
General Comments 0
You need to be logged in to leave comments.
Login now