##// END OF EJS Templates
Fixed flushed messages not getting sent with new 'method' message tag
Fixed flushed messages not getting sent with new 'method' message tag

File last commit:

r9198:b62fdeca
r14451:8995edf8
Show More
Progress Bars.ipynb
146 lines | 4.2 KiB | text/plain | TextLexer
Brian Granger
More changes to example notebooks.
r9193 {
"metadata": {
"name": "Progress Bars"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Two Examples of Progress Bars"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"A Javascript Progress Bar"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is a simple progress bar using HTML/Javascript:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import uuid\n",
"import time\n",
"from IPython.display import HTML, Javascript, display\n",
"\n",
"divid = str(uuid.uuid4())\n",
"\n",
"pb = HTML(\n",
"\"\"\"\n",
"<div style=\"border: 1px solid black; width:500px\">\n",
" <div id=\"%s\" style=\"background-color:blue; width:0%%\">&nbsp;</div>\n",
"</div> \n",
"\"\"\" % divid)\n",
"display(pb)\n",
"for i in range(1,101):\n",
" time.sleep(0.1)\n",
" \n",
" display(Javascript(\"$('div#%s').width('%i%%')\" % (divid, i)))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The above simply makes a div that is a box, and a blue div inside it with a unique ID \n",
"(so that the javascript won't collide with other similar progress bars on the same page). \n",
"\n",
"Then, at every progress point, we run a simple jQuery call to resize the blue box to\n",
"the appropriate fraction of the width of its containing box, and voil\u00e0 a nice\n",
"HTML/Javascript progress bar!"
]
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"ProgressBar class"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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"
]
},
{
"cell_type": "code",
"collapsed": true,
"input": [
Thomas Kluyver
More changes to example notebooks for Python 3 compatibility
r9198 "from __future__ import print_function\n",
Brian Granger
More changes to example notebooks.
r9193 "import sys, time\n",
"\n",
"class ProgressBar:\n",
" def __init__(self, iterations):\n",
" self.iterations = iterations\n",
" self.prog_bar = '[]'\n",
" self.fill_char = '*'\n",
" self.width = 50\n",
" self.__update_amount(0)\n",
"\n",
" def animate(self, iter):\n",
Thomas Kluyver
More changes to example notebooks for Python 3 compatibility
r9198 " print('\\r', self, end='')\n",
Brian Granger
More changes to example notebooks.
r9193 " sys.stdout.flush()\n",
" self.update_iteration(iter + 1)\n",
"\n",
" def update_iteration(self, elapsed_iter):\n",
" self.__update_amount((elapsed_iter / float(self.iterations)) * 100.0)\n",
" self.prog_bar += ' %d of %s complete' % (elapsed_iter, self.iterations)\n",
"\n",
" def __update_amount(self, new_amount):\n",
" percent_done = int(round((new_amount / 100.0) * 100.0))\n",
" all_full = self.width - 2\n",
" num_hashes = int(round((percent_done / 100.0) * all_full))\n",
" self.prog_bar = '[' + self.fill_char * num_hashes + ' ' * (all_full - num_hashes) + ']'\n",
" pct_place = (len(self.prog_bar) // 2) - len(str(percent_done))\n",
" pct_string = '%d%%' % percent_done\n",
" self.prog_bar = self.prog_bar[0:pct_place] + \\\n",
" (pct_string + self.prog_bar[pct_place + len(pct_string):])\n",
"\n",
" def __str__(self):\n",
" return str(self.prog_bar)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"p = ProgressBar(1000)\n",
"for i in range(1001):\n",
" time.sleep(0.002)\n",
" p.animate(i)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
}
],
"metadata": {}
}
]
}