When using Python to perform time-consuming operations, it is common to use a progress bar to visualize the progress. tqdm in Python is used to achieve this function.
First, let’s see the effect of tqdm’s progress bar.
Basic tqdm usage
There are 3 main uses of tqdm, automatic control, manual control or for scripting or command line.
Automatically controlled operation
The most basic usage, wrapping tqdm() directly around an arbitrary iterator.
trange(i) is a specially optimized instance of tqdm(range(i)).
If instantiated outside the loop, manual control of tqdm() can be allowed.
Manual control of the run
Manually control the update of tqdm() with the with statement.
Or without the with statement, but with the del or close() method at the end.
The tqdm.update() method is used to manually update the progress bar and is useful for streaming operations such as reading files.
tqdm advanced applications
tqdm in a multi-process scenario