Python Progress Bar

后端 未结 30 2825
礼貌的吻别
礼貌的吻别 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:56

    With tqdm (conda install tqdm or pip install tqdm) you can add a progress meter to your loops in a second:

    from time import sleep
    from tqdm import tqdm
    for i in tqdm(range(10)):
        sleep(3)
    
     60%|██████    | 6/10 [00:18<00:12,  0.33 it/s]
    

    Also, there is a notebook version:

    from tqdm.notebook import tqdm
    for i in tqdm(range(100)):
        sleep(3)
    

    You can use tqdm.auto instead of tqdm.notebook to work in both a terminal and notebooks.

    tqdm.contrib contains some helper functions to do things like enumerate, map, and zip. There are concurrent maps in tqdm.contrib.concurrent.

    You can even get progress sent to your phone after disconnecting from a jupyter notebook using tqdm.contrib.telegram or tqdm.contrib.discord.

提交回复
热议问题