如果在 Jupyter 笔记本中使用中断,tqdm 颜色条显示红色
我使用 tqdm.notebook 中的 tqdm 来显示文件行迭代的进度条。我提供了total 参数来给出将要执行的迭代次数(因为我预先知道),以便可以准确地显示进度。
我使用 break
在所需迭代的最大数量(要读取的文件行)处停止。
尽管执行的迭代次数等于 tqdm
中提供给 total
的值,并且进度条显示已执行的最大迭代次数(例如 11/示例中的 11;参见图片),条形图显示为红色(不是绿色),表示提前终止/错误。
我已经尝试将 tqdm 对象分配给变量,并按照 这个相关问题。
如何才能让进度条正确显示?
from tqdm.notebook import tqdm
LETTERS = list('ABCDEFGHIJKL')
for idx, letter in enumerate(tqdm(LETTERS, total=len(LETTERS)-1)):
print(letter)
if idx >= len(LETTERS) - 1:
break
版本信息(如果有帮助)
IPython:8.1.1 ipykernel:6.9.2 ipywidgets:7.7.0 jupyter_client:7.1.2 jupyter_核心:4.9.2 jupyter_server:未安装 jupyterlab:未安装 NB客户端:0.5.13 nbconvert:6.4.4 nb格式:5.2.0 笔记本:6.4.10 qtconsole:5.2.2 特征:5.1.1
使用 Python 3.8.10 运行。
I use tqdm
from tqdm.notebook
to display a progress bar for iteration through lines of a file. I supply the total
argument to give the number of iterations that will be performed (since I know it upfront) so the progress can be accurately displayed.
I use a break
to stop at the maximum number of desired iterations (lines of the file to read).
Despite the fact that the number of iterations performed is equal to the value supplied to total
in tqdm
and the progress bar shows the maximum number of iterations have been performed (e.g. 11/11 in the example; see image), the bar is displayed in red (not green) indicating premature termination / an error.
I have already tried to assign the tqdm object to a variable and explicitly close the iterator in the condition before the break as per this related question.
How can I make the progress bar display correctly?
from tqdm.notebook import tqdm
LETTERS = list('ABCDEFGHIJKL')
for idx, letter in enumerate(tqdm(LETTERS, total=len(LETTERS)-1)):
print(letter)
if idx >= len(LETTERS) - 1:
break
Version information (if helpful)
IPython : 8.1.1
ipykernel : 6.9.2
ipywidgets : 7.7.0
jupyter_client : 7.1.2
jupyter_core : 4.9.2
jupyter_server : not installed
jupyterlab : not installed
nbclient : 0.5.13
nbconvert : 6.4.4
nbformat : 5.2.0
notebook : 6.4.10
qtconsole : 5.2.2
traitlets : 5.1.1
Running with Python 3.8.10.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于
break
位于最后一次迭代中,因此tqdm
假定循环意外停止,即使循环从语义方面完成也是如此。如果您想在最终结构中有一个绿色进度条,您的代码如下
Because the
break
is in the last iteration,tqdm
assumes that the loop stopped unintendedly, even if the loop is finished from the semantic side.If you want to have a green progress bar in the end structure your code like this