prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  ProgressBar is not visible in the notebook
  The `ProgressBar` doesn't update itself during execution while in the notebook.  Afterwards the full bar will pop up but it doesn't give you any cues during execution.
  
  
  </issue>
  <code>
  [start of dask/diagnostics/progress.py]
  1 from __future__ import division
  2 import sys
  3 import threading
  4 import time
  5 from timeit import default_timer
  6 
  7 from ..core import istask
  8 from .core import Diagnostic
  9 
  10 
  11 def format_time(t):
  12     """Format seconds into a human readable form.
  13 
  14     >>> format_time(10.4)
  15     '10.4s'
  16     >>> format_time(1000.4)
  17     '16min 40.4s'
  18     """
  19     m, s = divmod(t, 60)
  20     h, m = divmod(m, 60)
  21     if h:
  22         return '{0:2.0f}hr {1:2.0f}min {2:4.1f}s'.format(h, m, s)
  23     elif m:
  24         return '{0:2.0f}min {1:4.1f}s'.format(m, s)
  25     else:
  26         return '{0:4.1f}s'.format(s)
  27 
  28 
  29 class ProgressBar(Diagnostic):
  30     """A progress bar for dask.
  31 
  32     Can be used as a context manager around dask computations.
  33 
  34     Examples
  35     --------
  36     >>> with ProgressBar():    # doctest: +SKIP
  37     ...     out = res.compute()
  38     [########################################] | 100% Completed | 10.4 s
  39     """
  40 
  41     def __init__(self, width=40, dt=0.1):
  42         self._width = width
  43         self._dt = dt
  44 
  45     def _start(self, dsk, state):
  46         self._ntasks = len([k for (k, v) in dsk.items() if istask(v)])
  47         self._ndone = 0
  48         self._update_rate = max(1, self._ntasks // self._width)
  49         self._start_time = default_timer()
  50         # Start background thread
  51         self._running = True
  52         self._timer = threading.Thread(target=self._timer_func)
  53         self._timer.start()
  54 
  55     def _posttask(self, key, value, dsk, state, id):
  56         self._ndone += 1
  57 
  58     def _finish(self, dsk, state, errored):
  59         self._running = False
  60         self._timer.join()
  61         self._finalize_bar()
  62 
  63     def _timer_func(self):
  64         """Background thread for updating the progress bar"""
  65         while self._running:
  66             self._update_bar()
  67             time.sleep(self._dt)
  68 
  69     def _update_bar(self):
  70         tics = int(self._ndone * self._width / self._ntasks)
  71         bar = '#' * tics
  72         percent = (100 * self._ndone) // self._ntasks
  73         elapsed = format_time(default_timer() - self._start_time)
  74         msg = '\r[{0:<{1}}] | {2}% Completed | {3}'.format(bar, self._width,
  75                                                            percent, elapsed)
  76         sys.stdout.write(msg)
  77         sys.stdout.flush()
  78 
  79     def _finalize_bar(self):
  80         self._update_bar()
  81         sys.stdout.write('\n')
  82         sys.stdout.flush()
  83
  [end of dask/diagnostics/progress.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/dask/diagnostics/progress.py b/dask/diagnostics/progress.py
  --- a/dask/diagnostics/progress.py
  +++ b/dask/diagnostics/progress.py
  @@ -54,6 +54,7 @@
   
       def _posttask(self, key, value, dsk, state, id):
           self._ndone += 1
  +        sys.stdout.flush()
   
       def _finish(self, dsk, state, errored):
           self._running = False
  
verification_info: {"golden_diff": "diff --git a/dask/diagnostics/progress.py b/dask/diagnostics/progress.py\n--- a/dask/diagnostics/progress.py\n+++ b/dask/diagnostics/progress.py\n@@ -54,6 +54,7 @@\n \n     def _posttask(self, key, value, dsk, state, id):\n         self._ndone += 1\n+        sys.stdout.flush()\n \n     def _finish(self, dsk, state, errored):\n         self._running = False\n", "issue": "ProgressBar is not visible in the notebook\nThe `ProgressBar` doesn't update itself during execution while in the notebook.  Afterwards the full bar will pop up but it doesn't give you any cues during execution.\n\n", "before_files": [{"content": "from __future__ import division\nimport sys\nimport threading\nimport time\nfrom timeit import default_timer\n\nfrom ..core import istask\nfrom .core import Diagnostic\n\n\ndef format_time(t):\n    \"\"\"Format seconds into a human readable form.\n\n    >>> format_time(10.4)\n    '10.4s'\n    >>> format_time(1000.4)\n    '16min 40.4s'\n    \"\"\"\n    m, s = divmod(t, 60)\n    h, m = divmod(m, 60)\n    if h:\n        return '{0:2.0f}hr {1:2.0f}min {2:4.1f}s'.format(h, m, s)\n    elif m:\n        return '{0:2.0f}min {1:4.1f}s'.format(m, s)\n    else:\n        return '{0:4.1f}s'.format(s)\n\n\nclass ProgressBar(Diagnostic):\n    \"\"\"A progress bar for dask.\n\n    Can be used as a context manager around dask computations.\n\n    Examples\n    --------\n    >>> with ProgressBar():    # doctest: +SKIP\n    ...     out = res.compute()\n    [########################################] | 100% Completed | 10.4 s\n    \"\"\"\n\n    def __init__(self, width=40, dt=0.1):\n        self._width = width\n        self._dt = dt\n\n    def _start(self, dsk, state):\n        self._ntasks = len([k for (k, v) in dsk.items() if istask(v)])\n        self._ndone = 0\n        self._update_rate = max(1, self._ntasks // self._width)\n        self._start_time = default_timer()\n        # Start background thread\n        self._running = True\n        self._timer = threading.Thread(target=self._timer_func)\n        self._timer.start()\n\n    def _posttask(self, key, value, dsk, state, id):\n        self._ndone += 1\n\n    def _finish(self, dsk, state, errored):\n        self._running = False\n        self._timer.join()\n        self._finalize_bar()\n\n    def _timer_func(self):\n        \"\"\"Background thread for updating the progress bar\"\"\"\n        while self._running:\n            self._update_bar()\n            time.sleep(self._dt)\n\n    def _update_bar(self):\n        tics = int(self._ndone * self._width / self._ntasks)\n        bar = '#' * tics\n        percent = (100 * self._ndone) // self._ntasks\n        elapsed = format_time(default_timer() - self._start_time)\n        msg = '\\r[{0:<{1}}] | {2}% Completed | {3}'.format(bar, self._width,\n                                                           percent, elapsed)\n        sys.stdout.write(msg)\n        sys.stdout.flush()\n\n    def _finalize_bar(self):\n        self._update_bar()\n        sys.stdout.write('\\n')\n        sys.stdout.flush()\n", "path": "dask/diagnostics/progress.py"}]}