近期在做一些需要大量循环的工作,但是干循环还是不太好,毕竟程序一跑动辄几小时,页面一直不变,老实讲有点慌。所以新学了一个进度条tqdm给平平无奇的运行页面增添一分色彩
官方简述
tqdm means "progress" in Arabic (taqadum, تقدّم) and is an abbreviation for "I love you so much" in Spanish (te quiero demasiado).
浪漫起来了。。
参数解释
- iterable: 可迭代,可以是一个函数、列表。。。一般用range来计数。
Iterable to decorate with a progressbar. Leave blank to manually manage the updates.
- desc: 描述,字符串
Prefix for the progressbar.
- total: 总数,整型或浮点型
The number of expected iterations. If unspecified, len(iterable) is used if possible. If float("inf") or as a last resort, only basic progress statistics are displayed (no ETA, no progressbar). If gui is True and this parameter needs subsequent updating, specify an initial arbitrary large positive number, e.g. 9e9.
- leave: 完成后是否消失,布尔类型,比较奇怪的是false会消失
If [default: True], keeps all traces of the progressbar upon termination of iteration. If None, will leave only if position is 0.
- file: IO,可以导出进度信息
Specifies where to output the progress messages (default: sys.stderr). Uses file.write(str) and file.flush() methods. For encoding, see write_bytes.
- ncols: 整个进度条的宽度 ps.所有的!所有的
The width of the entire output message.
- mininterval: 最小刷新间隔
Minimum progress display update interval [default: 0.1] seconds.
- maxinterval: 最大刷新间隔
Maximum progress display update interval [default: 10] seconds.
- miniters: 最小迭代间隔,试下来和mininterval没啥区别,可能在复杂迭代的时候有区别
Minimum progress display update interval, in iterations.
- ascii: 布尔类型,用ASCII码填充进度条
If unspecified or False, use unicode (smooth blocks) to fill the meter. The fallback is to use ASCII characters " 123456789#".
- disable: 布尔类型,把进度条搞没。这不是脱裤子XX吗
Whether to disable the entire progressbar wrapper [default: False]. If set to None, disable on non-TTY.
- unit: 计数单位
String that will be used to define the unit of each iteration [default: it].
- unit_scale: 单位换算
If 1 or True, the number of iterations will be reduced/scaled automatically and a metric prefix following the International System of Units standard will be added (kilo, mega, etc.) [default: False]. If any other non-zero number, will scale total and n.
- dynamic_ncols: 使进度条宽高可调
If set, constantly alters ncols and nrows to the environment (allowing for window resizes) [default: False].
- smoothing: 平滑移动
Exponential moving average smoothing factor for speed estimates (ignored in GUI mode). Ranges from 0 (average speed) to 1 (current/instantaneous speed) [default: 0.3].
- bar_format: 自定义样式
Specify a custom bar string formatting. May impact performance. [default: '{l_bar}{bar}{r_bar}'], where l_bar='{desc}: {percentage:3.0f}%|' and r_bar='| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, ' '{rate_fmt}{postfix}]' Possible vars: l_bar, bar, r_bar, n, n_fmt, total, total_fmt, percentage, elapsed, elapsed_s, ncols, nrows, desc, unit, rate, rate_fmt, rate_noinv, rate_noinv_fmt, rate_inv, rate_inv_fmt, postfix, unit_divisor, remaining, remaining_s, eta. Note that a trailing ": " is automatically removed after {desc} if the latter is empty.
- initial: 设置初始值,不从0开始的时候用
The initial counter value. Useful when restarting a progress bar [default: 0]. If using float, consider specifying {n:.3f} or similar in bar_format, or specifying unit_scale.
- position: 进度条之前空几行
Specify the line offset to print this bar (starting from 0) Automatic if unspecified. Useful to manage multiple bars at once (eg, from threads).
- postfix: 在进度条之后显示其他信息
Specify additional stats to display at the end of the bar. Calls set_postfix(**postfix) if possible (dict).
- unit_divisor: 和unit_scale配合使用,单位除数
[default: 1000], ignored unless unit_scale is True.
- write_bytes: bool, optional
If (default: None) and file is unspecified, bytes will be written in Python 2. If True will also write bytes. In all other cases will default to unicode.
- lock_args: 刷新中间量
Passed to refresh for intermediate output (initialisation, iterating, and updating).
- nrows: 高度
The screen height. If specified, hides nested bars outside this bound. If unspecified, attempts to use environment height. The fallback is 20.
- colour: 进度条颜色
Bar colour (e.g. 'green', '00ff00').
运行方式
迭代
for i in tqdm(range(1,5),desc='processing',colour='CYAN'):
time.sleep(1)
“手动”
with tqdm(total=100, colour='GREEN') as pbar:
for i in range(0,100):
if i%3 == 0:
pbar.update()
else:
time.sleep(0.1)
continue