Progress bars are a great way to visually indicate progress in your Python programs, especially during tasks that take a significant amount of time. For example, they are commonly used during a for loop that processes large datasets, while downloading files from the internet or even when an application is starting up to indicate loading progress. Let’s now explore the different methods to create progress bars efficiently.
Using tqdm
tqdm is one of the most popular and easiest libraries for showing progress bars in Python. It works well with loops and gives a neat, real-time progress bar in your terminal.
Python
from tqdm import tqdm
import time
for i in tqdm(range(100)):
time.sleep(0.05) # Simulating a task
Output
Using tqdmExplanation: This code runs a loop 100 times and tqdm adds a progress bar to show how far along it is. Each loop pauses briefly using time.sleep(0.05) to mimic a task taking time. As it runs, the progress bar updates in real time, giving you a clear visual of the task’s progress.
Using rich.progress
rich is a modern Python library for beautiful terminal output, including progress bars with colors and animations. It makes your console output visually appealing.
Python
from rich.progress import Progress
import time
with Progress() as p:
t = p.add_task("Processing...", total=100)
while not p.finished:
p.update(t, advance=1)
time.sleep(0.05)
Output
Using rich.progressExplanation: rich.progress create a colorful progress bar for a 100-step task. It updates the bar step-by-step with a short pause time.sleep(0.05) to simulate work, providing a smooth, real-time view of progress until completion.
Using alive_progress
alive_progress adds a fun twist to progress bars by showing animated bars and spinners. It's very user-friendly and adds a bit of life to long-running tasks.
Python
from alive_progress import alive_bar
import time
with alive_bar(100) as bar:
for i in range(100):
time.sleep(0.05)
bar()
Output
Using alive_progressExplanation: alive_progress show an animated progress bar. It runs a 100-step loop with a brief pause time.sleep(0.05) to simulate work, updating the progress bar after each iteration.
Using progressbar2
progressbar2 is a more traditional progress bar library that gives you a classic loading bar feel. It's a solid choice and has been around for a while.
Python
import progressbar
import time
b = progressbar.ProgressBar(maxval=100)
b.start()
for i in range(100):
time.sleep(0.05)
b.update(i + 1)
b.finish()
Output
Using progressbar2Explanation: progressbar2 library display a terminal progress bar for a 100-step task. It starts the bar, simulates work with a short pause in each loop iteration, updates the progress with b.update(i + 1) and finishes cleanly with b.finish().
Related articles
Similar Reads
Interesting Facts About Python Python is a high-level, general-purpose programming language that is widely used for web development, data analysis, artificial intelligence and more. It was created by Guido van Rossum and first released in 1991. Python's primary focus is on simplicity and readability, making it one of the most acc
7 min read
Running Python program in the background Let us see how to run a Python program or project in the background i.e. the program will start running from the moment device is on and stop at shut down or when you close it. Just run one time to assure the program is error-bug free One way is to use pythonw, pythonw is the concatenation of python
3 min read
Python Event-Driven Programming Event-driven programming is a powerful paradigm used in Python for building responsive and scalable applications. In this model, the flow of the program is driven by events such as user actions, system notifications, or messages from other parts of the program. In this article, we will learn about e
3 min read
Python Program for Compound Interest Let us discuss the formula for compound interest. The formula to calculate compound interest annually is given by: A = P(1 + R/100) t Compound Interest = A - P Where, A is amount P is the principal amount R is the rate and T is the time spanFind Compound Interest with Python Python3 # Python3 progra
3 min read
Output of Python programs | Set 8 Prerequisite - Lists in Python Predict the output of the following Python programs. Program 1 Python list = [1, 2, 3, None, (1, 2, 3, 4, 5), ['Geeks', 'for', 'Geeks']] print len(list) Output: 6Explanation: The beauty of python list datatype is that within a list, a programmer can nest another list,
3 min read
Python Glossary Python is a beginner-friendly programming language, widely used for web development, data analysis, automation and more. Whether you're new to coding or need a quick reference, this glossary provides clear, easy-to-understand definitions of essential Python termsâlisted alphabetically for quick acce
5 min read
Prefix sum list-Python The task of creating a prefix sum list in Python involves iterating over a sequence of numbers and calculating the cumulative sum at each index. For example, with a list of numbers a = [3, 4, 1, 7, 9, 1], the task is to compute the cumulative sum at each index, resulting in a prefix sum list like [3
3 min read
Python Fundamentals Coding Practice Problems Welcome to this article on Python basic problems, featuring essential exercises on coding, number swapping, type conversion, conditional statements, loops and more. These problems help beginners build a strong foundation in Python fundamentals and problem-solving skills. Letâs start coding!Python Ba
1 min read
How to Add Two Numbers in Python The task of adding two numbers in Python involves taking two input values and computing their sum using various techniques . For example, if a = 5 and b = 7 then after addition, the result will be 12.Using the "+" Operator+ operator is the simplest and most direct way to add two numbers . It perform
4 min read
Python program to find Cumulative sum of a list Calculating the cumulative sum of a list means finding the running total of the elements as we move through the list. In this article, we will explore How to find the cumulative sum of a list. Using itertools.accumulate()This is the most efficient method for calculating cumulative sums. itertools mo
3 min read