当前位置:网站首页>The progress bar written in Python is so wonderful~

The progress bar written in Python is so wonderful~

2020-11-08 11:42:00 Watch the corner.

I recommended one to you before windows Artifact , There is a small function in it, which is the progress bar of life , You can see 2020 Only half of the year's progress is left , So how much progress do you have left ?

see , This article introduces the current situation of 6 This is a more commonly used progress bar , So that everyone can intuitively see the latest progress of script running

  • 1. General progress bar
  • 2. With a time bar
  • 3.tpdm Progress bar
  • 4.progress Progress bar
  • 5.alive_progress Progress bar
  • 6. Visual progress bar
  • 7. Progress bar ?

1. General progress bar

In code iteration, you can do your own statistical calculation , And use the formatted string to output the code running progress

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 a time bar

Import time Module to calculate the running time of the code , In addition, the code iteration progress uses the formatted string to output the code running progress

import time
scale = 50
print(" Execution start , 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"+" end of execution , Fortunately ".center(scale // 2,"-"))

  

3.tpdm Progress bar

This is a toolkit for generating progress bars , have access to pip Download at 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 iterative object in it 
for i in tqdm(range(1, 500)):
   #  Simulate your mission 
   sleep(0.01)
sleep(0.5)

If you need the source code, you can join me Python Learning circles to get :1156465813

4.progress Progress bar

You just need to define the number of iterations 、 The progress type of each iteration can be informed , Specific code examples are 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 Progress bar

seeing the name of a thing one thinks of its function , This library can make the progress bar vivid , It's more animated than the progress bar we've seen before , Need to use pip Download , The code case is 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

I'd like to recommend my own Python Develop learning groups :1156465813, All of us are learning Python Developed , If you are learning Python , Welcome to , Everyone is a software development party , Share dry goods irregularly ( Only Python Software development ), Including a copy I made myself 2020 Abreast of the times Python Advanced materials and advanced development tutorials , You are welcome to go further Python Little buddy !

use PySimpleGUI Get a graphical progress bar , We can add a simple line of code , Get the graphical progress bar in the command line script , Is also used pip Download , 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)

  

( End )

The following is useless , For this blog to be crawled by search engines
(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)
python What is it Zero basis to learn python How long will it take? python Why is it called a reptile
python Novice reptile tutorial python Crawler universal code python How do reptiles make money
python Basic course Web crawler python python Classic examples of reptiles
python Reptiles
(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)
The above is useless , For this blog to be crawled by search engines

 

版权声明
本文为[Watch the corner.]所创,转载请带上原文链接,感谢