LoginSignup
4
0

More than 1 year has passed since last update.

google colab でログ出力無しで進捗を表示する

Posted at

1.はじめに
 google colab でライブラリのインストールなどを行うとき、場合によっては長々とログを出力することがあります。これはスマートでないですが、ログを出力させないと今度は進捗が全く分からない。では、どうすれば良いか。

2.対策
 tqdm.notebook と  io.capture_output() を下記のように組み合わせます。処理ブロックがA〜Dの4つだとしたら、各ブロックの終わりに pbar.update(*) で進捗率を記載し、合計100になるようにしておけばOKです。
 各ブロックが終わると pbar.update(*) で設定した分プログレスバーが増えますので、大体の進捗が1行で分ります。

from IPython.utils import io
import os
import subprocess
import tqdm.notebook

TQDM_BAR_FORMAT = '{l_bar}{bar}| {n_fmt}/{total_fmt} [elapsed: {elapsed} remaining: {remaining}]'

try:
  with tqdm.notebook.tqdm(total=100, bar_format=TQDM_BAR_FORMAT) as pbar:
    with io.capture_output() as captured:
      # -------
      #    A
      # -------
      pbar.update(8)

      # -------
      #    B
      # -------
      pbar.update(10)

      # -------
      #    C
      # -------
      pbar.update(2)

      # -------
      #    D
      # -------
      pbar.update(80)
except subprocess.CalledProcessError:
  print(captured)
  raise

コンソール.png

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