Google Colaboratory でプログレスバー を表示したい.
参考元: How do I use updatable displays on colab?
参考元: Demo Program
使いやすく書き換えたもの
ProgressBar.ipyrb
class progressbar:
def __init__(self, data):
if type(data) is not int:
self._datas = data
else:
self._datas = range(data)
self._length = len(self._datas)
self._count = 0
self._image = display(self.__bar(), display_id=True)
def show(self):
from IPython.display import display
self._image.update(self.__bar())
return self._count / self._length
def step(self, count=1):
self._count += count
self.show()
def __next__(self):
if self._count < self._length:
r = self._datas[self._count]
else:
raise StopIteration
self.step(1)
return r
def __iter__(self):
return self
def __bar(self):
from IPython.display import HTML
return HTML("""
<progress
value='{value}'
max='{length}',
style='width: 100%'
>
{value}
</progress>
""".format(value=self._count, length=self._length))
使い方 その1
sample01.ipyrb
import time
loop_size = 1000
pgb = progressbar(loop_size)
for i in range(loop_size):
print(i, end=' ')
time.sleep(0.01)
pgb.step()
使い方 その2
sample02.ipyrb
import time
loop_size = 1000
for i in progressbar(range(loop_size)):
print(i, end=' ')
time.sleep(0.01)