Python
Tqdm - 진행율 표시하기(progress bar)
IT_달토끼
2022. 10. 23. 14:01
파이썬 명령을 실행할 때, 시간이 오래 걸리는 경우가 있다.
그럴 때, 화면에 진행율을 표시해주면 대기시간이 덜 답답할 것이다.
우선 tqdm을 따로 설치해주어야 한다.
명령창에서 pip install tqdm을 실행하자.
pip install tqdm
주피터 노트북에서 바로 설치하기를 원한다면, 앞에 매직명령어인 !를 붙여주면 된다.
! pip install tqdm
만약 anaconda를 이용해서 python을 깔았다면, anaconda prompt에서 다음 명령어를 수행한다.
conda install -c conda-forge tqdm
tqdm이 깔렸다면, 사용하는 방법은 간단하다.
tqdm의 인자로 iterale한 객체를 넣어주면 된다.
tqdm 안에 desc속성값을 주면, 프로그레스바 앞에 표시되는 문자열을 넣어줄 수 있다.
추가로 수행속도가 빨라서 프로그레스바가 바로 100%로 표시되는 것을 방지하기 위해 time.sleep을 걸어준다.
from tqdm import tqdm
import time
customer_list = ['amy', 'babie', 'catherine', 'david', 'ecole']
for i in tqdm(customer_list, desc='progress bar'):
time.sleep(0.05)
코드를 실행하면, 다음과 같이 프로그레스 바가 나타날 것이다.
만약 주피터 노트북에서 tqdm 라이브러리를 사용하기를 원하다면, 다음과 같이 임포트하면 된다.
from tqdm import tqdm_notebook
import time
customer_list = ['amy', 'babie', 'catherine', 'david', 'ecole']
for i in tqdm(customer_list, desc='progress bar'):
time.sleep(0.05)
다음과 같이 trange()메소드를 이용해서 반복횟수를 더욱 편하게 설정할 수도 있다.
trange(i)는 tqdm(range(i))와 같다.
from tqdm import tqdm, trange
import time
for i in trange(10):
print(i, end='')
time.sleep(0.05)
tqdm으로 표현해보면 아래와 같이 나타낼 수 있다.
from tqdm import tqdm
import time
for i in tqdm(range(10)):
print(i, end='')
time.sleep(0.05)
더 자세한 정보는 아래 github에서 확인할 수 있다.