15
12

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.

tqdmの表示内容をカスタマイズしたい

Posted at

なにをするか

  • tqdmの表示内容を変えてみる

環境

  • Python 3.7.0
  • tqdm 4.32.1
  • GNU bash, バージョン 4.3.48(1)-release (x86_64-pc-linux-gnu)

通常

いつもの

基本
from tqdm import tqdm
from time import sleep

for i in tqdm(range(10)):
    sleep(0.1)
アウトプット
100%|██████████████████████████████████| 10/10 [00:01<00:00,  9.96it/s]

forの中でprintすると大変

printあり
for i in tqdm(range(10)):
    print(i)
    sleep(0.1)
アウトプット
  0%|                                           | 0/10 [00:00<?, ?it/s]
0
 10%|███▌                               | 1/10 [00:00<00:00,  9.98it/s]
1
 20%|███████                            | 2/10 [00:00<00:00,  9.98it/s]
2
 30%|██████████▌                        | 3/10 [00:00<00:00,  9.97it/s]
3
 40%|██████████████                     | 4/10 [00:00<00:00,  9.97it/s]
4
 50%|█████████████████▌                 | 5/10 [00:00<00:00,  9.97it/s]
5
 60%|█████████████████████              | 6/10 [00:00<00:00,  9.97it/s]
6
 70%|████████████████████████▌          | 7/10 [00:00<00:00,  9.97it/s]
7
 80%|████████████████████████████       | 8/10 [00:00<00:00,  9.97it/s]
8
 90%|███████████████████████████████▌   | 9/10 [00:00<00:00,  9.97it/s]
9
100%|██████████████████████████████████| 10/10 [00:01<00:00,  9.97it/s]

工夫する

postfixの中に表示してしまえばいい

tqdmのpostfixに含める
with tqdm(range(10)) as t:
    for i in t:
        sleep(0.1)
        t.postfix = str(i)
        t.update()
アウトプット
100%|███████████████████████████████| 10/10 [00:01<00:00,  9.97it/s, 9]

postfixの別の書き方

別の書き方
with tqdm(range(10)) as t:
    for i in t:
        sleep(0.1)
        t.set_postfix(i=i)
        t.update()
アウトプット
100%|█████████████████████████████| 10/10 [00:01<00:00,  9.94it/s, i=9]

説明をつける

かっこいい?
with tqdm(range(10), desc="RANGE(10)") as t:
    for i in t:
        sleep(0.1)
        t.set_postfix(i=i)
        t.update()
アウトプット
RANGE(10): 100%|██████████████████| 10/10 [00:01<00:00,  9.95it/s, i=9]

postfixの位置を変えたい

postfixを変えたい
bar_format = "{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}{postfix}{rate_fmt}]"
with tqdm(range(10), desc="RANGE(10)", bar_format=bar_format) as t:
    for i in t:
        sleep(0.1)
        t.set_postfix(i=i)
        t.update()
アウトプット
RANGE(10): 100%|████████████████████| 10/10 [00:01<00:00, i=9 9.95it/s]

プログレスバーを消したい

プログレスバーを消す
bar_format = "{l_bar}{r_bar}"
with tqdm(range(10), desc="RANGE(10)", bar_format=bar_format) as t:
    for i in t:
        sleep(0.1)
        t.set_postfix(i=i)
        t.update()
アウトプット
RANGE(10): 100%|| 10/10 [00:01<00:00,  9.95it/s, i=9]

bar_formatの全体(メモのため)

{l_bar}{bar}{r_bar}と同じ
{desc}: {percentage:3.0f}%|{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}{rate_fmt}{postfix}]
その他variables
n, total, elapsed_s, ncols, desc, unit, rate, rate_fmt, rate_noinv, rate_noinv_fmt, rate_inv, rate_inv_fmt, unit_divisor, remaining_s

実はshにも組み込める

tqdm
$ seq 10 | tqdm | wc -l
100it [00:00, 81284.96it/s]
100

もっと知りたい

15
12
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
15
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?