Skip to content

Commit 9ef5018

Browse files
committed
javascript progress bar
1 parent 8302257 commit 9ef5018

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

pymc/progressbar.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
from __future__ import print_function
77

88
import sys, time
9+
import uuid
910
try:
10-
from IPython.core.display import clear_output
11+
from IPython.core.display import HTML, Javascript, display
12+
1113
have_ipython = True
1214
except ImportError:
1315
have_ipython = False
@@ -19,8 +21,22 @@ def __init__(self, iterations):
1921
self.fill_char = '*'
2022
self.width = 40
2123
self.__update_amount(0)
24+
25+
self.start = time.time()
26+
self.last = 0
2227
if have_ipython:
2328
self.animate = self.animate_ipython
29+
self.divid = str(uuid.uuid4())
30+
self.sec_id = str(uuid.uuid4())
31+
32+
pb = HTML(
33+
"""
34+
<div style="float: left; border: 1px solid black; width:500px">
35+
<div id="%s" style="background-color:blue; width:0%%">&nbsp;</div>
36+
</div>
37+
<label id="%s" style="padding-left: 10px;" text = ""/>
38+
""" % (self.divid,self.sec_id))
39+
display(pb)
2440
else:
2541
self.animate = self.animate_noipython
2642

@@ -30,22 +46,24 @@ def animate_noipython(self, iter):
3046
else:
3147
print(self)
3248
self.update_iteration(iter)
33-
# time.sleep(0.5)
3449

3550
def animate_ipython(self, iter):
36-
try:
37-
clear_output()
38-
except Exception:
39-
# terminal IPython has no clear_output
40-
pass
41-
print('\r', self, end='')
42-
sys.stdout.flush()
43-
self.update_iteration(iter)
51+
elapsed = time.time() - self.start
52+
iter = iter + 1
53+
if elapsed - self.last > .5 or iter == self.iterations:
54+
self.last = elapsed
55+
56+
self.update_iteration(iter)
57+
fraction = int(100*iter/float(self.iterations))
58+
59+
display(Javascript("$('div#%s').width('%i%%')" % (self.divid, fraction)))
60+
display(Javascript("$('label#%s').text('%i%% in %.1f sec')" % (self.sec_id, fraction, round(elapsed, 1))))
4461

4562
def update_iteration(self, elapsed_iter):
4663
self.__update_amount((elapsed_iter / float(self.iterations)) * 100.0)
4764
self.prog_bar += ' %d of %s complete' % (elapsed_iter, self.iterations)
4865

66+
4967
def __update_amount(self, new_amount):
5068
percent_done = int(round((new_amount / 100.0) * 100.0))
5169
all_full = self.width - 2

0 commit comments

Comments
 (0)