Using Python Progress Bar Libraries: Progress, tqdm, alive‑progress, and PySimpleGUI
This article introduces four popular Python progress‑bar libraries—Progress, tqdm, alive‑progress, and PySimpleGUI—showing how to add simple command‑line and graphical progress indicators with just a few lines of code and providing usage examples and visual demos.
Many developers avoid progress bars, assuming they add unnecessary complexity, but a progress indicator can be implemented with only a few lines of code. This guide demonstrates how to add progress bars in both command‑line scripts and PySimpleGUI graphical interfaces using four common Python libraries.
Progress
The first library presented is Progress . You define the number of iterations, choose a bar type, and update the bar on each loop.
<code>import time
from progress.bar import IncrementalBar
mylist = [1,2,3,4,5,6,7,8]
bar = IncrementalBar('Countdown', max=len(mylist))
for item in mylist:
bar.next()
time.sleep(1)
bar.finish()
</code>The resulting bar looks like this:
Progress supports several bar styles; you can select a different visual format if you prefer.
Documentation: https://pypi.org/project/progress/1.5/
tqdm
The next library is tqdm , which provides a similar interface with a few extra options.
<code>import time
from tqdm import tqdm
mylist = [1,2,3,4,5,6,7,8]
for i in tqdm(mylist):
time.sleep(1)
</code>Its visual output appears as follows:
Documentation: https://tqdm.github.io/
alive‑progress
alive‑progress adds animated, more lively progress bars.
<code>from alive_progress import alive_bar
import time
mylist = [1,2,3,4,5,6,7,8]
with alive_bar(len(mylist)) as bar:
for i in mylist:
bar()
time.sleep(1)
</code>Example output:
More features and usage details are available at the project repository: https://github.com/rsalmei/alive-progress
PySimpleGUI (Command‑line and GUI)
For a graphical progress bar, PySimpleGUI can be used. A simple one‑line progress meter can be added to a script:
<code>import PySimpleGUI as sg
import time
mylist = [1,2,3,4,5,6,7,8]
for i, item in enumerate(mylist):
sg.one_line_progress_meter('This is my progress meter!', i+1, len(mylist), '-key-')
time.sleep(1)
</code>And a full‑featured GUI with a progress bar and output window can be built as follows:
<code>import PySimpleGUI as sg
import time
mylist = [1,2,3,4,5,6,7,8]
progressbar = [[sg.ProgressBar(len(mylist), orientation='h', size=(51,10), key='progressbar')]]
outputwin = [[sg.Output(size=(78,20))]]
layout = [
[sg.Frame('Progress', layout=progressbar)],
[sg.Frame('Output', layout=outputwin)],
[sg.Submit('Start'), sg.Cancel()]
]
window = sg.Window('Custom Progress Meter', layout)
progress_bar = window['progressbar']
while True:
event, values = window.read(timeout=10)
if event == 'Cancel' or event is None:
break
elif event == 'Start':
for i, item in enumerate(mylist):
print(item)
time.sleep(1)
progress_bar.UpdateBar(i+1)
window.close()
</code>These examples show that adding a progress bar to a Python script is straightforward and greatly improves visibility of long‑running tasks.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.