Python Jupyter Notebook 中的 tqdm 反复打印新的进度条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42212810/
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
tqdm in Jupyter Notebook prints new progress bars repeatedly
提问by Rohan Saxena
I am using tqdm
to print progress in a script I'm running in a Jupyter notebook. I am printing all messages to the console via tqdm.write()
. However, this still gives me a skewed output like so:
我正在使用tqdm
在 Jupyter 笔记本中运行的脚本打印进度。我正在通过tqdm.write()
. 然而,这仍然给了我一个像这样倾斜的输出:
That is, each time a new line has to be printed, a new progress bar is printed on the next line. This does not happen when I run the script via terminal. How can I solve this?
也就是说,每次必须打印新行时,都会在下一行打印一个新的进度条。当我通过终端运行脚本时不会发生这种情况。我该如何解决这个问题?
回答by oscarbranson
Try using tqdm.notebook.tqdm
instead of tqdm
, as outlined here.
尝试使用tqdm.notebook.tqdm
,而不是tqdm
作为概述这里。
This could be as simple as changing your import to:
这可能就像将导入更改为:
from tqdm.notebook import tqdm
from tqdm.notebook import tqdm
Good luck!
祝你好运!
EDIT:After testing, it seems that tqdm
actually works fine in 'text mode' in Jupyter notebook. It's hard to tell because you haven't provided a minimal example, but it looks like your problem is caused by a print statement in each iteration. The print statement is ouputting a number (~0.89) in between each status bar update, which is messing up the output. Try removing the print statement.
编辑:经过测试,似乎tqdm
在 Jupyter 笔记本中的“文本模式”中实际上可以正常工作。很难说,因为你没有提供一个最小的例子,但看起来你的问题是由每次迭代中的打印语句引起的。打印语句在每次状态栏更新之间输出一个数字(~0.89),这会弄乱输出。尝试删除打印语句。
回答by de1
This is an alternative answer for the case where tqdm_notebookdoesn't work for you.
这是tqdm_notebook不适合您的情况的替代答案。
Given the following example:
给出以下示例:
from time import sleep
from tqdm import tqdm
values = range(3)
with tqdm(total=len(values)) as pbar:
for i in values:
pbar.write('processed: %d' %i)
pbar.update(1)
sleep(1)
The output would look something like this (progress would show up red):
输出看起来像这样(进度会显示为红色):
0%| | 0/3 [00:00<?, ?it/s]
processed: 1
67%|██████▋ | 2/3 [00:01<00:00, 1.99it/s]
processed: 2
100%|██████████| 3/3 [00:02<00:00, 1.53it/s]
processed: 3
The problem is that the output to stdoutand stderrare processed asynchronously and separately in terms of new lines.
问题是stdout和stderr 的输出是异步处理的,并且根据新行分开处理。
If say Jupyter receives on stderr the first line and then the "processed" output on stdout. Then once it receives an output on stderr to update the progress, it wouldn't go back and update the first line as it would only update the last line. Instead it will have to write a new line.
如果说 Jupyter 在 stderr 上接收第一行,然后在 stdout 上接收“已处理”输出。然后,一旦它收到 stderr 上的输出以更新进度,它就不会返回并更新第一行,因为它只会更新最后一行。相反,它将不得不写一个新行。
Workaround 1, writing to stdout
解决方法 1,写入标准输出
One workaround would be to output both to stdout instead:
一种解决方法是将两者都输出到 stdout:
import sys
from time import sleep
from tqdm import tqdm
values = range(3)
with tqdm(total=len(values), file=sys.stdout) as pbar:
for i in values:
pbar.write('processed: %d' % (1 + i))
pbar.update(1)
sleep(1)
The output will change to (no more red):
输出将更改为(不再是红色):
processed: 1 | 0/3 [00:00<?, ?it/s]
processed: 2 | 0/3 [00:00<?, ?it/s]
processed: 3 | 2/3 [00:01<00:00, 1.99it/s]
100%|██████████| 3/3 [00:02<00:00, 1.53it/s]
Here we can see that Jupyter doesn't seem to clear until the end of the line. We could add another workaround for that by adding spaces. Such as:
在这里我们可以看到 Jupyter 似乎直到行尾才清除。我们可以通过添加空格来为此添加另一种解决方法。如:
import sys
from time import sleep
from tqdm import tqdm
values = range(3)
with tqdm(total=len(values), file=sys.stdout) as pbar:
for i in values:
pbar.write('processed: %d%s' % (1 + i, ' ' * 50))
pbar.update(1)
sleep(1)
Which gives us:
这给了我们:
processed: 1
processed: 2
processed: 3
100%|██████████| 3/3 [00:02<00:00, 1.53it/s]
Workaround 2, set description instead
解决方法 2,改为设置说明
It might in general be more straight forward not to have two outputs but update the description instead, e.g.:
一般来说,没有两个输出而是更新描述可能更直接,例如:
import sys
from time import sleep
from tqdm import tqdm
values = range(3)
with tqdm(total=len(values), file=sys.stdout) as pbar:
for i in values:
pbar.set_description('processed: %d' % (1 + i))
pbar.update(1)
sleep(1)
With the output (description updated while it's processing):
随着输出(在处理时更新描述):
processed: 3: 100%|██████████| 3/3 [00:02<00:00, 1.53it/s]
Conclusion
结论
You can mostly get it to work fine with plain tqdm. But if tqdm_notebookworks for you, just use that (but then you'd probably not read that far).
大多数情况下,您可以使用普通的 tqdm 使其正常工作。但是如果tqdm_notebook适合你,就使用它(但你可能不会读那么远)。
回答by Shritam Kumar Mund
Most of the answers are outdated now. Better if you import tqdmcorrectly.
大多数答案现在已经过时了。如果正确导入tqdm 会更好。
from tqdm import tqdm_notebook as tqdm
回答by Julio Cezar Silva
If the other tips here don't work and - just like me - you're using the pandas
integration through progress_apply
, you can let tqdm
handle it:
如果这里的其他提示不起作用,并且 - 就像我一样 - 您正在使用pandas
集成 through progress_apply
,您可以让tqdm
处理它:
from tqdm.autonotebook import tqdm
tqdm.pandas()
df.progress_apply(row_function, axis=1)
The main point here lies in the tqdm.autonotebook
module. As stated in their instructions for use in IPython Notebooks, this makes tqdm
choose between progress bar formats used in Jupyter notebooks and Jupyter consoles - for a reason still lacking further investigations on my side, the specific format chosen by tqdm.autonotebook
works smoothly in pandas
, while all others didn't, for progress_apply
specifically.
这里的重点在于tqdm.autonotebook
模块。正如他们在 IPython Notebooks 中使用的说明中所述,这使得tqdm
在 Jupyter notebooks 和 Jupyter 控制台中使用的进度条格式之间进行选择 - 由于我方面仍然缺乏进一步调查的原因,选择的特定格式tqdm.autonotebook
在pandas
. '不,progress_apply
具体来说。
回答by mjarosie
回答by James Owers
None of the above works for me. I find that running to following sorts this issue after error (It just clears all the instances of progress bars in the background):
以上都不适合我。我发现在错误后运行到以下排序这个问题(它只是清除后台进度条的所有实例):
from tqdm import tqdm
# blah blah your code errored
tqdm._instances.clear()
回答by Kranthi Kumar Valaboju
Use tqdm_notebook
使用 tqdm_notebook
from tqdm import tqdm_notebook as tqdm
从 tqdm 导入 tqdm_notebook 作为 tqdm
x=[1,2,3,4,5]
x=[1,2,3,4,5]
for i in tqdm(len(x)):
对于 tqdm(len(x)) 中的 i:
print(x[i])
回答by Nebulo
For everyone who is on windows and couldn't solve the duplicating bars issue with any of the solutions mentioned here. I had to install the colorama
package as stated in tqdm's known issueswhich fixed it.
对于使用 Windows 并且无法使用此处提到的任何解决方案解决重复条问题的每个人。我必须colorama
按照tqdm修复它的已知问题中的说明安装该软件包。
pip install colorama
Try it with this example:
用这个例子试试:
from tqdm import tqdm
from time import sleep
for _ in tqdm(range(5), "All", ncols = 80, position = 0):
for _ in tqdm(range(100), "Sub", ncols = 80, position = 1, leave = False):
sleep(0.01)
Which will produce something like:
这将产生类似的东西:
All: 60%|████████████████████████ | 3/5 [00:03<00:02, 1.02s/it]
Sub: 50%|██████████████████▌ | 50/100 [00:00<00:00, 97.88it/s]