Skip to content

Commit 2a82405

Browse files
authored
Merge pull request #1079 from tqdm/dask
2 parents 4ff7753 + 7c42d80 commit 2a82405

10 files changed

Lines changed: 122 additions & 21 deletions

File tree

.meta/.readme.rst

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ Changelog
140140
The list of all changes is available either on GitHub's Releases:
141141
|GitHub-Status|, on the
142142
`wiki <https://github.com/tqdm/tqdm/wiki/Releases>`__, or on the
143-
`website <https://tqdm.github.io/releases/>`__.
143+
`website <https://tqdm.github.io/releases>`__.
144144

145145

146146
Usage
@@ -460,7 +460,10 @@ Submodules
460460
"""`rich.progress` version."""
461461
462462
class tqdm.keras.TqdmCallback(keras.callbacks.Callback):
463-
"""`keras` callback for epoch and batch progress."""
463+
"""Keras callback for epoch and batch progress."""
464+
465+
class tqdm.dask.TqdmCallback(dask.callbacks.Callback):
466+
"""Dask callback for task progress."""
464467
465468
466469
``contrib``
@@ -470,8 +473,8 @@ The ``tqdm.contrib`` package also contains experimental modules:
470473

471474
- ``tqdm.contrib.itertools``: Thin wrappers around ``itertools``
472475
- ``tqdm.contrib.concurrent``: Thin wrappers around ``concurrent.futures``
473-
- ``tqdm.contrib.discord``: Posts to `Discord <https://discord.com/>`__ bots
474-
- ``tqdm.contrib.telegram``: Posts to `Telegram <https://telegram.org/>`__ bots
476+
- ``tqdm.contrib.discord``: Posts to `Discord <https://discord.com>`__ bots
477+
- ``tqdm.contrib.telegram``: Posts to `Telegram <https://telegram.org>`__ bots
475478
- ``tqdm.contrib.bells``: Automagically enables all optional features
476479

477480
* ``auto``, ``pandas``, ``discord``, ``telegram``
@@ -829,6 +832,24 @@ A ``keras`` callback is also available:
829832
830833
model.fit(..., verbose=0, callbacks=[TqdmCallback()])
831834
835+
Dask Integration
836+
~~~~~~~~~~~~~~~~
837+
838+
A ``dask`` callback is also available:
839+
840+
.. code:: python
841+
842+
from tqdm.dask import TqdmCallback
843+
844+
with TqdmCallback(desc="compute"):
845+
...
846+
arr.compute()
847+
848+
# or use callback globally
849+
cb = TqdmCallback(desc="global")
850+
cb.register()
851+
arr.compute()
852+
832853
IPython/Jupyter Integration
833854
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
834855

README.rst

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ Changelog
140140
The list of all changes is available either on GitHub's Releases:
141141
|GitHub-Status|, on the
142142
`wiki <https://github.com/tqdm/tqdm/wiki/Releases>`__, or on the
143-
`website <https://tqdm.github.io/releases/>`__.
143+
`website <https://tqdm.github.io/releases>`__.
144144

145145

146146
Usage
@@ -679,7 +679,10 @@ Submodules
679679
"""`rich.progress` version."""
680680
681681
class tqdm.keras.TqdmCallback(keras.callbacks.Callback):
682-
"""`keras` callback for epoch and batch progress."""
682+
"""Keras callback for epoch and batch progress."""
683+
684+
class tqdm.dask.TqdmCallback(dask.callbacks.Callback):
685+
"""Dask callback for task progress."""
683686
684687
685688
``contrib``
@@ -689,8 +692,8 @@ The ``tqdm.contrib`` package also contains experimental modules:
689692

690693
- ``tqdm.contrib.itertools``: Thin wrappers around ``itertools``
691694
- ``tqdm.contrib.concurrent``: Thin wrappers around ``concurrent.futures``
692-
- ``tqdm.contrib.discord``: Posts to `Discord <https://discord.com/>`__ bots
693-
- ``tqdm.contrib.telegram``: Posts to `Telegram <https://telegram.org/>`__ bots
695+
- ``tqdm.contrib.discord``: Posts to `Discord <https://discord.com>`__ bots
696+
- ``tqdm.contrib.telegram``: Posts to `Telegram <https://telegram.org>`__ bots
694697
- ``tqdm.contrib.bells``: Automagically enables all optional features
695698

696699
* ``auto``, ``pandas``, ``discord``, ``telegram``
@@ -1048,6 +1051,24 @@ A ``keras`` callback is also available:
10481051
10491052
model.fit(..., verbose=0, callbacks=[TqdmCallback()])
10501053
1054+
Dask Integration
1055+
~~~~~~~~~~~~~~~~
1056+
1057+
A ``dask`` callback is also available:
1058+
1059+
.. code:: python
1060+
1061+
from tqdm.dask import TqdmCallback
1062+
1063+
with TqdmCallback(desc="compute"):
1064+
...
1065+
arr.compute()
1066+
1067+
# or use callback globally
1068+
cb = TqdmCallback(desc="global")
1069+
cb.register()
1070+
arr.compute()
1071+
10511072
IPython/Jupyter Integration
10521073
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10531074

benchmarks/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ These benchmarks serve two purposes:
1111
- [`progressbar2`](https://pypi.org/project/progressbar2)
1212
- [`alive-progress`](https://pypi.org/project/alive-progress)
1313

14-
Performance graphs are available at <https://tqdm.github.io/tqdm/>
14+
Performance graphs are available at <https://tqdm.github.io/tqdm>
1515

1616
## Running
1717

environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ dependencies:
2525
- flake8-comprehensions
2626
- coverage
2727
# extras
28+
- dask # dask
2829
- matplotlib # gui
2930
- numpy # pandas, keras, contrib.tenumerate
3031
- pandas

tests/tests_dask.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from __future__ import division
2+
3+
from time import sleep
4+
5+
from .tests_tqdm import importorskip, mark
6+
7+
pytestmark = mark.slow
8+
9+
10+
def test_dask(capsys):
11+
"""Test tqdm.dask.TqdmCallback"""
12+
ProgressBar = importorskip('tqdm.dask').TqdmCallback
13+
dask = importorskip('dask')
14+
15+
schedule = [dask.delayed(sleep)(i / 10) for i in range(5)]
16+
with ProgressBar():
17+
dask.compute(schedule)
18+
_, err = capsys.readouterr()
19+
assert '5/5' in err

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ passenv=TOXENV CI GITHUB_* CODECOV_* COVERALLS_* CODACY_* HOME
2727
deps=
2828
{[core]deps}
2929
cython
30+
dask[delayed]
3031
matplotlib
3132
numpy
3233
pandas

tqdm/dask.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from __future__ import absolute_import
2+
3+
from functools import partial
4+
5+
from dask.callbacks import Callback
6+
7+
from .auto import tqdm as tqdm_auto
8+
9+
__author__ = {"github.com/": ["casperdcl"]}
10+
__all__ = ['TqdmCallback']
11+
12+
13+
class TqdmCallback(Callback):
14+
"""Dask callback for task progress."""
15+
def __init__(self, start=None, pretask=None, tqdm_class=tqdm_auto,
16+
**tqdm_kwargs):
17+
"""
18+
Parameters
19+
----------
20+
tqdm_class : optional
21+
`tqdm` class to use for bars [default: `tqdm.auto.tqdm`].
22+
tqdm_kwargs : optional
23+
Any other arguments used for all bars.
24+
"""
25+
super(TqdmCallback, self).__init__(start=start, pretask=pretask)
26+
if tqdm_kwargs:
27+
tqdm_class = partial(tqdm_class, **tqdm_kwargs)
28+
self.tqdm_class = tqdm_class
29+
30+
def _start_state(self, _, state):
31+
self.pbar = self.tqdm_class(total=sum(
32+
len(state[k]) for k in ['ready', 'waiting', 'running', 'finished']))
33+
34+
def _posttask(self, *_, **__):
35+
self.pbar.update()
36+
37+
def _finish(self, *_, **__):
38+
self.pbar.close()
39+
40+
def display(self):
41+
"""Displays in the current cell in Notebooks."""
42+
container = getattr(self.bar, 'container', None)
43+
if container is None:
44+
return
45+
from .notebook import display
46+
display(container)

tqdm/gui.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,8 @@
2424

2525

2626
class tqdm_gui(std_tqdm): # pragma: no cover
27-
"""
28-
Experimental Matplotlib GUI version of tqdm!
29-
"""
30-
27+
"""Experimental Matplotlib GUI version of tqdm!"""
3128
# TODO: @classmethod: write() on GUI?
32-
3329
def __init__(self, *args, **kwargs):
3430
from collections import deque
3531

tqdm/keras.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818

1919
class TqdmCallback(keras.callbacks.Callback):
20-
"""`keras` callback for epoch and batch progress"""
20+
"""Keras callback for epoch and batch progress."""
2121
@staticmethod
2222
def bar2callback(bar, pop=None, delta=(lambda logs: 1)):
2323
def callback(_, logs=None):
@@ -98,7 +98,7 @@ def on_train_end(self, *_, **__):
9898
self.epoch_bar.close()
9999

100100
def display(self):
101-
"""displays in the current cell in Notebooks"""
101+
"""Displays in the current cell in Notebooks."""
102102
container = getattr(self.epoch_bar, 'container', None)
103103
if container is None:
104104
return

tqdm/rich.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,8 @@ def render(self, task):
7474

7575

7676
class tqdm_rich(std_tqdm): # pragma: no cover
77-
"""
78-
Experimental rich.progress GUI version of tqdm!
79-
"""
80-
77+
"""Experimental rich.progress GUI version of tqdm!"""
8178
# TODO: @classmethod: write()?
82-
8379
def __init__(self, *args, **kwargs):
8480
"""
8581
This class accepts the following parameters *in addition* to

0 commit comments

Comments
 (0)