LoginSignup
6
4

More than 3 years have passed since last update.

Githubおもしろリポジトリ③ ~ループ中も飽きない?新しいプログレスバー~

Last updated at Posted at 2021-01-10

このシリーズについて

githubのトレンドリポジトリから情報を定期通知する機能を作ったのですが、それを通じて発見した面白そうなリポジトリを紹介するシリーズです。目次はこちらです。

alive-progressとは

今回はalive-progressという面白いプログレスバーを紹介します。pythonのプログレスバーといえばtqdmを使う方が多いと思うのですが、ちょっと遊び心をいれたプログレスバーを使いたいという方のために作られたのがalive-progressです。下のような感じになります。
progress.gif

使い方

まずはalive-progressをインストールします。

pip install alive-progress

上のGIFのような動作にしたい場合は、下のコードで出来ます。

from alive_progress import alive_bar, config_handler
import time
from tqdm import tqdm

def main():
    for x in 5000, 6000:
        with alive_bar(x, bar='checks', spinner='notes') as bar:
            for i in range(5000):
                time.sleep(.001)
                bar()
    # show_bars()
    # show_spinners()


if __name__ == '__main__':
    main()

with文の第一引数としてループを回す数を渡してあげれば大丈夫です。あとは、barの種類をbar=''のところに、barの右側の表示をspinner=''で選んであげればGIFのようになります。barにチェックマーク、spinnerに音符を選んでいます。そしてfor文の最後にbar()を入れてあげれば完成です。
barとspinnersのバリュエーションは、それぞれshow_bars(),show_spinners()で表示されます。
barの一覧
bars.gif
spinnerの一覧
spinners.gif

tqdmとの比較

下のコードで比較してみました。

from alive_progress import alive_bar, config_handler
import time
from tqdm import tqdm

def main():
    print("~~Using tqdm~~")
    for i in 1000, 2000:
        for x in tqdm(range(1000)):
            time.sleep(.001)

    print("~~Using active-progress")
    for x in 1000, 2000:
        with alive_bar(x, bar='checks', spinner='notes') as bar:
            for i in range(1000):
                time.sleep(.001)
                bar()

結果は下のような感じになりました。
tqdm.gif
渡された値がループしきれていないときに(今回は2000)、active-progressは(!)のマークで知らせていますね。シンプルさはtqdmが良いですが、自分はactive-progress好きですね(笑)。

まとめ

ループ回すコードを書くのが楽しみになりました(笑)。

間違いや質問、ご意見等ありましたらお気軽にコメントください。頑張って答えますので(笑)。

6
4
0

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
6
4