Python running progress bar sorting

  • 1. Ordinary progress bar

  • 2. With time progress bar

  • 3.tpdm progress bar

  • 4.progress bar

  • 5.alive_progress bar

  • 6. Visual progress bar

1. Ordinary progress bar

In the iterative running of the code, you can carry out statistical calculation by yourself, and use the formatted string to output the running progress of the code

import sys
import time
def progress_bar():
    for i in range(1, 101):
        print("\r", end="")
        print("Download progress: {}%: ".format(i), "" * (i // 2), end="")
        sys.stdout.flush()
        time.sleep(0.05)
progress_bar()

2. With time progress bar

Import the time module to calculate the code running time, add the code iteration progress, and use the format string to output the code running progress

import time
scale = 50
print("When the execution begins, pray for no error".center(scale // 2,"-"))
start = time.perf_counter()
for i in range(scale + 1):
    a = "*" * i
    b = "." * (scale - i)
    c = (i / scale) * 100
    dur = time.perf_counter() - start
    print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,dur),end = "")
    time.sleep(0.1)
print("\n"+"The execution is over. Fortunately".center(scale // 2,"-"))

 

3.tpdm progress bar

This is a toolkit for generating progress bar. You can use pip to download on the terminal. Of course, you can also switch the style of progress bar

from time import sleep
from tqdm import tqdm
# It's the same here, tqdm This is the most commonly used method of this progress bar
# There is an iteratable object in it
for i in tqdm(range(1, 500)):
   # Simulate your task
   sleep(0.01)
sleep(0.5)

4.progress bar

You only need to define the number of iterations and the type of progress bar, and inform the progress bar at each iteration. The specific code case is as follows

import time
from progress.bar import IncrementalBar
mylist = [1,2,3,4,5,6,7,8]
bar = IncrementalBar('Countdown', max = len(mylist))
for item in mylist:
    bar.next()
    time.sleep(1)
    bar.finish()

5.alive_progress bar

As the name suggests, this library can make the progress bar vivid. It has more animation effects than the progress bar we have seen before. You need to download it using pip. The code examples are as follows:

from alive_progress import alive_bar
items = range(100)                  # retrieve your set of items
with alive_bar(len(items)) as bar:   # declare your expected total
    for item in items:               # iterate as usual
        # process each item
        bar()
        time.sleep(0.1)

6. Visual progress bar

Use PySimpleGUI to get the graphical progress bar. We can add a line of simple code to get the graphical progress bar in the command line script. It is also downloaded using pip. The code case is as follows

import PySimpleGUI as sg
import time
mylist = [1,2,3,4,5,6,7,8]
for i, item in enumerate(mylist):
    sg.one_line_progress_meter('This is my progress meter!', i+1, len(mylist), '-key-')
    time.sleep(1)

 

Posted by sniped22 on Sat, 14 May 2022 00:55:52 +0300