78

I am trying to use a progress bar in a python script that I have since I have a for loop that takes quite a bit of time to process. I have looked at other explanations on here already but I am still confused. Here is what my for loop looks like in my script:

for member in members:
    url = "http://api.wiki123.com/v1.11/member?id="+str(member) 
    header = {"Authorization": authorization_code}
    api_response = requests.get(url, headers=header)
    member_check = json.loads(api_response.text)
    member_status = member_check.get("response") 

I have read a bit about using the progressbar library but my confusion lies in where I have to put the code to support a progress bar relative to my for loop I have included here.

2
  • I've published a new kind of progress bar, which you can print, see throughput and eta, even pause it, besides the very cool animations! Please take a look: github.com/rsalmei/alive-progress !alive-progress
    – rsalmei
    Commented Aug 23, 2019 at 6:45
  • This post is awesome because I think all the following answers are useful and it just depends on your need.
    – Balive13
    Commented Aug 15, 2022 at 12:07

7 Answers 7

152

Using tqdm:

from tqdm import tqdm

for member in tqdm(members):
    # current contents of your for loop

tqdm() takes members and iterates over it, but each time it yields a new member (between each iteration of the loop), it also updates a progress bar on your command line. That makes this actually quite similar to Matthias' solution (printing stuff at the end of each loop iteration), but the progressbar update logic is nicely encapsulated inside tqdm.

1
  • That was hard to search because my stupid brain assumed that for i in tqdm(range(10)) weren't allowing for for in iteration.
    –  vrnvorona
    Commented Dec 8, 2020 at 21:05
51

To show the progress bar:

from tqdm import tqdm

for x in tqdm(my_list):
    # do something with x

#### In case using with enumerate:
for i, x in enumerate( tqdm(my_list) ):
    # do something with i and x

enter image description here

Some notes on the attached picture:

49%: It already finished 49% of the whole process

979/2000: Working on the 979th element/iteration, out of 2000 elements/iterations

01:50: It's been running for 1 minute and 50 seconds

01:55: Estimated time left to run

8.81 it/s: On average, it processes 8.81 elements per second

8

I think this could be most elegantly be solved in this manner:

import progressbar

bar = progressbar.ProgressBar(maxval=len(members)).start()

for idx, member in enumerate(members):
    ...
    bar.update(idx)
3
  • As many would suspect, the first line results in an error: NameError: name 'progressbar' is not defined Commented Jan 7, 2022 at 0:27
  • @WinEunuuchs2Unix you need to install the package and include it.
    – nammerkage
    Commented Jun 9, 2022 at 7:16
  • @nammerkage It was a subtle hint for author to add import into code. I wrote my own progress bar not requiring imports BTW: stackoverflow.com/a/70586588/6929343 Commented Jun 9, 2022 at 23:12
5

The basic idea of a progress bar from a loop is to insert points within the loop to update the progress bar. An example would be something like this:

membersProcessed = 0
for member in members:
    url = "http://api.wiki123.com/v1.11/member?id="+str(member) 
    header = {"Authorization": authorization_code}
    api_response = requests.get(url, headers=header)
    member_check = json.loads(api_response.text)
    member_status = member_check.get("response") 

    membersProcessed += 1
    print 'Progress: {}/{} members processed'.format(membersProcessed, len(members))

Maybe this helps.

And you could include a more detailed one by adding points after certain commands within the for loop as well.

1
  • When running the code on jupyter notebook, the browser will break if it has a lot of outputs Commented Feb 16, 2022 at 8:23
4

The rich module has also a progress bar that can be included in your for loop:

import time  # for demonstration only
from rich.progress import track

members = ['Liam', 'Olivia', 'Noah', 'Emma', 'Oliver', 'Charlotte']  # for demonstration only

for member in track(members):
    # your code here
    print(member)  # for demonstration only
    time.sleep(1.5)  # for demonstration only

Note: time is only used to get the delay for the screenshot.

Here's a screenshot from within the run: enter image description here

1

Or you can use this (can be used for any situation):

for i in tqdm (range (1), desc="Loading..."):
    for member in members:
       url = "http://api.wiki123.com/v1.11/member?id="+str(member) 
       header = {"Authorization": authorization_code}
       api_response = requests.get(url, headers=header)
       member_check = json.loads(api_response.text)
       member_status = member_check.get("response") 
1

Here Is a simple progress bar code with 0 imports

#!/usr/bin/python3
def progressbar(current_value,total_value,bar_lengh,progress_char): 
    percentage = int((current_value/total_value)*100)                                                # Percent Completed Calculation 
    progress = int((bar_lengh * current_value ) / total_value)                                       # Progress Done Calculation 
    loadbar = "Progress: [{:{len}}]{}%".format(progress*progress_char,percentage,len = bar_lengh)    # Progress Bar String
    print(loadbar, end='\r')                                                                         # Progress Bar Output

if __name__ == "__main__":
    the_list = range(1,301) 
    for i in the_list:
        progressbar(i,len(the_list),30,'■')
    print("\n")

enter image description here You can implement it like so in your case.

def progressbar(current_value,total_value,bar_lengh,progress_char): 
    percentage = int((current_value/total_value)*100)                                            
    progress = int((bar_lengh * current_value ) / total_value)                                   
    loadbar = "Progress: [{:{len}}]{}%".format(progress*progress_char,percentage,len = bar_lengh)
    print(loadbar, end='\r')                                                                     

for member in members:
    url = "http://api.wiki123.com/v1.11/member?id="+str(member) 
    header = {"Authorization": authorization_code}
    api_response = requests.get(url, headers=header)
    member_check = json.loads(api_response.text)
    member_status = member_check.get("response")
    progressbar(member,len(members),30,'■')         # (Current iteration, Total iterations, Progress bar lenght, Progress bar character)
print("\n")                                         # New Line After Progress Bar Completed 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.