首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python进度条

Python进度条
EN

Stack Overflow用户
提问于 2010-07-02 02:39:37
回答 33查看 654.2K关注 0票数 424

当我的脚本正在执行一些可能需要花费时间的任务时,我如何使用进度条?

例如,一个函数需要一些时间才能完成,完成后返回True。如何在函数执行期间显示进度条?

请注意,我需要这是实时的,所以我不知道该怎么做。我需要一个thread来完成这个任务吗?我没有头绪。

现在,当函数正在执行时,我不会打印任何东西,但是一个进度条会更好。此外,我更感兴趣的是如何从代码的角度来实现这一点。

EN

回答 33

Stack Overflow用户

回答已采纳

发布于 2010-07-02 02:57:19

有一些特定的库(like this one here),但也许可以做一些非常简单的事情:

代码语言:javascript
复制
import time
import sys

toolbar_width = 40

# setup toolbar
sys.stdout.write("[%s]" % (" " * toolbar_width))
sys.stdout.flush()
sys.stdout.write("\b" * (toolbar_width+1)) # return to start of line, after '['

for i in xrange(toolbar_width):
    time.sleep(0.1) # do real work here
    # update the bar
    sys.stdout.write("-")
    sys.stdout.flush()

sys.stdout.write("]\n") # this ends the progress bar

注意:progressbar2progressbar的一个分支,已经多年没有维护过了。

票数 216
EN

Stack Overflow用户

发布于 2014-11-05 23:52:59

使用tqdm (conda install tqdmpip install tqdm),您可以在一秒钟内向循环中添加一个进度指示器:

代码语言:javascript
复制
from time import sleep
from tqdm import tqdm
for i in tqdm(range(10)):
    sleep(3)

 60%|██████    | 6/10 [00:18<00:12,  0.33 it/s]

此外,还有一个notebook version

代码语言:javascript
复制
from tqdm.notebook import tqdm
for i in tqdm(range(100)):
    sleep(3)

您可以使用tqdm.auto而不是tqdm.notebook在终端和笔记本中工作。

tqdm.contrib包含一些辅助函数,用于执行enumeratemapzip等操作。在tqdm.contrib.concurrent中有并发映射。

您甚至可以使用tqdm.contrib.telegramtqdm.contrib.discord在断开与jupyter笔记本电脑的连接后将进度发送到您的手机。

票数 547
EN

Stack Overflow用户

发布于 2013-04-07 17:12:00

上面的建议很好,但我认为大多数人只想要一个现成的解决方案,不依赖于外部包,但也可以重用。

我得到了上面所有东西中最好的一点,并把它变成了一个函数,还有一个测试用例。

要使用它,只需复制"def update_progress(Update_progress)“下面的行,而不是测试脚本。别忘了导入sys。每当需要显示或更新进度条时,都可以调用此方法。

这是通过将"\r“符号直接发送到控制台以将光标移回开始位置来实现的。python中的"print“不符合上面的符号,因此我们需要”sys“。

代码语言:javascript
复制
import time, sys

# update_progress() : Displays or updates a console progress bar
## Accepts a float between 0 and 1. Any int will be converted to a float.
## A value under 0 represents a 'halt'.
## A value at 1 or bigger represents 100%
def update_progress(progress):
    barLength = 10 # Modify this to change the length of the progress bar
    status = ""
    if isinstance(progress, int):
        progress = float(progress)
    if not isinstance(progress, float):
        progress = 0
        status = "error: progress var must be float\r\n"
    if progress < 0:
        progress = 0
        status = "Halt...\r\n"
    if progress >= 1:
        progress = 1
        status = "Done...\r\n"
    block = int(round(barLength*progress))
    text = "\rPercent: [{0}] {1}% {2}".format( "#"*block + "-"*(barLength-block), progress*100, status)
    sys.stdout.write(text)
    sys.stdout.flush()


# update_progress test script
print "progress : 'hello'"
update_progress("hello")
time.sleep(1)

print "progress : 3"
update_progress(3)
time.sleep(1)

print "progress : [23]"
update_progress([23])
time.sleep(1)

print ""
print "progress : -10"
update_progress(-10)
time.sleep(2)

print ""
print "progress : 10"
update_progress(10)
time.sleep(2)

print ""
print "progress : 0->1"
for i in range(101):
    time.sleep(0.1)
    update_progress(i/100.0)

print ""
print "Test completed"
time.sleep(10)

这就是测试脚本的结果所显示的(最后一个进度条动画):

代码语言:javascript
复制
progress : 'hello'
Percent: [----------] 0% error: progress var must be float
progress : 3
Percent: [##########] 100% Done...
progress : [23]
Percent: [----------] 0% error: progress var must be float

progress : -10
Percent: [----------] 0% Halt...

progress : 10
Percent: [##########] 100% Done...

progress : 0->1
Percent: [##########] 100% Done...
Test completed
票数 96
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3160699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档