How do I implement a progress bar

后端 未结 5 397
遇见更好的自我
遇见更好的自我 2020-12-24 08:19

How do I implement a progress bar in jupyter-notebook?

I\'ve done this:

count = 0
max_count = 100
bar_width = 40
while count <= max_count:
    tim         


        
相关标签:
5条回答
  • 2020-12-24 08:27

    You can try tqdm. Example code:

    # pip install tqdm
    from tqdm import tqdm_notebook
    
    # works on any iterable, including cursors. 
    # for iterables with len(), no need to specify 'total'.
    for rec in tqdm_notebook(items, 
                             total=total, 
                             desc="Processing records"):
        # any code processing the elements in the iterable
        len(rec.keys())
    

    Demo: https://youtu.be/T0gmQDgPtzY

    0 讨论(0)
  • 2020-12-24 08:37

    Here is a solution (following this).

    from ipywidgets import IntProgress
    from IPython.display import display
    import time
    
    max_count = 100
    
    f = IntProgress(min=0, max=max_count) # instantiate the bar
    display(f) # display the bar
    
    count = 0
    while count <= max_count:
        f.value += 1 # signal to increment the progress bar
        time.sleep(.1)
        count += 1
    

    If the value that's changing in the loop is a float instead of an int, you can use ipwidgets.FloatProgress instead.

    0 讨论(0)
  • 2020-12-24 08:40

    Take a look at this open-source widget: log-process

    0 讨论(0)
  • I have used ipywidgets and threading to implement a progress bar for the duration of a method call, see the example below

    import threading
    import time
    import ipywidgets as widgets
    def method_I_want_progress_bar_for():   
        progress = widgets.FloatProgress(value=0.0, min=0.0, max=1.0)
        finished = False
        def work(progress):#method local to this method
            total = 200
            for i in range(total):
                if finished != True:
                    time.sleep(0.2)
                    progress.value = float(i+1)/total
                else:
                    progress.value = 200
                    break
    
        thread = threading.Thread(target=work, args=(progress,))
        display(progress)
        #start the progress bar thread
        thread.start()
    
        #Whatever code you want to run async
    
        finished = True #To set the process bar to 100% and exit the thread
    
    0 讨论(0)
  • 2020-12-24 08:48

    In August 2020, the log-process widget is no longer an appropriate method to apply as it has been integrated into tqdm. The first tutorial example (using the new API) works out-of-the-box in VSCode, and I suspect it will work nicely in Jupyter as well.

    from tqdm import tqdm
    for i in tqdm(range(10)):
        pass
    

    0 讨论(0)
提交回复
热议问题