##// END OF EJS Templates
Update example notebook for new progressbar.
Marius van Niekerk -
Show More
@@ -740,12 +740,24 b' class ProgressBar(DisplayObject):'
740 740 total : int
741 741 maximum size of the progressbar
742 742 """
743 self._display_id = hexlify(os.urandom(8)).decode('ascii')
744 743 self.total = total
745 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 758 def _repr_html_(self):
748 return "<progress style='width:100%' max='{}' value='{}'></progress>".format(self.total, self.progress)
759 return "<progress style='width:{}' max='{}' value='{}'></progress>".format(
760 self.html_width, self.total, self.progress)
749 761
750 762 def display(self):
751 763 display(self, display_id=self._display_id)
@@ -196,8 +196,7 b' def test_displayobject_repr():'
196 196
197 197 def test_progress():
198 198 p = display.ProgressBar(10)
199 nt.assert_equal(repr(p), '<IPython.core.display.ProgressBar object>')
200 p._show_mem_addr = True
199 nt.assert_true('0/10' in repr(p))
201 200 nt.assert_equal(p._repr_html_(), "<progress style='width:100%' max='10' value='0'></progress>")
202 201
203 202 def test_json():
@@ -222,21 +222,16 b''
222 222 },
223 223 {
224 224 "cell_type": "code",
225 "execution_count": 35,
225 "execution_count": 14,
226 226 "metadata": {},
227 227 "outputs": [
228 228 {
229 229 "data": {
230 230 "text/html": [
231 "<progress\n",
232 " value=10\n",
233 " max=10\n",
234 " style=\"width: 60ex\"/>\n",
235 " 10 / 10\n",
236 " "
231 "<progress style='width:100%' max='10' value='10'></progress>"
237 232 ],
238 233 "text/plain": [
239 "[============================================================] 10/10"
234 "<IPython.core.display.ProgressBar object>"
240 235 ]
241 236 },
242 237 "metadata": {},
@@ -244,43 +239,7 b''
244 239 }
245 240 ],
246 241 "source": [
247 "import os\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",
242 "from IPython.display import ProgressBar\n",
284 243 "\n",
285 244 "bar = ProgressBar(10)\n",
286 245 "bar.display()"
@@ -295,21 +254,16 b''
295 254 },
296 255 {
297 256 "cell_type": "code",
298 "execution_count": 36,
257 "execution_count": 15,
299 258 "metadata": {},
300 259 "outputs": [
301 260 {
302 261 "data": {
303 262 "text/html": [
304 "<progress\n",
305 " value=10\n",
306 " max=10\n",
307 " style=\"width: 60ex\"/>\n",
308 " 10 / 10\n",
309 " "
263 "<progress style='width:100%' max='10' value='10'></progress>"
310 264 ],
311 265 "text/plain": [
312 "[============================================================] 10/10"
266 "<IPython.core.display.ProgressBar object>"
313 267 ]
314 268 },
315 269 "metadata": {},
@@ -351,7 +305,7 b''
351 305 "name": "python",
352 306 "nbconvert_exporter": "python",
353 307 "pygments_lexer": "ipython3",
354 "version": "3.5.1"
308 "version": "3.6.2"
355 309 }
356 310 },
357 311 "nbformat": 4,
General Comments 0
You need to be logged in to leave comments. Login now