1

After looking at the following question with regards to Python Progress bars, I am still confused: Python Progress Bar

I am new to Python and I am trying to add a progress bar to a script I created which runs a for loop. The loop takes a significant amount of time to process so I want the progress bar to show when the loop is running. The part that confuses me is, do I have set to up a loop inside of a loop in order to implement a progress bar? Here is a sample of my for loop from my code:

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") 

Do I need to include the code to add a progress bar INTO this loop or does it have to be included outside of the loop? Thanks.

Update: I mention the progressbar library here but I am open to suggestions of other libraries.

Community
  • 1
  • 1
user7681184
  • 893
  • 2
  • 12
  • 24

4 Answers4

0

A simple solution would be to print '-' at the end of the loop. This prints one hyphen for each iteration of the for loop.

If you instead wanted to always print out 100 hyphens, 1 hyphen for each 'percent' then you might do something like print '-'*(100/len(members)), with an extra check for adding extra dashes to the last iteration for an even 100.

avodo
  • 73
  • 6
  • Thanks for the response but I don't think this is the info I was looking for. More specifically, I was interested in using some type of progress bar library (like "progressbar" for example) but I am confused where the code needs to be inserted relative to my FOR loop that I have included as an example above. Thanks. – user7681184 Apr 05 '17 at 23:03
0

It's actually very easy with the enlighten library. Just create a progress bar before the loop and update it with each instance of the loop. Using the manager, you could have additional progress bars for other parts of your program.

import enlighten

manager = enlighten.get_manager()
pbar = manager.counter(total=len(members), desc="Checking status", unit='members')

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")
    pbar.update()
aviso
  • 2,371
  • 1
  • 14
  • 15
0

This can be easily done with atpbar as long as members in your code has a length.

You only need to wrap members with atpbar:

from atpbar import atpbar

⋮

for member in atpbar(members):
    ⋮
Tai Sakuma
  • 91
  • 2
  • 3
0

You could use the tqdm library.

You only need to modify your loop to include a counter, see this example:

from tqdm import tqdm
import time
startTime = time.clock()
totalCount = len(members)
for index, member in enumerate(members):
    stopTime = time.clock()
    statusBarText = tqdm.format_meter(index + 1,
                                      totalCount,
                                      stopTime - startTime,
                                      ncols=80,  # prints text 80 characters wide
                                      ascii=False)
    print(statusBarText, '\b' * 81, end='')
    startTime = time.clock()
    ... rest of the code in your loop ...