如何获取进度更新并变成进度条?
How to take progress update and turn into a progress bar?
我正在使用语音转文本 API,在等待结果的同时,我可以打印当前进度百分比:
while not operation.done():
print(operation.metadata.progress_percent)
time.sleep(5)
print(operation.metadata.progress_percent)
得到这个:
0
2
3
...
如何把它变成进度条?
您可以用回车 return、\r
结束打印语句的行以覆盖当前行,并使用它来连续 "update" 进度条。
打印进度条的例子:
import time
def progress_bar(progress, total=100, bar_length=20):
# How much of the bar should be filled
fill = progress * bar_length // total
# How much of the bar should be empty
empty = bar_length - fill
print(f'[{"#"*fill}{"-"*empty}]', end='\r')
# Go to next line if done
if progress == total:
print()
# Emulate a percentage progress
for i in range(0, 101):
progress_bar(i)
time.sleep(0.05)
这将输出一个看起来像这样的柱状图
[##############------]
我正在使用语音转文本 API,在等待结果的同时,我可以打印当前进度百分比:
while not operation.done():
print(operation.metadata.progress_percent)
time.sleep(5)
print(operation.metadata.progress_percent)
得到这个:
0
2
3
...
如何把它变成进度条?
您可以用回车 return、\r
结束打印语句的行以覆盖当前行,并使用它来连续 "update" 进度条。
打印进度条的例子:
import time
def progress_bar(progress, total=100, bar_length=20):
# How much of the bar should be filled
fill = progress * bar_length // total
# How much of the bar should be empty
empty = bar_length - fill
print(f'[{"#"*fill}{"-"*empty}]', end='\r')
# Go to next line if done
if progress == total:
print()
# Emulate a percentage progress
for i in range(0, 101):
progress_bar(i)
time.sleep(0.05)
这将输出一个看起来像这样的柱状图
[##############------]