No Result
View All Result
DevRescue
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
DevRescue
Home Blog Python

Cool Progress Bar in Python

by Khaleel O.
June 19, 2022
in Python
Reading Time: 4 mins read
A A
python progress bar
python progress bar

Hi there! Let’s create cool progress bars using Python. We will be using Python 3.8.10 on Windows 10. Let’s go! ✨⚡🔥

First, let’s install the alive-progress library with this simple command:

pip install alive-progress

Now that we have installed the library, let’s write our code:

from alive_progress import alive_bar
import time

steps = [1,2,3,4,5,6,7]
with alive_bar(total=len(steps), title="Operation X", theme='smooth') as bar:
    for s in steps:
        bar.text = f"Working on Step {s}..."
        time.sleep(2)
        bar()

Let’s explain what is happening here:

  1. We import our alive-progress library and the time library obviously.
  2. We define a list steps which contains 7 numbers, 1 to 7 inclusive. This list represents a list of steps of some arbitrary operation. We will use our progress bar to show the status of the completion of each step. In practice, this will be a typical use case for a progress bar.
  3. We initialize our bar with the method alive_bar() which was imported earlier.
    • parameter total is the total length of progress bar, equal to the number of steps in our operation
    • parameter title is the name of our progress bar, it will be displayed to the left of the progress bar
    • parameter theme is the look of the progress bar, can be one of 4 values: smooth, classic, scuba, musical
  4. We define a for loop that will iterate through the steps list and increment the progress bar as each step s is completed. In this instance we are simply calling sleep() to pause the program for 2 seconds for each step. We are also displaying a line of text indicating what step we are currently working on.
  5. The function bar() will update the displayed progress bar with the new state after each iteration of the inner for loop.

When the program executes, we should the see the following if all goes well:

python progress bar
python progress bar demo 1

Let’s do another example:

from alive_progress import alive_bar
import time

steps = [1,2,3,4,5,6,7]
with alive_bar(total=len(steps), title="Operation X", bar='halloween',spinner='elements',dual_line=True) as bar:
    for s in steps:
        bar.text = f"Working on Step {s}..."
        time.sleep(2)
        bar()

This is a more fancy example where we added two new parameters to the alive_bar() method: bar and spinner. These parameters add styles to the progress bar. The bar is the actual progress bar itself and the spinner is the little animation to the right of the progress bar that indicates work is taking place in between the ticks of the bar. When the above code executes we will get the following:

python progress bar
python progress bar demo 2

If we want a demo of all the potential themes, bars or spinners available we can execute the following code:

from alive_progress.styles import show_bars, show_spinners, show_themes

#Hit Ctrl-C to continue
show_bars()
show_spinners()
show_themes()

When the above code executes you will see an impressive display of all the bars, spinners and themes the library has to offer. You will need to hit Ctrl-C on your keyboard to continue.

python progress bar
alive-progress showcase

So there you have it! A cool progress bar in Python for your projects. Thanks for reading! 👌👌👌

Previous Post

Extract Pixel RGB Color Values In Python

Next Post

Webpage Screenshot with Python

Khaleel O.

Khaleel O.

I love to share, educate and help developers. I have 14+ years experience in IT. Currently transitioning from Systems Administration to DevOps. Avid reader, intellectual and dreamer. Enter Freely, Go safely, And leave something of the happiness you bring.

Related Posts

Python

Python Fibonacci Recursive Solution

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Fibonacci Recursive Solution. Let's go! 🔥🔥🔥 The Fibonacci sequence is a series of numbers in which...

Read moreDetails
Python

Python Slice String List Tuple

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Slice string list tuple how-to tutorial. Let's go! 🔥🔥🔥 In Python, a slice is a feature...

Read moreDetails
Python

Python Blowfish Encryption Example

by Khaleel O.
January 14, 2024
0
0

Let's do a Python Blowfish Encryption example. Let's go! 🔥 🔥 Blowfish is a symmetric-key block cipher algorithm designed for...

Read moreDetails
Python

Python Deque Methods

by Khaleel O.
January 14, 2024
0
0

In this post we'll list Python Deque Methods. Ready? Let's go! 🔥🔥🔥 A deque (double-ended queue) in Python is a...

Read moreDetails

DevRescue © 2021 All Rights Reserved. Privacy Policy. Cookie Policy

Manage your privacy

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}
Manage your privacy
To provide the best experiences, DevRescue.com will use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}
No Result
View All Result
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us

DevRescue © 2022 All Rights Reserved Privacy Policy