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 = ""+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.

1

5 Answers

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

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

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 = ""+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

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

bar = progressbar.ProgressBar(maxval=len(members)).start() for idx, member in enumerate(members): ... bar.update(idx) 
3

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

for i in tqdm (range (1), desc="Loading..."): for member in members: url = ""+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") 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy