We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN 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.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `dask/diagnostics/progress.py`
Content:
```
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 FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

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

{"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"}], "after_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        sys.stdout.flush()\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"}]}