Skip to content

Commit

Permalink
[Core] Add HTML reprs for ClientContext and WorkerContext (ray-pr…
Browse files Browse the repository at this point in the history
  • Loading branch information
peytondmurray committed Jul 6, 2022
1 parent 146f65b commit ea47d97
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions python/ray/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ def __getattr__(self, attr):
"timeline",
"util",
"wait",
"widgets",
"LOCAL_MODE",
"SCRIPT_MODE",
"WORKER_MODE",
Expand Down
15 changes: 15 additions & 0 deletions python/ray/_private/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
from ray.util.debug import log_once
from ray.util.scheduling_strategies import PlacementGroupSchedulingStrategy
from ray.util.tracing.tracing_helper import _import_from_string
from ray.widgets import Template

SCRIPT_MODE = 0
WORKER_MODE = 1
Expand Down Expand Up @@ -971,6 +972,20 @@ def disconnect(self):
# Include disconnect() to stay consistent with ClientContext
ray.shutdown()

def _repr_html_(self):
if self.dashboard_url:
dashboard_row = Template("context_dashrow.html.j2").render(
dashboard_url=self.dashboard_url
)
else:
dashboard_row = None

return Template("context.html.j2").render(
python_version=self.python_version,
ray_version=self.ray_version,
dashboard_row=dashboard_row,
)


global_worker = Worker()
"""Worker: The global Worker object for this worker process.
Expand Down
15 changes: 15 additions & 0 deletions python/ray/client_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from ray._private.worker import init as ray_driver_init
from ray.job_config import JobConfig
from ray.util.annotations import Deprecated, PublicAPI
from ray.widgets import Template

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -81,6 +82,20 @@ def _disconnect_with_context(self, force_disconnect: bool) -> None:
# This is only a driver connected to an existing cluster.
ray.shutdown()

def _repr_html_(self):
if self.dashboard_url:
dashboard_row = Template("context_dashrow.html.j2").render(
dashboard_url=self.dashboard_url
)
else:
dashboard_row = None

return Template("context.html.j2").render(
python_version=self.python_version,
ray_version=self.ray_version,
dashboard_row=dashboard_row,
)


@Deprecated
class ClientBuilder:
Expand Down
3 changes: 3 additions & 0 deletions python/ray/widgets/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from ray.widgets.render import Template

__all__ = ["Template"]
38 changes: 38 additions & 0 deletions python/ray/widgets/render.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import pathlib
from typing import List

from ray.util.annotations import DeveloperAPI


@DeveloperAPI
class Template:
"""Class which provides basic HTML templating."""

def __init__(self, file: str):
with open(pathlib.Path(__file__).parent / "templates" / file, "r") as f:
self.template = f.read()

def render(self, **kwargs) -> str:
"""Render an HTML template with the given data.
This is done by replacing instances of `{{ key }}` with `value`
from the keyword arguments.
Returns:
str: HTML template with the keys of the kwargs replaced with corresponding
values.
"""
rendered = self.template
for key, value in kwargs.items():
rendered = rendered.replace("{{ " + key + " }}", value if value else "")
return rendered

@staticmethod
def list_templates() -> List[pathlib.Path]:
"""List the available HTML templates.
Returns:
List[pathlib.Path]: A list of files with .html.j2 extensions inside
./templates/
"""
return (pathlib.Path(__file__).parent / "templates").glob("*.html.j2")
37 changes: 37 additions & 0 deletions python/ray/widgets/templates/context.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<div>
<div style="margin-left: 50px;display: flex;flex-direction: row;align-items: center">
<h3 style="color: var(--jp-ui-font-color0)">Ray</h3>
<svg version="1.1" id="ray" width="3em" viewBox="0 0 144.5 144.6" style="margin-left: 3em;margin-right: 3em">
<g id="layer-1">
<path fill="#00a2e9" class="st0" d="M97.3,77.2c-3.8-1.1-6.2,0.9-8.3,5.1c-3.5,6.8-9.9,9.9-17.4,9.6S58,88.1,54.8,81.2c-1.4-3-3-4-6.3-4.1
c-5.6-0.1-9.9,0.1-13.1,6.4c-3.8,7.6-13.6,10.2-21.8,7.6C5.2,88.4-0.4,80.5,0,71.7c0.1-8.4,5.7-15.8,13.8-18.2
c8.4-2.6,17.5,0.7,22.3,8c1.3,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3
C72.6,62.3,72.4,62.3,72.3,62.3z M115,72.5c0.1,5.4,4.5,9.7,9.8,9.9c5.6-0.2,10-4.8,10-10.4c-0.2-5.4-4.6-9.7-10-9.7
c-5.3-0.1-9.8,4.2-9.9,9.5C115,72.1,115,72.3,115,72.5z M19.5,62.3c-5.4,0.1-9.8,4.4-10,9.8c-0.1,5.1,5.2,10.4,10.2,10.3
c5.6-0.2,10-4.9,9.8-10.5c-0.1-5.4-4.5-9.7-9.9-9.6C19.6,62.3,19.5,62.3,19.5,62.3z M71.8,134.6c5.9,0.2,10.3-3.9,10.4-9.6
c0.5-5.5-3.6-10.4-9.1-10.8c-5.5-0.5-10.4,3.6-10.8,9.1c0,0.5,0,0.9,0,1.4c-0.2,5.3,4,9.8,9.3,10
C71.6,134.6,71.7,134.6,71.8,134.6z"/>
</g>
</svg>
<table>
<tr>
<td style="text-align: left"><b>Python version:</b></td>
<td style="text-align: left"><b>{{ python_version }}</b></td>
</tr>
<tr>
<td style="text-align: left"><b>Ray version:</b></td>
<td style="text-align: left"><b> {{ ray_version }}</b></td>
</tr>
{{ dashboard_row }}
</table>
</div>
</div>
4 changes: 4 additions & 0 deletions python/ray/widgets/templates/context_dashrow.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<tr>
<td style="text-align: left"><b>Dashboard:</b></td>
<td style="text-align: left"><b><a href="{{ dashboard_url }}" target="_blank">{{ dashboard_url }}</a></b></td>
</tr>

0 comments on commit ea47d97

Please sign in to comment.