Recently, I saw a few packages on this topic, two stood out:
- progressbar (>= 2.3)
- tqdm
For a big loop like:
for i in range(1000000): # do something here
It might look a while and lack of responses could make user think something has just gone wrong, even it might not be so. A progress bar certainly is a good idea to let user know everything is doing fine, just need some time to process. The question is how you implement it. These two packages provide a quick way to display a progress bar with very little extra coding:
import time from progressbar import (Bar, ETA, FileTransferSpeed, Percentage, ProgressBar, RotatingMarker) from tqdm import tqdm widgets = [Bar(), ' ', Percentage(), ' ', ETA(), ' ', FileTransferSpeed('iters')] pbar = ProgressBar(widgets=widgets) # or simply for default progress bar style # pbar = ProgressBar() for i in pbar(range(100)): time.sleep(0.01) for i in tqdm(range(100), leave=True): time.sleep(0.01) print
The final output looks like:
|##################################| 100% Time: 0:00:01 95.31 iters/s |##########| 100/100 100% [elapsed: 00:01 left: 00:00, 98.77 iters/sec]
You simply wrap the iterable with progressbar.ProgressBar() or tqdm.tadm, and that’s all. They can estimate the number of iterations, show the statistics and time, accordingly.
progressbar has more customization options, you could rearrange and format the way you like while tqdm is more simple, but still does the job right.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.