Show More
@@ -740,12 +740,24 b' class ProgressBar(DisplayObject):' | |||||
740 | total : int |
|
740 | total : int | |
741 | maximum size of the progressbar |
|
741 | maximum size of the progressbar | |
742 | """ |
|
742 | """ | |
743 | self._display_id = hexlify(os.urandom(8)).decode('ascii') |
|
|||
744 | self.total = total |
|
743 | self.total = total | |
745 | self._progress = 0 |
|
744 | self._progress = 0 | |
|
745 | self.html_width = '60ex' | |||
|
746 | self.text_width = 60 | |||
|
747 | self._display_id = hexlify(os.urandom(8)).decode('ascii') | |||
|
748 | ||||
|
749 | def __repr__(self): | |||
|
750 | fraction = self.progress / self.total | |||
|
751 | filled = '=' * int(fraction * self.text_width) | |||
|
752 | rest = ' ' * (self.text_width - len(filled)) | |||
|
753 | return '[{}{}] {}/{}'.format( | |||
|
754 | filled, rest, | |||
|
755 | self.progress, self.total, | |||
|
756 | ) | |||
746 |
|
757 | |||
747 | def _repr_html_(self): |
|
758 | def _repr_html_(self): | |
748 |
return "<progress style='width: |
|
759 | return "<progress style='width:{}' max='{}' value='{}'></progress>".format( | |
|
760 | self.html_width, self.total, self.progress) | |||
749 |
|
761 | |||
750 | def display(self): |
|
762 | def display(self): | |
751 | display(self, display_id=self._display_id) |
|
763 | display(self, display_id=self._display_id) |
@@ -196,8 +196,7 b' def test_displayobject_repr():' | |||||
196 |
|
196 | |||
197 | def test_progress(): |
|
197 | def test_progress(): | |
198 | p = display.ProgressBar(10) |
|
198 | p = display.ProgressBar(10) | |
199 | nt.assert_equal(repr(p), '<IPython.core.display.ProgressBar object>') |
|
199 | nt.assert_true('0/10' in repr(p)) | |
200 | p._show_mem_addr = True |
|
|||
201 | nt.assert_equal(p._repr_html_(), "<progress style='width:100%' max='10' value='0'></progress>") |
|
200 | nt.assert_equal(p._repr_html_(), "<progress style='width:100%' max='10' value='0'></progress>") | |
202 |
|
201 | |||
203 | def test_json(): |
|
202 | def test_json(): |
@@ -222,21 +222,16 b'' | |||||
222 | }, |
|
222 | }, | |
223 | { |
|
223 | { | |
224 | "cell_type": "code", |
|
224 | "cell_type": "code", | |
225 |
"execution_count": |
|
225 | "execution_count": 14, | |
226 | "metadata": {}, |
|
226 | "metadata": {}, | |
227 | "outputs": [ |
|
227 | "outputs": [ | |
228 | { |
|
228 | { | |
229 | "data": { |
|
229 | "data": { | |
230 | "text/html": [ |
|
230 | "text/html": [ | |
231 | "<progress\n", |
|
231 | "<progress style='width:100%' max='10' value='10'></progress>" | |
232 | " value=10\n", |
|
|||
233 | " max=10\n", |
|
|||
234 | " style=\"width: 60ex\"/>\n", |
|
|||
235 | " 10 / 10\n", |
|
|||
236 | " " |
|
|||
237 | ], |
|
232 | ], | |
238 | "text/plain": [ |
|
233 | "text/plain": [ | |
239 | "[============================================================] 10/10" |
|
234 | "<IPython.core.display.ProgressBar object>" | |
240 | ] |
|
235 | ] | |
241 | }, |
|
236 | }, | |
242 | "metadata": {}, |
|
237 | "metadata": {}, | |
@@ -244,43 +239,7 b'' | |||||
244 | } |
|
239 | } | |
245 | ], |
|
240 | ], | |
246 | "source": [ |
|
241 | "source": [ | |
247 | "import os\n", |
|
242 | "from IPython.display import ProgressBar\n", | |
248 | "from binascii import hexlify\n", |
|
|||
249 | "\n", |
|
|||
250 | "class ProgressBar(object):\n", |
|
|||
251 | " def __init__(self, capacity):\n", |
|
|||
252 | " self.progress = 0\n", |
|
|||
253 | " self.capacity = capacity\n", |
|
|||
254 | " self.html_width = '60ex'\n", |
|
|||
255 | " self.text_width = 60\n", |
|
|||
256 | " self._display_id = hexlify(os.urandom(8)).decode('ascii')\n", |
|
|||
257 | " \n", |
|
|||
258 | " def __repr__(self):\n", |
|
|||
259 | " fraction = self.progress / self.capacity\n", |
|
|||
260 | " filled = '=' * int(fraction * self.text_width)\n", |
|
|||
261 | " rest = ' ' * (self.text_width - len(filled))\n", |
|
|||
262 | " return '[{}{}] {}/{}'.format(\n", |
|
|||
263 | " filled, rest,\n", |
|
|||
264 | " self.progress, self.capacity,\n", |
|
|||
265 | " )\n", |
|
|||
266 | " \n", |
|
|||
267 | " def _repr_html_(self):\n", |
|
|||
268 | " return \"\"\"<progress\n", |
|
|||
269 | " value={progress}\n", |
|
|||
270 | " max={capacity}\n", |
|
|||
271 | " style=\"width: {width}\"/>\n", |
|
|||
272 | " {progress} / {capacity}\n", |
|
|||
273 | " \"\"\".format(\n", |
|
|||
274 | " progress=self.progress,\n", |
|
|||
275 | " capacity=self.capacity,\n", |
|
|||
276 | " width=self.html_width,\n", |
|
|||
277 | " )\n", |
|
|||
278 | " \n", |
|
|||
279 | " def display(self):\n", |
|
|||
280 | " display(self, display_id=self._display_id)\n", |
|
|||
281 | " \n", |
|
|||
282 | " def update(self):\n", |
|
|||
283 | " update_display(self, display_id=self._display_id)\n", |
|
|||
284 | "\n", |
|
243 | "\n", | |
285 | "bar = ProgressBar(10)\n", |
|
244 | "bar = ProgressBar(10)\n", | |
286 | "bar.display()" |
|
245 | "bar.display()" | |
@@ -295,21 +254,16 b'' | |||||
295 | }, |
|
254 | }, | |
296 | { |
|
255 | { | |
297 | "cell_type": "code", |
|
256 | "cell_type": "code", | |
298 |
"execution_count": |
|
257 | "execution_count": 15, | |
299 | "metadata": {}, |
|
258 | "metadata": {}, | |
300 | "outputs": [ |
|
259 | "outputs": [ | |
301 | { |
|
260 | { | |
302 | "data": { |
|
261 | "data": { | |
303 | "text/html": [ |
|
262 | "text/html": [ | |
304 | "<progress\n", |
|
263 | "<progress style='width:100%' max='10' value='10'></progress>" | |
305 | " value=10\n", |
|
|||
306 | " max=10\n", |
|
|||
307 | " style=\"width: 60ex\"/>\n", |
|
|||
308 | " 10 / 10\n", |
|
|||
309 | " " |
|
|||
310 | ], |
|
264 | ], | |
311 | "text/plain": [ |
|
265 | "text/plain": [ | |
312 | "[============================================================] 10/10" |
|
266 | "<IPython.core.display.ProgressBar object>" | |
313 | ] |
|
267 | ] | |
314 | }, |
|
268 | }, | |
315 | "metadata": {}, |
|
269 | "metadata": {}, | |
@@ -351,7 +305,7 b'' | |||||
351 | "name": "python", |
|
305 | "name": "python", | |
352 | "nbconvert_exporter": "python", |
|
306 | "nbconvert_exporter": "python", | |
353 | "pygments_lexer": "ipython3", |
|
307 | "pygments_lexer": "ipython3", | |
354 |
"version": "3. |
|
308 | "version": "3.6.2" | |
355 | } |
|
309 | } | |
356 | }, |
|
310 | }, | |
357 | "nbformat": 4, |
|
311 | "nbformat": 4, |
General Comments 0
You need to be logged in to leave comments.
Login now