developer tip

Jupyter 노트북의 tqdm

optionbox 2020. 9. 1. 07:25
반응형

Jupyter 노트북의 tqdm


tqdmJupyter 노트북에서 실행중인 스크립트의 진행 상황을 인쇄 하는 사용 하고 있습니다. 를 통해 모든 메시지를 콘솔에 인쇄하고 tqdm.write()있습니다. 그러나 이것은 여전히 ​​다음과 같이 왜곡 된 출력을 제공합니다.

여기에 이미지 설명 입력

즉, 새 줄을 인쇄해야 할 때마다 새 진행률 표시 줄이 다음 줄에 인쇄됩니다. 터미널을 통해 스크립트를 실행할 때는 발생하지 않습니다. 어떻게 해결할 수 있습니까?


여기에 설명 된대로 tqdm_notebook대신을 사용해보십시오 . 이 단계에서는 실험적이지만 대부분의 경우 잘 작동합니다.tqdm

가져 오기를 다음으로 변경하는 것만 큼 간단 할 수 있습니다.

from tqdm import tqdm_notebook as tqdm

행운을 빕니다!

편집 : 테스트 후 tqdmJupyter 노트북의 '텍스트 모드'에서 실제로 제대로 작동 하는 것 같습니다 . 최소한의 예제를 제공하지 않았기 때문에 말하기는 어렵지만 문제가 각 반복의 print 문으로 인해 발생한 것 같습니다. print 문은 각 상태 표시 줄 업데이트 사이에 숫자 (~ 0.89)를 출력하여 출력을 엉망으로 만듭니다. print 문을 제거해보십시오.


이것은 tqdm_notebook 이 작동하지 않는 경우에 대한 대안 입니다.

다음 예가 주어집니다.

from time import sleep
from tqdm import tqdm

values = range(3)
with tqdm(total=len(values)) as pbar:
    for i in values:
        pbar.write('processed: %d' %i)
        pbar.update(1)
        sleep(1)

출력은 다음과 같습니다 (진행률이 빨간색으로 표시됨).

  0%|          | 0/3 [00:00<?, ?it/s]
processed: 1
 67%|██████▋   | 2/3 [00:01<00:00,  1.99it/s]
processed: 2
100%|██████████| 3/3 [00:02<00:00,  1.53it/s]
processed: 3

문제는 stdoutstderr에 대한 출력 이 새 줄 측면에서 비동기 적으로 개별적으로 처리 된다는 것 입니다.

Jupyter가 stderr에서 첫 번째 행을 수신 한 다음 stdout에서 "처리 된"출력을 수신한다고하면. 그런 다음 진행 상황을 업데이트하기 위해 stderr에서 출력을 수신하면 마지막 줄만 업데이트하므로 돌아가서 첫 번째 줄을 업데이트하지 않습니다. 대신 새 줄을 작성해야합니다.

해결 방법 1, stdout에 쓰기

한 가지 해결 방법은 대신 stdout에 둘 다 출력하는 것입니다.

import sys
from time import sleep
from tqdm import tqdm

values = range(3)
with tqdm(total=len(values), file=sys.stdout) as pbar:
    for i in values:
        pbar.write('processed: %d' % (1 + i))
        pbar.update(1)
        sleep(1)

출력이 다음과 같이 변경됩니다 (더 이상 빨간색 없음).

processed: 1   | 0/3 [00:00<?, ?it/s]
processed: 2   | 0/3 [00:00<?, ?it/s]
processed: 3   | 2/3 [00:01<00:00,  1.99it/s]
100%|██████████| 3/3 [00:02<00:00,  1.53it/s]

여기서 우리는 줄이 끝날 때까지 Jupyter가 지워지지 않는 것처럼 보입니다. 공백을 추가하여 다른 해결 방법을 추가 할 수 있습니다. 예 :

import sys
from time import sleep
from tqdm import tqdm

values = range(3)
with tqdm(total=len(values), file=sys.stdout) as pbar:
    for i in values:
        pbar.write('processed: %d%s' % (1 + i, ' ' * 50))
        pbar.update(1)
        sleep(1)

우리에게 다음을 제공합니다.

processed: 1                                                  
processed: 2                                                  
processed: 3                                                  
100%|██████████| 3/3 [00:02<00:00,  1.53it/s]

해결 방법 2, 대신 설명 설정

일반적으로 두 개의 출력을 가지지 않고 대신 설명을 업데이트하는 것이 더 간단 할 수 있습니다. 예 :

import sys
from time import sleep
from tqdm import tqdm

values = range(3)
with tqdm(total=len(values), file=sys.stdout) as pbar:
    for i in values:
        pbar.set_description('processed: %d' % (1 + i))
        pbar.update(1)
        sleep(1)

출력 (처리하는 동안 업데이트 된 설명) :

processed: 3: 100%|██████████| 3/3 [00:02<00:00,  1.53it/s]

결론

You can mostly get it to work fine with plain tqdm. But if tqdm_notebook works for you, just use that (but then you'd probably not read that far).


If the other tips here don't work and - just like me - you're using the pandas integration through progress_apply, you can let tqdm handle it:

from tqdm.auto import tqdm
tqdm.pandas()

df.progress_apply(row_function, axis=1)

The main point here lies in the tqdm.auto module. As stated in their instructions for use in IPython Notebooks, this makes tqdm choose between progress bar formats used in Jupyter notebooks and Jupyter consoles - for a reason still lacking further investigations on my side, the specific format chosen by tqdm.auto works smoothly in pandas, while all others didn't, for progress_apply specifically.

참고 URL : https://stackoverflow.com/questions/42212810/tqdm-in-jupyter-notebook

반응형