Progress bar for a "for" loop in Python script
Question:
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.
Answers:
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.
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
.
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)
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
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
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")
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 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")
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
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.
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.
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
.
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)
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
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
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")
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 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")
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