current position:Home>Python - progress bar artifact tqdm usage
Python - progress bar artifact tqdm usage
2022-02-01 06:47:35 【Why】
This is my participation 11 The fourth of the yuegengwen challenge 22 God , Check out the activity details :2021 One last more challenge
The progress bar display is particularly important during program operation ,Python Use in
tqdm
Library as a progress bar operation tool , This article briefly introducestqdm
Common functions .
background
tqdm
From Arabic taqaddum, It means process ( “progress”);Also in Spanish “I love you so much” (te quiero demasiado) Abbreviation ( This is a coincidence )
- The function of this module is to decorate
tqdm(iterable)
Any iteratable object , Make the loop in the code (loop) Show the progress bar to users during operation . - Stolen Official website The picture shows the effect intuitively
preparation
Introduction package
from tqdm import tqdm
Copy code
Generate tqdm object
tqdm Class constructor :
__init__(iterable=None, desc=None, total=None, leave=True, file=None, ncols=None, mininterval=0.1, maxinterval=10.0, miniters=None, ascii=None, disable=False, unit='it', unit_scale=False, dynamic_ncols=False, smoothing=0.3, bar_format=None, initial=0, position=None, postfix=None, unit_divisor=1000, write_bytes=None, lock_args=None, nrows=None, colour=None, gui=False, **kwargs)
Copy code
The parameters are complex , Here are some common parameters and methods , Please refer to Official document
- iterable: The most common parameters , Indicates that this iteration object is used to initialize
tqdm
object , If the progress bar is updated manually, this parameter can beNone
- desc: Description of the progress bar
- total: Total number of progress bars
- miniters: int, optional. Minimum update interval for progress display during iteration .
- unit : str, optional. Define the unit of each iteration . The default is
"it"
, That is, each iteration , When downloading or decompressing , Set to"B"
, For each “ byte ”. - unit_scale: bool or int or float, optional. The default is
False
, If set to1
perhapsTrue
, It will be automatically converted according to the international system of units (kilo, mega, etc.) . such as , In the example of downloading the progress bar , IfFalse
, The data size is displayed in bytes , Set toTrue
Then it's converted to Kb、Mb.
Usage method
Automatically control progress
- take
tqdm()
Wrap directly on any iterator :
from tqdm import tqdm
for i in tqdm(range(10000)):
pass
>> 100%|██████████████████| 10000/10000 [00:00<00:00, 1248304.76it/s]
Copy code
- Add description :
from tqdm import tqdm
for i in tqdm(range(10000), desc='test str:'):
pass
>> test str:: 100%|█████████████████| 10000/10000 [00:00<00:00, 1666257.75it/s]
Copy code
trange(i)
It's righttqdm(range(i))
Specially optimized examples :
from tqdm import trange
for i in trange(10000):
pass
>> 100%|██████████████████| 10000/10000 [00:00<00:00, 1191844.79it/s]
Copy code
- If you want fine control
tqdm
object , You need to generate objects in advance , And control it in the cycle :
Using external objects, you can create objects directly , At this point, you need to close the object outside the loop :
from tqdm import tqdm
pbar = tqdm(range(10000))
for i in pbar:
pbar.set_description(str(i))
pass
pbar.close()
>> 9999: 100%|█████████████████████████| 10000/10000 [00:02<00:00, 4677.99it/s]
Copy code
You can also use
with
function , Outside the loop body, the compiler automatically ends its life cycle :
from tqdm import tqdm
with tqdm(range(10000)) as pbar:
for i in pbar:
pbar.set_description(str(i))
pass
>> 9999: 100%|█████████████████████████| 10000/10000 [00:02<00:00, 4659.45it/s]
Copy code
Manually control the progress
- When running manually
tqdm
Object does not require an iterator as an initialization parameter , But you need to specify the maximum lengthtotal
Value :
from tqdm import tqdm
with tqdm(total=10000) as pbar:
for i in range(10000):
pbar.update(1)
>> 100%|████████████████████████████| 10000/10000 [00:00<00:00, 99888.16it/s]
Copy code
Reference material
copyright notice
author[Why],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202010647325948.html
The sidebar is recommended
- Django paging (II)
- Concurrent. For Python concurrent programming Futures or multiprocessing?
- Programmers over the age of 25 can't know a few Chinese herbal medicines. Python crawler lessons 9-9
- Python crawler from introduction to pit full series of tutorials (detailed tutorial + various practical combat)
- The second bullet of class in Python
- Python object oriented programming 03: class inheritance and its derived terms
- How IOS developers learn Python Programming 13 - function 4
- Python crawler from introduction to mastery (VI) form and crawler login
- Python crawler from entry to mastery (V) challenges of dynamic web pages
- Deeply understand pandas to read excel, TXT, CSV files and other commands
guess what you like
-
Daily python, Chapter 18, class
-
"I just want to collect some plain photos in Python for machine learning," he said. "I believe you a ghost!"
-
Django view
-
Python implements filtering emoticons in text
-
When winter comes, python chooses a coat with temperament for mom! Otherwise, there's really no way to start!
-
Python crawler - get fund change information
-
Highlight actor using Python VTK
-
Python crawler actual combat: crawling southern weekend news articles
-
leetcode 406. Queue Reconstruction by Height(python)
-
leetcode 1043. Partition Array for Maximum Sum (python)
Random recommended
- Python * * packaging and unpacking details
- Python realizes weather query function
- Python from 0 to 1 (day 12) - Python data application 2 (STR function)
- Python from 0 to 1 (day 13) - Python data application 3
- Numpy common operations of Python data analysis series Chapter 8
- How to implement mockserver [Python version]
- Van * Python! Write an article and publish the script on multiple platforms
- Python data analysis - file reading
- Python data De duplication and missing value processing
- Python office automation - play with browser
- Python series tutorial 127 -- Reference vs copy
- Control flow in Python: break and continue
- Teach you how to extract tables in PDF with Python
- leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal(python)
- leetcode 1338. Reduce Array Size to The Half(python)
- Object oriented and exception handling in Python
- How to configure load balancing for Django service
- How to embed Python in go
- Python Matplotlib drawing graphics
- Python object-oriented programming 05: concluding summary of classes and objects
- Python from 0 to 1 (day 14) - Python conditional judgment 1
- Several very interesting modules in Python
- How IOS developers learn Python Programming 15 - object oriented programming 1
- Daily python, Chapter 20, exception handling
- Understand the basis of Python collaboration in a few minutes
- [centos7] how to install and use Python under Linux
- leetcode 1130. Minimum Cost Tree From Leaf Values(python)
- leetcode 1433. Check If a String Can Break Another String(python)
- Python Matplotlib drawing 3D graphics
- Talk about deep and shallow copying in Python
- Python crawler series - network requests
- Python thread 01 understanding thread
- Analysis of earthquake distribution in the past 10 years with Python~
- You need to master these before learning Python crawlers
- After the old friend (R & D post) was laid off, I wanted to join the snack bar. I collected some data in Python. It's more or less a intention
- Python uses redis
- Python crawler - ETF fund acquisition
- Detailed tutorial on Python operation Tencent object storage (COS)
- [Python] comparison of list, tuple, array and bidirectional queue methods
- Go Python 3 usage and pit Prevention Guide