Skip to content

Commit 5e150aa

Browse files
authored
Add HTML reprs to some scheduler classes (#4795)
1 parent a2a0feb commit 5e150aa

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

distributed/scheduler.py

+36-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import html
23
import inspect
34
import itertools
45
import json
@@ -732,13 +733,22 @@ def clean(self):
732733
return ws
733734

734735
def __repr__(self):
735-
return "<Worker %r, name: %s, memory: %d, processing: %d>" % (
736+
return "<WorkerState %r, name: %s, memory: %d, processing: %d>" % (
736737
self._address,
737738
self._name,
738739
len(self._has_what),
739740
len(self._processing),
740741
)
741742

743+
def _repr_html_(self):
744+
text = (
745+
f"<b>WorkerState: </b> {html.escape(self._address)} "
746+
f'<font style="color: var(--jp-ui-font-color2, gray)">name: </font>{self.name} '
747+
f'<font style="color: var(--jp-ui-font-color2, gray)">memory: </font>{len(self._has_what)} '
748+
f'<font style="color: var(--jp-ui-font-color2, gray)">processing: </font>{len(self._processing)}'
749+
)
750+
return text
751+
742752
@ccall
743753
@exceptval(check=False)
744754
def identity(self) -> dict:
@@ -1549,7 +1559,19 @@ def set_nbytes(self, nbytes: Py_ssize_t):
15491559
self._nbytes = nbytes
15501560

15511561
def __repr__(self):
1552-
return "<Task %r %s>" % (self._key, self._state)
1562+
return "<TaskState %r %s>" % (self._key, self._state)
1563+
1564+
def _repr_html_(self):
1565+
color = (
1566+
"var(--jp-error-color0, red)"
1567+
if self._state == "erred"
1568+
else "var(--jp-ui-font-color0, black)"
1569+
)
1570+
text = f'<b>TaskState: </b> <font style="color: {color}">{self._state} </font>'
1571+
if self._state == "memory":
1572+
text += f'<font style="color: var(--jp-ui-font-color2, gray)">nbytes: </font>{format_bytes(self._nbytes)} '
1573+
text += f'<font style="color: var(--jp-ui-font-color2, gray)">key: </font>{html.escape(self._key)}'
1574+
return text
15531575

15541576
@ccall
15551577
def validate(self):
@@ -3579,11 +3601,22 @@ def __init__(
35793601

35803602
def __repr__(self):
35813603
parent: SchedulerState = cast(SchedulerState, self)
3582-
return '<Scheduler: "%s" processes: %d cores: %d>' % (
3604+
return '<Scheduler: "%s" workers: %d cores: %d, tasks: %d>' % (
35833605
self.address,
35843606
len(parent._workers),
35853607
parent._total_nthreads,
3608+
len(parent._tasks),
3609+
)
3610+
3611+
def _repr_html_(self):
3612+
parent: SchedulerState = cast(SchedulerState, self)
3613+
text = (
3614+
f"<b>Scheduler: </b>{html.escape(self.address)} "
3615+
f'<font color="gray">workers: </font>{len(parent._workers)} '
3616+
f'<font color="gray">cores: </font>{parent._total_nthreads} '
3617+
f'<font color="gray">tasks: </font>{len(parent._tasks)}'
35863618
)
3619+
return text
35873620

35883621
def identity(self, comm=None):
35893622
""" Basic information about ourselves and our cluster """

0 commit comments

Comments
 (0)