2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Colaboratory で Progress bar を表示

Last updated at Posted at 2019-03-11

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)
2
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?