In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to use tqdm to display progress in Python applications". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use tqdm to display progress in Python applications.
The Semitic root q-d-m in Aramaic, Hebrew and Arabic is usually associated with progress or progress. Taqaddum in Arabic means "progress". Progress is very important. As every feel-good movie tells you, journey is as important as destination.
Most programs have a clear goal, a desired final state. Sometimes it can take a long time to calculate this final state. Although computers don't care about feelings, people do. Human beings are not willing to sit and wait, and there is no obvious sign of progress. The questions are spreading. Did the program crash? Is disk performance jitter? Does the operating system allocate all computing resources to other tasks?
Like justice, progress must be seen, not just completed. The Python library tqdm helps to make progress clear.
The tqdm module works under the console, but it also specifically supports Jupyter, one of my favorite environments. To use tqdm in Jupyter, you need to import the notebook submodule and install ipywidgets. The notebook submodule is compatible with the tqdm interface.
This means that you can do some import-time actions to import the correct module while keeping the usage of tqdm unchanged. The trick is to check whether the _ _ main__ module has the global variable get_ipython. Although this is only a heuristic method, it is a fairly accurate one:
Import sys if hasattr (sys. Modules ["_ _ main__"], "get_ipython"): from tqdm import notebook as tqdm else: import tqdm
In the simplest case, something needs to be run for a certain number of iterations (known in advance), and each iteration takes about the same time. For example, there is an algorithm for calculating the square root of any number, starting with 1 as a guess, and then calculating an improved guess:
Def improve_guess (rt, n): return (rt + n/rt) / 2
A little improvement will bring you closer to the square root. For example, you can calculate the square root of 2:
Guess = 1 target = 2 for i in tqdm.trange (10): guess = improve_guess (guess, target)
Accurate to 10 decimal places!
Round (2-guess*guess, 10) 0.0
A slightly more complex example is when the number of elements is known and processing each element takes a similar amount of time. For example, you can calculate the product of some numbers. To do this, you need some random numbers:
Import random numbers = [random.uniform (0,2.8) for i in range (100)] numbers [: 5] [2.6575636572230916,0.1286674965830302,1.0634250104041332, 1.1760969844376505,0.45192978568125486]
Now that you have these numbers, you can multiply them. The easiest way to use tqdm is to wrap a Python iterative function. The values are the same, but tqdm displays a progress bar:
Result = 1 for num in tqdm.tqdm (numbers): result * = num result2.4081854901728303
Tqdm output
However, not everything is predictable. One of the most unpredictable things is the speed of the network. When you download a large file, the only way to measure progress is to check how much has been downloaded:
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 (totaltotal=total) as progress: for chunk in response.iter_bytes (): progress.update (len (chunk))
Tqdm output
Sometimes it makes sense to "nest" the progress bar. For example, if you want to download a directory, you need a progress bar to track files and set a progress bar for each file.
Here is an example (but without actually downloading a directory):
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 (totaltotal=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)
Tqdm output
So, if your program takes a while to display the final results, to avoid frustrating your users. Please show its progress!
At this point, I believe you have a deeper understanding of "how to use tqdm to show progress in Python applications". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.