SoFunction
Updated on 2024-11-16

Python one line of code to quickly implement the program progress bar example

introductory

Have you ever encountered any of the following problems while writing code?

  • Already written a program and want to see the progress of its execution?
  • When writing code to batch process files, how do I show how many files are being processed now?

👆 A progress bar as shown above is one of the best solutions, how to quickly add a progress bar to a program without modifying the original code?

Today we're going to learn one of the easiest ways ~ the

1, first on the code

Download third-party libraries for progress bars.

pip install poprogress

Use this library to quickly make progress bars

from poprogress import simple_progress
a_list = [1, 2, 3, 4, 5, 6, 7, 8]*100000000
for a in simple_progress(a_list。desc='This parameter is a description of the progress bar and can be left out'):
    pass

The effect is as follows👇.

2. Instructions for use

If you are careful, you must have noticed that this progress bar code, does no harm to the code we usually write.

Normally we might just looplistand the progress bar is putting thislistexpense or outlaysimple_progress()Wrap it up in a loop.

# The usual code:
for i in list:
  pass
# Add the code for the progress bar
for i in simple_progress(list):
  pass

So if you've already written code and want to add a progress bar, also just put theforWhat comes after the loop is directly followed by thesimple_progress()Just wrap it up ~ the programmer doesn't need to make any changes.

Isn't it very simple?

3. Principle of realization

Students who want to learn more about it can look at the source code and study how it is implemented:

⭐GitHub:/CoderWanFen…

Above is Python one line of code to quickly achieve the program progress bar example of the details, more information about Python program progress bar please pay attention to my other related articles!