Python Progress Bar

后端 未结 30 2251
礼貌的吻别
礼貌的吻别 2020-11-22 06:13

How do I use a progress bar when my script is doing some task that is likely to take time?

For example, a function which takes some time to complete and returns

30条回答
  •  花落未央
    2020-11-22 06:57

    You can also use enlighten. The main advantage is you can log at the same time without overwriting your progress bar.

    import time
    import enlighten
    
    manager = enlighten.Manager()
    pbar = manager.counter(total=100)
    
    for num in range(1, 101):
        time.sleep(0.05)
        print('Step %d complete' % num)
        pbar.update()
    

    It also handles multiple progress bars.

    import time
    import enlighten
    
    manager = enlighten.Manager()
    odds = manager.counter(total=50)
    evens = manager.counter(total=50)
    
    for num in range(1, 101):
        time.sleep(0.05)
        if num % 2:
            odds.update()
        else:
            evens.update()
    

提交回复
热议问题