ฉันเขียนสิ่งนี้เมื่อไม่นานมานี้และมีความสุขกับมันมาก อย่าลังเลที่จะใช้มัน
ใช้เวลาindexและtotalและเป็นทางเลือกtitleหรือbar_length. เมื่อเสร็จแล้วให้แทนที่แก้วชั่วโมงด้วยเครื่องหมายถูก
⏳ Calculating: [████░░░░░░░░░░░░░░░░░░░░░] 18.0% done
✅ Calculating: [█████████████████████████] 100.0% done
ฉันรวมตัวอย่างที่สามารถเรียกใช้เพื่อทดสอบได้
import sys
import time
def print_percent_done(index, total, bar_len=50, title='Please wait'):
'''
index is expected to be 0 based index.
0 <= index < total
'''
percent_done = (index+1)/total*100
percent_done = round(percent_done, 1)
done = round(percent_done/(100/bar_len))
togo = bar_len-done
done_str = '█'*int(done)
togo_str = '░'*int(togo)
print(f'\t⏳{title}: [{done_str}{togo_str}] {percent_done}% done', end='\r')
if round(percent_done) == 100:
print('\t✅')
r = 50
for i in range(r):
print_percent_done(i,r)
time.sleep(.02)
ฉันยังมีเวอร์ชันที่มีแถบความคืบหน้าที่ตอบสนองขึ้นอยู่กับความกว้างของเทอร์มินัลที่ใช้shutil.get_terminal_size()หากเป็นที่สนใจ