【问题标题】:How do I implement a progress bar如何实现进度条
【发布时间】:2016-08-09 23:07:46
【问题描述】:

如何在 jupyter-notebook 中实现进度条?

我已经这样做了:

count = 0
max_count = 100
bar_width = 40
while count <= max_count:
    time.sleep(.1)
    b = bar_width * count / max_count
    l = bar_width - b
    print '\r' + u"\u2588" * b + '-' * l,
    count += 1

当我可以访问一个循环来打印内容时,这很棒。但是有没有人知道有什么聪明的方法可以异步运行某种进度条?

【问题讨论】:

    标签: jupyter-notebook


    【解决方案1】:

    这是一个解决方案(遵循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
    

    如果循环中更改的值是 float 而不是 int,则可以改用 ipwidgets.FloatProgress

    【讨论】:

    • 非常方便。但是如果你要增加一个整数,为什么不使用 IntProgress 呢?
    • 可能主要是无知! :)
    【解决方案2】:

    看看这个开源小部件:log-process

    【讨论】:

      【解决方案3】:

      你可以试试tqdm。示例代码:

      # 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())
      

      演示: https://youtu.be/T0gmQDgPtzY

      【讨论】:

      • 我想要这个,但我希望它只与常规 tqdm 一起工作,这样我就可以导入我的代码并在本机或在 Jupyter Notebook 中运行它。
      • @EricHansen,试试:from tqdm.auto import tqdm。它将尝试自动检测您是在笔记本中运行,还是在文本控制台中运行。
      • 截至 2021 年 7 月,tqdm.auto 仍然无法检测您是否在 vscode 笔记本中运行,并将使用默认控制台输出。如果您在 vscode 中运行并遇到此错误,请改用tqdm.notebook
      【解决方案4】:

      在 2020 年 8 月,log-process 小部件不再适合应用,因为它已集成到 tqdm 中。第一个教程示例(使用新的 API)在 VSCode 中开箱即用,我怀疑它在 Jupyter 中也能很好地工作。

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

      2021 年 2 月更新:

      Tqdm 尝试检测您是否在笔记本中,并尝试相应地选择适当类型的进度条。它通过模块tqdm.auto 来实现。我观察到tqdm.auto 在 VSCode 笔记本中选择命令行变体(如上所示)而不是tqdm.notebook 变体的情况。

      这通常没有不良影响,但偶尔会导致非常冗长的滚动输出。我在 VSCode 笔记本中训练 tensorflow 模型时看到了这种情况。因此,我建议:

      1. from tqdm import tqdm - 在库代码或脚本中
      2. from tqdm.notebook import tqdm - 在笔记本中

      【讨论】:

      • 这真的很好,不需要额外的代码行,谢谢!
      • 从 tqdm.notebook 导入 tqdm。我在单元格下方有进度条,并且在进度条下方有打印。这是完美的!!!
      【解决方案5】:

      我在方法调用期间使用了 ipywidgets 和线程来实现进度条,请参见下面的示例

      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
      

      【讨论】:

        【解决方案6】:

        另一个使用 tqdm 的进度条示例

            from tqdm import tqdm
            my_list = list(range(100))
            with tqdm(total=len(my_list)) as pbar:
              for x in my_list:
                  pbar.update(1)
        

        【讨论】:

          猜你喜欢
          • 2021-11-19
          • 2022-01-11
          • 1970-01-01
          • 1970-01-01
          • 2010-11-18
          • 2010-11-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多