Python 我可以向 tqdm 进度条添加消息吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37506645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:28:37  来源:igfitidea点击:

Can I add message to the tqdm progressbar?

pythontqdm

提问by Dror Hilman

When using the tqdm progress bar: can I add a message to the same line as the progress bar in a loop?

使用 tqdm 进度条时:我可以在循环中将消息添加到与进度条相同的行吗?

I tried using the "tqdm.write" option, but it adds a new line on every write. I would like each iteration to show a short message next to the bar, that will disappear in the next iteration. Is this possible?

我尝试使用“tqdm.write”选项,但它在每次写入时添加一个新行。我希望每次迭代都在栏旁边显示一条短消息,该消息将在下一次迭代中消失。这可能吗?

回答by Ghrua

The example shown in Usage of tqdmworks well for me.

tqdm 的用法中显示的示例对我来说效果很好。

pbar = tqdm(["a", "b", "c", "d"])
for char in pbar:
    pbar.set_description("Processing %s" % char)

回答by gaborous

You can change the description to show a small message before the progress bar, like this:

您可以更改描述以在进度条前显示一条小消息,如下所示:

from tqdm import trange
from time import sleep
t = trange(100, desc='Bar desc', leave=True)
for i in t:
    t.set_description("Bar desc (file %i)" % i)
    t.refresh() # to show immediately the update
    sleep(0.01)