Skip to content

Commit

Permalink
Merge pull request #2112 from HarshKhilawala/move-html-progressbar
Browse files Browse the repository at this point in the history
Move HTMLProgressBar from qutip.ipynbtools to qutip.ui.progressbar.
  • Loading branch information
hodgestar committed Mar 9, 2023
2 parents c499d44 + 8de851f commit 40ff8e1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 57 deletions.
1 change: 1 addition & 0 deletions doc/changes/2112.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Moved HTMLProgressBar from qutip/ipynbtools.py to qutip/ui/progressbar.py
61 changes: 5 additions & 56 deletions qutip/ipynbtools.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
This module contains utility functions for using QuTiP with IPython notebooks.
"""
from qutip.ui.progressbar import BaseProgressBar
from qutip.ui.progressbar import BaseProgressBar, HTMLProgressBar
from .settings import _blas_info, available_cpu_count
import IPython

Expand All @@ -11,16 +11,16 @@
try:
from ipyparallel import Client
__all__ = ['version_table', 'plot_animation',
'parallel_map', 'HTMLProgressBar']
'parallel_map']
except:
__all__ = ['version_table', 'plot_animation', 'HTMLProgressBar']
__all__ = ['version_table', 'plot_animation']
else:
try:
from IPython.parallel import Client
__all__ = ['version_table', 'plot_animation',
'parallel_map', 'HTMLProgressBar']
'parallel_map']
except:
__all__ = ['version_table', 'plot_animation', 'HTMLProgressBar']
__all__ = ['version_table', 'plot_animation']


from IPython.display import HTML, Javascript, display
Expand Down Expand Up @@ -97,57 +97,6 @@ def version_table(verbose=False):
return HTML(html)


class HTMLProgressBar(BaseProgressBar):
"""
A simple HTML progress bar for using in IPython notebooks. Based on
IPython ProgressBar demo notebook:
https://github.com/ipython/ipython/tree/master/examples/notebooks
Example usage:
n_vec = linspace(0, 10, 100)
pbar = HTMLProgressBar(len(n_vec))
for n in n_vec:
pbar.update(n)
compute_with_n(n)
"""

def __init__(self, iterations=0, chunk_size=1.0):
self.divid = str(uuid.uuid4())
self.textid = str(uuid.uuid4())
self.pb = HTML("""\
<div style="border: 2px solid grey; width: 600px">
<div id="%s" \
style="background-color: rgba(121,195,106,0.75); width:0%%">&nbsp;</div>
</div>
<p id="%s"></p>
""" % (self.divid, self.textid))
display(self.pb)
super(HTMLProgressBar, self).start(iterations, chunk_size)

def start(self, iterations=0, chunk_size=1.0):
super(HTMLProgressBar, self).start(iterations, chunk_size)

def update(self, n):
p = (n / self.N) * 100.0
if p >= self.p_chunk:
lbl = ("Elapsed time: %s. " % self.time_elapsed() +
"Est. remaining time: %s." % self.time_remaining_est(p))
js_code = ("$('div#%s').width('%i%%');" % (self.divid, p) +
"$('p#%s').text('%s');" % (self.textid, lbl))
display(Javascript(js_code))
# display(Javascript("$('div#%s').width('%i%%')" % (self.divid,
# p)))
self.p_chunk += self.p_chunk_size

def finished(self):
self.t_done = time.time()
lbl = "Elapsed time: %s" % self.time_elapsed()
js_code = ("$('div#%s').width('%i%%');" % (self.divid, 100.0) +
"$('p#%s').text('%s');" % (self.textid, lbl))
display(Javascript(js_code))


def _visualize_parfor_data(metadata):
"""
Visualizing the task scheduling meta data collected from AsyncResults.
Expand Down
55 changes: 54 additions & 1 deletion qutip/ui/progressbar.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__all__ = ['BaseProgressBar', 'TextProgressBar',
'EnhancedTextProgressBar', 'TqdmProgressBar',
'progess_bars']
'HTMLProgressBar', 'progess_bars']

import time
import datetime
Expand Down Expand Up @@ -145,6 +145,57 @@ def finished(self):
self.t_done = time.time()


class HTMLProgressBar(BaseProgressBar):
"""
A simple HTML progress bar for using in IPython notebooks. Based on
IPython ProgressBar demo notebook:
https://github.com/ipython/ipython/tree/master/examples/notebooks
Example usage:
n_vec = linspace(0, 10, 100)
pbar = HTMLProgressBar(len(n_vec))
for n in n_vec:
pbar.update(n)
compute_with_n(n)
"""

def __init__(self, iterations=0, chunk_size=1.0):
self.divid = str(uuid.uuid4())
self.textid = str(uuid.uuid4())
self.pb = HTML("""\
<div style="border: 2px solid grey; width: 600px">
<div id="%s" \
style="background-color: rgba(121,195,106,0.75); width:0%%">&nbsp;</div>
</div>
<p id="%s"></p>
""" % (self.divid, self.textid))
display(self.pb)
super(HTMLProgressBar, self).start(iterations, chunk_size)

def start(self, iterations=0, chunk_size=1.0):
super(HTMLProgressBar, self).start(iterations, chunk_size)

def update(self, n):
p = (n / self.N) * 100.0
if p >= self.p_chunk:
lbl = ("Elapsed time: %s. " % self.time_elapsed() +
"Est. remaining time: %s." % self.time_remaining_est(p))
js_code = ("$('div#%s').width('%i%%');" % (self.divid, p) +
"$('p#%s').text('%s');" % (self.textid, lbl))
display(Javascript(js_code))
# display(Javascript("$('div#%s').width('%i%%')" % (self.divid,
# p)))
self.p_chunk += self.p_chunk_size

def finished(self):
self.t_done = time.time()
lbl = "Elapsed time: %s" % self.time_elapsed()
js_code = ("$('div#%s').width('%i%%');" % (self.divid, 100.0) +
"$('p#%s').text('%s');" % (self.textid, lbl))
display(Javascript(js_code))


progess_bars = {
"Enhanced": EnhancedTextProgressBar,
"enhanced": EnhancedTextProgressBar,
Expand All @@ -153,6 +204,8 @@ def finished(self):
True: TextProgressBar,
"Tqdm": TqdmProgressBar,
"tqdm": TqdmProgressBar,
"Html": HTMLProgressBar,
"html": HTMLProgressBar,
"": BaseProgressBar,
False: BaseProgressBar,
None: BaseProgressBar,
Expand Down

0 comments on commit 40ff8e1

Please sign in to comment.