小工具|Python进度条模块Progressbar

简介: 小工具|Python进度条模块Progressbar

Python进度条模块Progressbar

今天突然被问了一个问题:程序在执行中很久不结束是怎么回事?看不到程序执行的进度,在我们进行大工程的时候的确是一件很头疼的事,本文让我们来了解一个很常用的进度条展示小工具——Progressbar。

注:最新版的使用者不要照着GitHub上的官方文档去操作,文档信息没有维护,会出现很多问题。

下载模块

pip install progressbar

注意:安装模块的时候可能会出现warning,耐心等待,总会出现success。

简单的使用方法

import time
from progressbar import *
total = 1000
def test_func():
    time.sleep(0.01)
progress = ProgressBar()
for i in progress(range(1000)):
    test_func()

对于简单的循环函数而言,我们只需要把它加在我们组合了progress方法的循环下就可以了。

我们会看到如下的进度条:

image.png

放在循环外定义使用

# -*- coding=utf-8 -*-
from __future__ import division
import sys, time
from progressbar import *
total = 1000
def test_func():
    time.sleep(0.01)
bar = ProgressBar().start()
for i in range(1000):
    bar.update(int((i / (total - 1)) * 100))
    test_func()
bar.finish()

注意:不要忽略了start()和fininsh()否则会出现问题。

多层信息的展示使用

# -*- coding=utf-8 -*-
import time
from progressbar import *
total = 1000
def test_func():
    time.sleep(0.01)
widgets = [
    'Progress: ',
    Percentage(), ' ',
    Bar('#'), ' ',
    Timer(), ' ',
    ETA(), ' ',
    FileTransferSpeed()
]
bar = ProgressBar(widgets=widgets, maxval=10 * total).start()
for i in range(total):
    bar.update(10 * i + 1)
    test_func()
bar.finish()

结果如下:

image.png

参数说明:

'Progress: ' :设置进度条前显示的文字

Percentage() :显示百分比

Bar('#') :设置进度条形状

ETA() :显示预计剩余时间

Timer() :显示已用时间

相关文章
|
1天前
|
机器学习/深度学习 数据可视化 数据挖掘
Python数据分析工具有哪些
【7月更文挑战第3天】Python数据分析工具有哪些
103 58
|
1天前
|
JSON JavaScript 数据格式
|
1天前
|
监控 Python
paramiko 模块 ---Python脚本监控当前系统的CPU、内存、根目录、IP地址等信息
paramiko 模块 ---Python脚本监控当前系统的CPU、内存、根目录、IP地址等信息
|
11天前
|
XML 数据格式 Python
Python的`import`用于加载模块,基础形式是`import module`,全量导入
【6月更文挑战第23天】Python的`import`用于加载模块,基础形式是`import module`,全量导入;`from module import name`选择性导入部分,减少命名空间污染;`from module import *`导入所有(不推荐),易引发冲突。别名导入如`from math import sqrt as square_root`可避免冲突。包导入用`.`,如`import xml.etree.ElementTree as ET`。
35 8
|
11天前
|
XML 数据格式 Python
在Python中,导入其他模块是通过使用import语句完成的
在Python中导入模块涉及`import`语句的不同用法:1) `import math`导入整个标准库;2) `from math import sqrt`导入单个函数;3) `import numpy as np`使用别名;4) `from random import *`导入所有(不推荐);5) `import xml.etree.ElementTree as ET`导入子模块;6) 使用`importlib.import_module()`延迟导入;7) `from .module import func`导入相对路径模块,需管理`sys.path`。
31 6
|
8天前
|
Python
python--os模块
python--os模块
11 2
|
8天前
|
Python
python-time模块
python-time模块
9 1
|
10天前
|
算法 数据安全/隐私保护 开发者
Python logger模块详细讲解
Python logger模块详细讲解
15 3
|
11天前
|
Python
Python的`os`模块核心功能概述:通过`os.getcwd()`获取
【6月更文挑战第23天】Python的`os`模块核心功能概述:通过`os.getcwd()`获取、`os.chdir()`改变工作目录;使用`os.mkdir()`, `os.makedirs()`创建目录,`os.rmdir()`, `os.removedirs()`删除;`os.rename()`, `os.renames()`重命名文件或目录;`os.remove()`删除文件;`os.listdir()`列出目录内容;`os.path.exists()`, `os.path.isfile()`, `os.path.isdir()`检查路径;`os.stat()`获取文件属性。
22 4
|
11天前
|
XML 数据格式 Python
Python模块导入包括:`import math`导入标准库
【6月更文挑战第23天】Python模块导入包括:`import math`导入标准库,`from math import sqrt`导入单个函数,`import numpy as np`给模块取别名,`from random import *`导入所有(不推荐),`import xml.etree.ElementTree as ET`导入子模块,`import_module('pandas')`按需导入,和使用相对路径如`from .module import func`处理项目结构。记得调整`sys.path`以包含自定义模块路径。
21 4