我如何实现进度条
How do I implement a progress bar
如何在 jupyter-notebook 中实现进度条?
我已经这样做了:
count = 0
max_count = 100
bar_width = 40
while count <= max_count:
time.sleep(.1)
b = bar_width * count / max_count
l = bar_width - b
print '\r' + u"\u2588" * b + '-' * l,
count += 1
当我可以访问打印内容的循环时,这很棒。但是有人知道 运行 某种异步进度条的巧妙方法吗?
看看这个开源小部件:log-process
这是一个解决方案(遵循this)。
from ipywidgets import IntProgress
from IPython.display import display
import time
max_count = 100
f = IntProgress(min=0, max=max_count) # instantiate the bar
display(f) # display the bar
count = 0
while count <= max_count:
f.value += 1 # signal to increment the progress bar
time.sleep(.1)
count += 1
如果循环中改变的值是 float
而不是 int
,您可以使用 ipwidgets.FloatProgress
代替。
你可以试试tqdm。示例代码:
# pip install tqdm
from tqdm import tqdm_notebook
# works on any iterable, including cursors.
# for iterables with len(), no need to specify 'total'.
for rec in tqdm_notebook(items,
total=total,
desc="Processing records"):
# any code processing the elements in the iterable
len(rec.keys())
我已经使用 ipywidgets 和线程实现了方法调用期间的进度条,请参见下面的示例
import threading
import time
import ipywidgets as widgets
def method_I_want_progress_bar_for():
progress = widgets.FloatProgress(value=0.0, min=0.0, max=1.0)
finished = False
def work(progress):#method local to this method
total = 200
for i in range(total):
if finished != True:
time.sleep(0.2)
progress.value = float(i+1)/total
else:
progress.value = 200
break
thread = threading.Thread(target=work, args=(progress,))
display(progress)
#start the progress bar thread
thread.start()
#Whatever code you want to run async
finished = True #To set the process bar to 100% and exit the thread
2020 年 8 月,log-process widget is no longer an appropriate method to apply as it has been integrated into tqdm。第一个教程示例(使用新的 API)在 VSCode 中工作 out-of-the-box,我怀疑它在 Jupyter 中也能很好地工作。
from tqdm import tqdm
for i in tqdm(range(10)):
pass
2021 年 2 月更新:
Tqdm 尝试检测您是否在笔记本中,并相应地尝试 select 适当类型的进度条。它通过一个模块 tqdm.auto
来实现。我观察到在 VSCode 笔记本中 tqdm.auto
select 是 command-line 变体(如上所示)而不是 tqdm.notebook
变体的情况。
这通常没有不良影响,但有时会导致极其冗长的滚动输出。我在 VSCode noteboks 中训练 tensorflow 模型时看到过这种情况。因此,我建议:
from tqdm import tqdm
- 在库代码或脚本中
from tqdm.notebook import tqdm
- 在笔记本中
另一个使用 tqdm 的进度条示例
from tqdm import tqdm
my_list = list(range(100))
with tqdm(total=len(my_list)) as pbar:
for x in my_list:
pbar.update(1)
如何在 jupyter-notebook 中实现进度条?
我已经这样做了:
count = 0
max_count = 100
bar_width = 40
while count <= max_count:
time.sleep(.1)
b = bar_width * count / max_count
l = bar_width - b
print '\r' + u"\u2588" * b + '-' * l,
count += 1
当我可以访问打印内容的循环时,这很棒。但是有人知道 运行 某种异步进度条的巧妙方法吗?
看看这个开源小部件:log-process
这是一个解决方案(遵循this)。
from ipywidgets import IntProgress
from IPython.display import display
import time
max_count = 100
f = IntProgress(min=0, max=max_count) # instantiate the bar
display(f) # display the bar
count = 0
while count <= max_count:
f.value += 1 # signal to increment the progress bar
time.sleep(.1)
count += 1
如果循环中改变的值是 float
而不是 int
,您可以使用 ipwidgets.FloatProgress
代替。
你可以试试tqdm。示例代码:
# pip install tqdm
from tqdm import tqdm_notebook
# works on any iterable, including cursors.
# for iterables with len(), no need to specify 'total'.
for rec in tqdm_notebook(items,
total=total,
desc="Processing records"):
# any code processing the elements in the iterable
len(rec.keys())
我已经使用 ipywidgets 和线程实现了方法调用期间的进度条,请参见下面的示例
import threading
import time
import ipywidgets as widgets
def method_I_want_progress_bar_for():
progress = widgets.FloatProgress(value=0.0, min=0.0, max=1.0)
finished = False
def work(progress):#method local to this method
total = 200
for i in range(total):
if finished != True:
time.sleep(0.2)
progress.value = float(i+1)/total
else:
progress.value = 200
break
thread = threading.Thread(target=work, args=(progress,))
display(progress)
#start the progress bar thread
thread.start()
#Whatever code you want to run async
finished = True #To set the process bar to 100% and exit the thread
2020 年 8 月,log-process widget is no longer an appropriate method to apply as it has been integrated into tqdm。第一个教程示例(使用新的 API)在 VSCode 中工作 out-of-the-box,我怀疑它在 Jupyter 中也能很好地工作。
from tqdm import tqdm
for i in tqdm(range(10)):
pass
2021 年 2 月更新:
Tqdm 尝试检测您是否在笔记本中,并相应地尝试 select 适当类型的进度条。它通过一个模块 tqdm.auto
来实现。我观察到在 VSCode 笔记本中 tqdm.auto
select 是 command-line 变体(如上所示)而不是 tqdm.notebook
变体的情况。
这通常没有不良影响,但有时会导致极其冗长的滚动输出。我在 VSCode noteboks 中训练 tensorflow 模型时看到过这种情况。因此,我建议:
from tqdm import tqdm
- 在库代码或脚本中from tqdm.notebook import tqdm
- 在笔记本中
另一个使用 tqdm 的进度条示例
from tqdm import tqdm
my_list = list(range(100))
with tqdm(total=len(my_list)) as pbar:
for x in my_list:
pbar.update(1)