6
6
from __future__ import print_function
7
7
8
8
import sys , time
9
+ import uuid
9
10
try :
10
- from IPython .core .display import clear_output
11
+ from IPython .core .display import HTML , Javascript , display
12
+
11
13
have_ipython = True
12
14
except ImportError :
13
15
have_ipython = False
@@ -19,8 +21,22 @@ def __init__(self, iterations):
19
21
self .fill_char = '*'
20
22
self .width = 40
21
23
self .__update_amount (0 )
24
+
25
+ self .start = time .time ()
26
+ self .last = 0
22
27
if have_ipython :
23
28
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%%"> </div>
36
+ </div>
37
+ <label id="%s" style="padding-left: 10px;" text = ""/>
38
+ """ % (self .divid ,self .sec_id ))
39
+ display (pb )
24
40
else :
25
41
self .animate = self .animate_noipython
26
42
@@ -30,22 +46,24 @@ def animate_noipython(self, iter):
30
46
else :
31
47
print (self )
32
48
self .update_iteration (iter )
33
- # time.sleep(0.5)
34
49
35
50
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 ))))
44
61
45
62
def update_iteration (self , elapsed_iter ):
46
63
self .__update_amount ((elapsed_iter / float (self .iterations )) * 100.0 )
47
64
self .prog_bar += ' %d of %s complete' % (elapsed_iter , self .iterations )
48
65
66
+
49
67
def __update_amount (self , new_amount ):
50
68
percent_done = int (round ((new_amount / 100.0 ) * 100.0 ))
51
69
all_full = self .width - 2
0 commit comments