tqdm is used to show progress percentage in python app.
usage
1) import module
import sys
if hasattr(sys.modules["__main__"], "get_ipython"):
from tqdm import notebook as tqdm
else:
import tqdm
2)
-
certain number of iterations
def improve_guess(rt, n): return (rt + n/rt) / 2 guess = 1 target = 2 for i in tqdm.trange(10): guess = improve_guess(guess, target)
- processing element in list
import random numbers = [random.uniform(0, 2.8) for i in range(100)] numbers[:5] result = 1 for num in tqdm.tqdm(numbers): result *= num result
- download progress
url = "https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tgz" import httpx with httpx.stream("GET", url) as response: total = int(response.headers["Content-Length"]) with tqdm.tqdm(total=total) as progress: for chunk in response.iter_bytes(): progress.update(len(chunk))
- “nest” progress bars
files = [f"vid-{i}.mp4" for i in range(4)] for fname in tqdm.tqdm(files, desc="files"): total = random.randrange(10**9, 2 * 10**9) with tqdm.tqdm(total=total, desc=fname) as progress: current = 0 while current < total: chunk_size = min(random.randrange(10**3, 10**5), total - current) current += chunk_size if random.uniform(0, 1) < 0.01: time.sleep(0.1) progress.update(chunk_size)