0
0

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 1 year has passed since last update.

progressbarとlogging

Posted at

狙い

tqdmではhandlerで設定したログのlevelがターミナルの出力に反映されない問題が解決されていないようで,richパッケージを使った方が楽なことが分かった。

tqdmのダメなところ

import logging
from tqdm.contrib.logging import logging_redirect_tqdm
from tqdm.auto import tqdm
logger = logging.Getlogger(logging.DEBUG)
sh = logging.streamHander()
sh.setLevel(logging.WARNING)
with logging_redirect_tqdm(loggers=[logger]):
    for i in tqdm(range(5)):
        logger.info(i)

上の例だと,shWARNING以上なので,logger.info(i)shには渡されないはずだが,表示されてしまう。

richを使う

richHandler

from rich.progress import track
from rich.logging import RichHandler
rh = richHandler()
rh.setLevel(logging.DEBUG)
logger.addHandler(rh)

ネスト無し 単純なfor

for i in track(range(10)):
    logger.info(i)

ネスト

from rich.progress import Progress
with Progress() as pb:
    t1 = pb.add_task('inner', total=10)
    t2 = pb.add_task('outer', total=100)

    for i in range(100):
        for j in range(10):
            logger.info('%s %s', i, j)
            pb.update(task_id=t1, completed=j + 1)
        pb.update(task_id=t2, completed=i + 1)
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?