Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create progress bar wrapper to use with dask #279

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions tqdm/__init__.py
Expand Up @@ -26,3 +26,9 @@ def tnrange(*args, **kwargs): # pragma: no cover
"""
from ._tqdm_notebook import tnrange as _tnrange
return _tnrange(*args, **kwargs)


try:
import ._dask as dask
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not valid Python 2.

except ImportError:
pass
29 changes: 29 additions & 0 deletions tqdm/_dask.py
@@ -0,0 +1,29 @@
from dask.callbacks import Callback
from ._tqdm import tqdm


class ProgressBar(Callback):
def __init__(self, tclass=tqdm, *targs, **tkwargs):
"""
Progress bar class to use with dask.

Parameters
----------
tclass : class or instance of tqdm (e.g. tqdm_notebook())
targs, tkwargs : arguments for the tqdm instance
"""
if not isinstance(tclass, type):
tclass = type(class)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class -> tclass

self._tclass = tclass
self._targs = targs
self._tkwargs = tkwargs

def _start_state(self, dsk, state):
self._tqdm = self.tclass(total=sum(len(state[k]) for k in ['ready', 'waiting', 'running', 'finished']),
*self.targs, **self.tkwargs)

def _posttask(self, key, result, dsk, state, worker_id):
self._tqdm.update(1)

def _finish(self, dsk, state, errored):
self._tqdm.close()