How to display the text progress bar in Python on the Console,

Source: Internet
Author: User

How to display the text progress bar in Python on the Console,

Progress bar implementation principle

What is the difference between a progress bar and a general print?
The answer is that print will output A \ n, that is, a line break, so that the cursor moves to the beginning of the next line, and then the output, the previous output through stdout is still retained, and make sure that we can see the latest output results below.
Otherwise, we must output the progress bar again to ensure that it is a progress bar. Otherwise, how can we call it a progress bar after a line break?
The simplest way to implement the progress bar is to move the cursor to the beginning of the line after the output is complete and continue to output a longer progress bar there. The new longer progress bar overwrites the old one, the animation effect is formed.

When using Python to process time-consuming tasks, you often want to know the current processing progress of the task. At this time, you need to constantly print the progress information of the task in the task. This is generally the case:

Def process_mission ():
"Task handling method """
# Here is the task processing process
Print ('currently processed to item [% d] '% count)
# Here is the task processing process

This method outputs a bunch of information similar to the following in the window:

Currently processed to 1st items
Currently processed to 2nd items
Currently processed to 3rd items
Currently processed to 4th items
Currently processed to 5th items
....

This information may be very long, and it may be very fast to output, so that it cannot be clearly viewed (when the output is very fast ).

At this time, we look forward to the following features:

[===========================] 25.60%

However, if print is used purely, the effect will be the same as the previous one. Full Screen is like this.

How can I display the progress bar correctly?

Python provides a module called progressbar. After using this module, you only need to display the progress as follows:

Import progressbar # define a progress bar first # http://blog.useasp.net/pbar = progressbar. progressBar (maxval = 100, \ widgets = [progressbar. bar ('=', '[', ']'), '', \ progressbar. percentage ()]) for I in xrange (100): # update progress bar pbar. update (I + 1) pbar. finish ()

# OK. This is the end.

Is it easy to display the progress bar? Unfortunately, this python module is not default, but needs to be installed:

Pip install progressbar

If pip is not installed, refer to here.

For a person with a simple progress bar, installing a python package seems to be a little too dynamic, so you can write one by yourself.

The following is a similar progress bar written by Mitchell, which is easy to use without installation packages.

# Before using this method, first import # from _ future _ import division # import math # import sys ### blog.useasp.net # def progressbar (cur, total ): percent = '{:. 2% }'. format (cur/total) sys. stdout. write ('\ R') sys. stdout. write ("[%-50 s] % s" % ('=' * int (math. floor (cur * 50/total), percent) sys. stdout. flush ()

Note:

Cur, total: the current value and the total value. Cur keeps approaching total During task processing until the two are equal and the task ends.

Note that the two may be integer, so division should be introduced.

You only need to call progressbar to output the progress bar on the console.

# Call method example progressbar (2,100) progressbar (3.9, 10) progressbar (3283,273 79)

At this point, we have completed a progress bar that will be dynamically updated on the console.

The above section describes how to display the text progress bar in Python on the Console. I hope it will be helpful to you.

Articles you may be interested in:
  • How to display progress bars in Python
  • How does Python call the command line progress bar?
  • How to output a progress bar in the python Console
  • Progress bar in the python Console
  • Python implements the console progress bar Function

Related Article

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.