Python Display progress bar implementation method

Source: Internet
Author: User

Principle of progress bar implementation

What is the difference between a progress bar and a regular print?
The answer is that print prints a \ n, a newline, so that the cursor moves to the beginning of the next line, and then the output, which is still retained by the STDOUT output, and ensures that we see the latest output below.
Progress bar Otherwise, we must again in situ output in order to ensure that he is a progress bar, otherwise the line is called a progress bar?

The easiest way to implement the progress bar is to move the cursor to the beginning of the line at the end of the output, and continue to output a longer progress bar there to achieve a new, longer progress bar to the old short cover, forming the animation effect.

Ii. Methods of implementation

1. \ r Escape Implementation

The escape character \ R can move the cursor to the beginning of the line without wrapping, and the escape character \ n Moves the cursor to the beginning of the line and wraps. In Python, output stdout (standard output) can be used Sys.stdout.write for example:

The code is as follows Copy Code
#!/usr/bin/env python
#-*-Coding=utf-8-*-
#Using GPL v2
"""
Usage:
Just A Template
"""
From __future__ Import Division
Import Sys,time
j = ' # '
if __name__ = = ' __main__ ':
For I in Range (1,61):
j + = ' # '
Sys.stdout.write (str ((I/60) *100)) + '% | | ' +j+ '-> ' + "\ r")
Sys.stdout.flush ()
Time.sleep (0.5)
Print

2, \b Escape Implementation method

The second idea is to use the escape character \b Escape character \b is the backspace key, that is, the output of the cursor back to the grid, so you can not use + =, for example:

The code is as follows Copy Code
#!/usr/bin/env python
#-*-Coding=utf-8-*-
#Using GPL v2
"""
Usage:
Just A Template
"""
From __future__ Import Division
Import Sys,time
if __name__ = = ' __main__ ':
For I in Range (1,61):
Sys.stdout.write (' # ' + '-> ' + "\b\b")
Sys.stdout.flush ()
Time.sleep (0.5)
Print

3. ProgressBar Module Realization Method

Python is a more powerful and convenient place, is a variety of other people do the class library. We just need to import it through imports. Here you can use the ProgressBar module (currently the latest version is 2.3 development version), the effect is as follows:

The version of stable version 2.2 is used in the figure above. The four examples are shown in the following code (which can be found in the module's progressbar.py file):

The code is as follows Copy Code
def example1 ():
Widgets = [' Test: ', percentage (), ', Bar (Marker=rotatingmarker ()),
", ETA (),", Filetransferspeed ()]
Pbar = ProgressBar (Widgets=widgets, maxval=10000000). Start ()
For I in Range (1000000):
# do something
Pbar.update (10*i+1)
Pbar.finish ()
Print
def example2 ():
Class Crazyfiletransferspeed (Filetransferspeed):
"It ' s bigger between percent"
def update (self, pbar):
If < Pbar.percentage () < 80:
Return ' bigger now ' + filetransferspeed.update (self,pbar)
Else
Return Filetransferspeed.update (Self,pbar)
Widgets = [Crazyfiletransferspeed (), ' <<< ', Bar (), ' >>> ', percentage (), ', ETA ()]
Pbar = ProgressBar (widgets=widgets, maxval=10000000)
# maybe do something
Pbar.start ()
For I in Range (2000000):
# do something
Pbar.update (5*i+1)
Pbar.finish ()
Print
Def example3 ():
Widgets = [Bar (' > '), ', ETA (), ', Reversebar (' < ')]
Pbar = ProgressBar (Widgets=widgets, maxval=10000000). Start ()
For I in Range (1000000):
# do something
Pbar.update (10*i+1)
Pbar.finish ()
Print
Def example4 ():
Widgets = [' Test: ', percentage (), ', ',
Bar (marker= ' 0 ', left= ' [', right= ']),
", ETA (),", Filetransferspeed ()]
Pbar = ProgressBar (widgets=widgets, maxval=500)
Pbar.start ()
For I in Range (100,500+1,50):
Time.sleep (0.2)
Pbar.update (i)
Pbar.finish ()
Print

4. Progressive Module

The progressive module is available only for versions above python2.7. The effect is as follows:

The installation method for the progressive module is as follows:

  code is as follows copy code

Pip Install Progressive
or
git clone https://github.com/hfaran/progressive.git
CD progressive
python setup.py Install

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.