Skip to content

Commit

Permalink
Add HTML reprs for Client.who_has and Client.has_what (dask#4853)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtomlinson authored May 27, 2021
1 parent 26a1da0 commit 2e01873
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
5 changes: 3 additions & 2 deletions distributed/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
)
from .diagnostics.plugin import UploadFile, WorkerPlugin, _get_worker_plugin_name
from .metrics import time
from .objects import HasWhat, WhoHas
from .protocol import to_serialize
from .protocol.pickle import dumps, loads
from .publish import Datasets
Expand Down Expand Up @@ -3202,7 +3203,7 @@ def who_has(self, futures=None, **kwargs):
keys = list(map(stringify, {f.key for f in futures}))
else:
keys = None
return self.sync(self.scheduler.who_has, keys=keys, **kwargs)
return WhoHas(self.sync(self.scheduler.who_has, keys=keys, **kwargs))

def has_what(self, workers=None, **kwargs):
"""Which keys are held by which workers
Expand Down Expand Up @@ -3236,7 +3237,7 @@ def has_what(self, workers=None, **kwargs):
workers = list(workers)
if workers is not None and not isinstance(workers, (tuple, list, set)):
workers = [workers]
return self.sync(self.scheduler.has_what, workers=workers, **kwargs)
return HasWhat(self.sync(self.scheduler.has_what, workers=workers, **kwargs))

def processing(self, workers=None):
"""The tasks currently running on each worker
Expand Down
67 changes: 67 additions & 0 deletions distributed/objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""This file contains custom objects.
These are mostly regular objects with more useful _repr_ and _repr_html_ methods."""


class HasWhat(dict):
"""A dictionary of all workers and which keys that worker has."""

def _repr_html_(self):
rows = ""

for worker, keys in sorted(self.items()):
summary = ""
for key in keys:
summary += f"""<tr><td>{key}</td></tr>"""

rows += f"""<tr>
<td>{worker}</td>
<td>{len(keys)}</td>
<td>
<details>
<summary style='display:list-item'>Expand</summary>
<table>
{summary}
</table>
</details>
</td>
</tr>"""

output = f"""
<table>
<tr>
<th>Worker</th>
<th>Key count</th>
<th>Key list</th>
</tr>
{rows}
</table>
"""

return output


class WhoHas(dict):
"""A dictionary of all keys and which workers have that key."""

def _repr_html_(self):
rows = ""

for title, keys in sorted(self.items()):
rows += f"""<tr>
<td>{title}</td>
<td>{len(keys)}</td>
<td>{", ".join(keys)}</td>
</tr>"""

output = f"""
<table>
<tr>
<th>Key</th>
<th>Copies</th>
<th>Workers</th>
</tr>
{rows}
</table>
"""

return output

0 comments on commit 2e01873

Please sign in to comment.