Skip to content

Python で tqdm を使ってプログレスバーを表示する

tqdm を使うと Python のコードで CLI 上からプログレスバーを表示することが出来ます。 今回は tqdm の使い方をメモしておきます。

検証環境

以下の環境で検証しました。

  • macOS Sonoma 14.2
  • Python 3.9.6
  • tqdm 4.66.1

インストール

tqdm を pip でインストールします。

1
python3 -m pip install tqdm

基本的な使い方

基本的なサンプルコードは以下の通りです。

1
2
3
4
5
6
7
8
#!/usr/bin/env python3

import time

from tqdm import tqdm

for i in tqdm(range(10)):
    time.sleep(1)

実行例は以下です。

file

update() でアップデートする

tqdm を初期化した後、update() でプログレスバーを進めることも出来ます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#!/usr/bin/env python3

import time

from tqdm import tqdm

progress = tqdm(total=100)

for i in range(100):
    time.sleep(0.1)
    progress.update(1)

実行例は以下です。

file