Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the progress bar gracefully in Python

Shulou Source: shulou.com Published: 2022-06-02 02:46:09 10月03日 Update

In this issue, Xiaobian will bring you about how to use the progress bar elegantly in Python. The article is rich in content and analyzed and described from a professional perspective. After reading this article, I hope you can gain something.

tqdm means "progress" in Arabic. The tqdm module allows you to show the progress of a loop very elegantly in the form of a progress bar.

By simply nesting tqdm on an iterable object, you can implement a progress bar that monitors loops.

The progress bar is displayed by default in the standard error stream, the sys.stderr file stream.

Here are some silly examples that show you the elegance and charm of tqdm modules.

Example 1: Basic example import time

from tqdm import tqdm

for i in tqdm(range(100)):

time.sleep(0.1)

Example 2: Add description [object Object] Example 3: Manually update import time

from tqdm import tqdm

with tqdm(total=100) as pbar:

for i in range(10):

time.sleep(0.1)

pbar.update(10)

Appendix: Manual implementation of loop progress bar import sys,time

#Progress bar function

def progress_bar(num,total):

rate = float(num)/total

ratenum = int(100*rate)

r = '\r[{}{}]{}%'.format('*'*ratenum,' '*(100-ratenum),ratenum)

sys.stdout.write(r)

sys.stdout.flush()

i,n = 0,100

for i in range(n):

time.sleep(0.1)

progress_bar(i+1,n)

The above is how to use the progress bar elegantly in Python shared by Xiaobian. If you happen to have similar doubts, you may wish to refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.

Tags: Progress examples elegance loops content modules analysis charming stupid professional small and medium rich in content functions can pass objects that is meaning manual manual files Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information OPPO Reno Apple Shulou Tech Info Docker