Skip to content

Instantly share code, notes, and snippets.

@minrk
Created March 27, 2012 00:33
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save minrk/2211026 to your computer and use it in GitHub Desktop.
Save minrk/2211026 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "progbar"
},
"nbformat": 3,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": true,
"input": [
"import sys, time",
"try:",
" from IPython.core.display import clear_output",
" have_ipython = True",
"except ImportError:",
" have_ipython = False",
"",
"class ProgressBar:",
" def __init__(self, iterations):",
" self.iterations = iterations",
" self.prog_bar = '[]'",
" self.fill_char = '*'",
" self.width = 40",
" self.__update_amount(0)",
" if have_ipython:",
" self.animate = self.animate_ipython",
" else:",
" self.animate = self.animate_noipython",
"",
" def animate_ipython(self, iter):",
" try:",
" clear_output()",
" except Exception:",
" # terminal IPython has no clear_output",
" pass",
" print '\\r', self,",
" sys.stdout.flush()",
" self.update_iteration(iter + 1)",
"",
" def update_iteration(self, elapsed_iter):",
" self.__update_amount((elapsed_iter / float(self.iterations)) * 100.0)",
" self.prog_bar += ' %d of %s complete' % (elapsed_iter, self.iterations)",
"",
" def __update_amount(self, new_amount):",
" percent_done = int(round((new_amount / 100.0) * 100.0))",
" all_full = self.width - 2",
" num_hashes = int(round((percent_done / 100.0) * all_full))",
" self.prog_bar = '[' + self.fill_char * num_hashes + ' ' * (all_full - num_hashes) + ']'",
" pct_place = (len(self.prog_bar) / 2) - len(str(percent_done))",
" pct_string = '%d%%' % percent_done",
" self.prog_bar = self.prog_bar[0:pct_place] + \\",
" (pct_string + self.prog_bar[pct_place + len(pct_string):])",
"",
" def __str__(self):",
" return str(self.prog_bar)"
],
"language": "python",
"outputs": [],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"p = ProgressBar(1000)",
"for i in range(1001):",
" p.animate(i)",
"# time.sleep(0.01)"
],
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
" ",
"[****************100%******************] 1000 of 1000 complete"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
""
]
}
],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": true,
"input": [],
"language": "python",
"outputs": []
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment