Skip to content

Commit

Permalink
Revert "Revert "Add a disconnect button to the context widgets in not…
Browse files Browse the repository at this point in the history
…ebooks (ray-project#34815)" (ray-project#35426)"

This reverts commit 45067ae.
  • Loading branch information
peytondmurray committed May 18, 2023
1 parent 601a3ea commit 54a1890
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 74 deletions.
90 changes: 74 additions & 16 deletions python/ray/_private/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
from ray.util.scheduling_strategies import PlacementGroupSchedulingStrategy
from ray.util.tracing.tracing_helper import _import_from_string
from ray.widgets import Template
from ray.widgets.util import ensure_ipywidgets_dep

SCRIPT_MODE = 0
WORKER_MODE = 1
Expand Down Expand Up @@ -1019,6 +1020,10 @@ class BaseContext(metaclass=ABCMeta):
Base class for RayContext and ClientContext
"""

dashboard_url: Optional[str]
python_version: str
ray_version: str

@abstractmethod
def disconnect(self):
"""
Expand All @@ -1036,6 +1041,73 @@ def __enter__(self):
def __exit__(self):
pass

def _context_table_template(self):
if self.dashboard_url:
dashboard_row = Template("context_dashrow.html.j2").render(
dashboard_url="https://" + self.dashboard_url
)
else:
dashboard_row = None

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

def _repr_html_(self):
return Template("context.html.j2").render(
context_logo=Template("context_logo.html.j2").render(),
context_table=self._context_table_template(),
)

@ensure_ipywidgets_dep("8")
def _get_widget_bundle(self, **kwargs) -> Dict[str, Any]:
"""Get the mimebundle for the widget representation of the context.
Args:
**kwargs: Passed to the _repr_mimebundle_() function for the widget
Returns:
Dictionary ("mimebundle") of the widget representation of the context.
"""
import ipywidgets

disconnect_button = ipywidgets.Button(
description="Disconnect",
disabled=False,
button_style="",
tooltip="Disconnect from the Ray cluster",
layout=ipywidgets.Layout(margin="auto 0px 0px 0px"),
)

def disconnect_callback(button):
button.disabled = True
button.description = "Disconnecting..."
self.disconnect()
button.description = "Disconnected"

disconnect_button.on_click(disconnect_callback)
left_content = ipywidgets.VBox(
[
ipywidgets.HTML(Template("context_logo.html.j2").render()),
disconnect_button,
],
layout=ipywidgets.Layout(),
)
right_content = ipywidgets.HTML(self._context_table_template())
widget = ipywidgets.HBox(
[left_content, right_content], layout=ipywidgets.Layout(width="100%")
)
return widget._repr_mimebundle_(**kwargs)

def _repr_mimebundle_(self, **kwargs):
bundle = self._get_widget_bundle(**kwargs)

# Overwrite the widget html repr and default repr with those of the BaseContext
bundle.update({"text/html": self._repr_html_(), "text/plain": repr(self)})
return bundle


@dataclass
class RayContext(BaseContext, Mapping):
Expand All @@ -1047,10 +1119,10 @@ class RayContext(BaseContext, Mapping):
python_version: str
ray_version: str
ray_commit: str
protocol_version = Optional[str]
address_info: Dict[str, Optional[str]]
protocol_version: Optional[str]

def __init__(self, address_info: Dict[str, Optional[str]]):
super().__init__()
self.dashboard_url = get_dashboard_url()
self.python_version = "{}.{}.{}".format(*sys.version_info[:3])
self.ray_version = ray.__version__
Expand Down Expand Up @@ -1092,20 +1164,6 @@ 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="https://" + 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: 0 additions & 15 deletions python/ray/client_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
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 @@ -86,20 +85,6 @@ 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="https://" + 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
4 changes: 4 additions & 0 deletions python/ray/tests/test_widgets.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from unittest import mock

import pytest
Expand All @@ -8,6 +9,7 @@
@mock.patch("ray.widgets.util.in_notebook")
def test_ensure_notebook_dep_missing(mock_in_notebook, mock_import_module, caplog):
"""Test that missing notebook dependencies trigger a warning."""
caplog.set_level(logging.INFO)

class MockDep:
__version__ = "8.0.0"
Expand All @@ -34,6 +36,7 @@ def dummy_ipython_display(self):
@mock.patch("ray.widgets.util.in_notebook")
def test_ensure_notebook_dep_outdated(mock_in_notebook, mock_import_module, caplog):
"""Test that outdated notebook dependencies trigger a warning."""
caplog.set_level(logging.INFO)

class MockDep:
__version__ = "7.0.0"
Expand All @@ -56,6 +59,7 @@ def dummy_ipython_display():
@mock.patch("ray.widgets.util.in_notebook")
def test_ensure_notebook_valid(mock_in_notebook, mock_import_module, caplog):
"""Test that valid notebook dependencies don't trigger a warning."""
caplog.set_level(logging.INFO)

class MockDep:
__version__ = "8.0.0"
Expand Down
5 changes: 2 additions & 3 deletions python/ray/widgets/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def render(self, **kwargs) -> str:
from the keyword arguments.
Returns:
str: HTML template with the keys of the kwargs replaced with corresponding
HTML template with the keys of the kwargs replaced with corresponding
values.
"""
rendered = self.template
Expand All @@ -34,7 +34,6 @@ 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/
A list of files with .html.j2 extensions inside ../templates/
"""
return (pathlib.Path(__file__).parent / "templates").glob("*.html.j2")
37 changes: 3 additions & 34 deletions python/ray/widgets/templates/context.html.j2
Original file line number Diff line number Diff line change
@@ -1,37 +1,6 @@
<div>
<div class="lm-Widget p-Widget lm-Panel p-Panel jp-Cell-outputWrapper">
<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,1.9,1.3,5.2,3.6,5.6c3.9,0.6,8,0.2,12,0.2c1.8,0,1.9-1.6,2.4-2.8c3.5-7.8,9.7-11.8,18-11.9
c8.2-0.1,14.4,3.9,17.8,11.4c1.3,2.8,2.9,3.6,5.7,3.3c1-0.1,2,0.1,3,0c2.8-0.5,6.4,1.7,8.1-2.7s-2.3-5.5-4.1-7.5
c-5.1-5.7-10.9-10.8-16.1-16.3C84,38,81.9,37.1,78,38.3C66.7,42,56.2,35.7,53,24.1C50.3,14,57.3,2.8,67.7,0.5
C78.4-2,89,4.7,91.5,15.3c0.1,0.3,0.1,0.5,0.2,0.8c0.7,3.4,0.7,6.9-0.8,9.8c-1.7,3.2-0.8,5,1.5,7.2c6.7,6.5,13.3,13,19.8,19.7
c1.8,1.8,3,2.1,5.5,1.2c9.1-3.4,17.9-0.6,23.4,7c4.8,6.9,4.6,16.1-0.4,22.9c-5.4,7.2-14.2,9.9-23.1,6.5c-2.3-0.9-3.5-0.6-5.1,1.1
c-6.7,6.9-13.6,13.7-20.5,20.4c-1.8,1.8-2.5,3.2-1.4,5.9c3.5,8.7,0.3,18.6-7.7,23.6c-7.9,5-18.2,3.8-24.8-2.9
c-6.4-6.4-7.4-16.2-2.5-24.3c4.9-7.8,14.5-11,23.1-7.8c3,1.1,4.7,0.5,6.9-1.7C91.7,98.4,98,92.3,104.2,86c1.6-1.6,4.1-2.7,2.6-6.2
c-1.4-3.3-3.8-2.5-6.2-2.6C99.8,77.2,98.9,77.2,97.3,77.2z M72.1,29.7c5.5,0.1,9.9-4.3,10-9.8c0-0.1,0-0.2,0-0.3
C81.8,14,77,9.8,71.5,10.2c-5,0.3-9,4.2-9.3,9.2c-0.2,5.5,4,10.1,9.5,10.3C71.8,29.7,72,29.7,72.1,29.7z M72.3,62.3
c-5.4-0.1-9.9,4.2-10.1,9.7c0,0.2,0,0.3,0,0.5c0.2,5.4,4.5,9.7,9.9,10c5.1,0.1,9.9-4.7,10.1-9.8c0.2-5.5-4-10-9.5-10.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>
{{ context_logo }}
{{ context_table }}
</div>
</div>
13 changes: 13 additions & 0 deletions python/ray/widgets/templates/context_logo.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="jp-RenderedHTMLCommon" style="display: flex; flex-direction: row;">
<svg viewBox="0 0 567 224" fill="none" xmlns="https://www.w3.org/2000/svg" style="height: 3em;">
<g clip-path="url(#clip0_4338_178347)">
<path d="M341.29 165.561H355.29L330.13 129.051C345.63 123.991 354.21 112.051 354.21 94.2307C354.21 71.3707 338.72 58.1807 311.88 58.1807H271V165.561H283.27V131.661H311.8C314.25 131.661 316.71 131.501 319.01 131.351L341.25 165.561H341.29ZM283.29 119.851V70.0007H311.82C331.3 70.0007 342.34 78.2907 342.34 94.5507C342.34 111.271 331.34 119.861 311.82 119.861L283.29 119.851ZM451.4 138.411L463.4 165.561H476.74L428.74 58.1807H416L367.83 165.561H380.83L392.83 138.411H451.4ZM446.19 126.601H398L422 72.1407L446.24 126.601H446.19ZM526.11 128.741L566.91 58.1807H554.35L519.99 114.181L485.17 58.1807H472.44L514.01 129.181V165.541H526.13V128.741H526.11Z" fill="var(--jp-ui-font-color0)"/>
<path d="M82.35 104.44C84.0187 97.8827 87.8248 92.0678 93.1671 87.9146C98.5094 83.7614 105.083 81.5067 111.85 81.5067C118.617 81.5067 125.191 83.7614 130.533 87.9146C135.875 92.0678 139.681 97.8827 141.35 104.44H163.75C164.476 101.562 165.622 98.8057 167.15 96.2605L127.45 56.5605C121.071 60.3522 113.526 61.6823 106.235 60.3005C98.9443 58.9187 92.4094 54.9203 87.8602 49.0574C83.3109 43.1946 81.0609 35.8714 81.5332 28.4656C82.0056 21.0599 85.1679 14.0819 90.4252 8.8446C95.6824 3.60726 102.672 0.471508 110.08 0.0272655C117.487 -0.416977 124.802 1.86091 130.647 6.4324C136.493 11.0039 140.467 17.5539 141.821 24.8501C143.175 32.1463 141.816 39.6859 138 46.0505L177.69 85.7505C182.31 82.9877 187.58 81.4995 192.962 81.4375C198.345 81.3755 203.648 82.742 208.33 85.3976C213.012 88.0532 216.907 91.9029 219.616 96.5544C222.326 101.206 223.753 106.492 223.753 111.875C223.753 117.258 222.326 122.545 219.616 127.197C216.907 131.848 213.012 135.698 208.33 138.353C203.648 141.009 198.345 142.375 192.962 142.313C187.58 142.251 182.31 140.763 177.69 138L138 177.7C141.808 184.071 143.155 191.614 141.79 198.91C140.424 206.205 136.44 212.75 130.585 217.313C124.731 221.875 117.412 224.141 110.004 223.683C102.596 223.226 95.6103 220.077 90.3621 214.828C85.1139 209.58 81.9647 202.595 81.5072 195.187C81.0497 187.779 83.3154 180.459 87.878 174.605C92.4405 168.751 98.9853 164.766 106.281 163.401C113.576 162.035 121.119 163.383 127.49 167.19L167.19 127.49C165.664 124.941 164.518 122.182 163.79 119.3H141.39C139.721 125.858 135.915 131.673 130.573 135.826C125.231 139.98 118.657 142.234 111.89 142.234C105.123 142.234 98.5494 139.98 93.2071 135.826C87.8648 131.673 84.0587 125.858 82.39 119.3H60C58.1878 126.495 53.8086 132.78 47.6863 136.971C41.5641 141.163 34.1211 142.972 26.7579 142.059C19.3947 141.146 12.6191 137.574 7.70605 132.014C2.79302 126.454 0.0813599 119.29 0.0813599 111.87C0.0813599 104.451 2.79302 97.2871 7.70605 91.7272C12.6191 86.1673 19.3947 82.5947 26.7579 81.6817C34.1211 80.7686 41.5641 82.5781 47.6863 86.7696C53.8086 90.9611 58.1878 97.2456 60 104.44H82.35ZM100.86 204.32C103.407 206.868 106.759 208.453 110.345 208.806C113.93 209.159 117.527 208.258 120.522 206.256C123.517 204.254 125.725 201.276 126.771 197.828C127.816 194.38 127.633 190.677 126.253 187.349C124.874 184.021 122.383 181.274 119.205 179.577C116.027 177.88 112.359 177.337 108.826 178.042C105.293 178.746 102.113 180.654 99.8291 183.44C97.5451 186.226 96.2979 189.718 96.3 193.32C96.2985 195.364 96.7006 197.388 97.4831 199.275C98.2656 201.163 99.4132 202.877 100.86 204.32ZM204.32 122.88C206.868 120.333 208.453 116.981 208.806 113.396C209.159 109.811 208.258 106.214 206.256 103.219C204.254 100.223 201.275 98.0151 197.827 96.97C194.38 95.9249 190.676 96.1077 187.348 97.4873C184.02 98.8669 181.274 101.358 179.577 104.536C177.879 107.714 177.337 111.382 178.041 114.915C178.746 118.448 180.653 121.627 183.439 123.911C186.226 126.195 189.717 127.443 193.32 127.44C195.364 127.443 197.388 127.042 199.275 126.259C201.163 125.476 202.878 124.328 204.32 122.88ZM122.88 19.4205C120.333 16.8729 116.981 15.2876 113.395 14.9347C109.81 14.5817 106.213 15.483 103.218 17.4849C100.223 19.4868 98.0146 22.4654 96.9696 25.9131C95.9245 29.3608 96.1073 33.0642 97.4869 36.3922C98.8665 39.7202 101.358 42.4668 104.535 44.1639C107.713 45.861 111.381 46.4036 114.914 45.6992C118.447 44.9949 121.627 43.0871 123.911 40.301C126.195 37.515 127.442 34.0231 127.44 30.4205C127.44 28.3772 127.038 26.3539 126.255 24.4664C125.473 22.5788 124.326 20.8642 122.88 19.4205ZM19.42 100.86C16.8725 103.408 15.2872 106.76 14.9342 110.345C14.5813 113.93 15.4826 117.527 17.4844 120.522C19.4863 123.518 22.4649 125.726 25.9127 126.771C29.3604 127.816 33.0638 127.633 36.3918 126.254C39.7198 124.874 42.4664 122.383 44.1635 119.205C45.8606 116.027 46.4032 112.359 45.6988 108.826C44.9944 105.293 43.0866 102.114 40.3006 99.8296C37.5145 97.5455 34.0227 96.2983 30.42 96.3005C26.2938 96.3018 22.337 97.9421 19.42 100.86ZM100.86 100.86C98.3125 103.408 96.7272 106.76 96.3742 110.345C96.0213 113.93 96.9226 117.527 98.9244 120.522C100.926 123.518 103.905 125.726 107.353 126.771C110.8 127.816 114.504 127.633 117.832 126.254C121.16 124.874 123.906 122.383 125.604 119.205C127.301 116.027 127.843 112.359 127.139 108.826C126.434 105.293 124.527 102.114 121.741 99.8296C118.955 97.5455 115.463 96.2983 111.86 96.3005C109.817 96.299 107.793 96.701 105.905 97.4835C104.018 98.2661 102.303 99.4136 100.86 100.86Z" fill="#00AEEF"/>
</g>
<defs>
<clipPath id="clip0_4338_178347">
<rect width="566.93" height="223.75" fill="white"/>
</clipPath>
</defs>
</svg>
</div>
11 changes: 11 additions & 0 deletions python/ray/widgets/templates/context_table.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<table class="jp-RenderedHTMLCommon" style="border-collapse: collapse;color: var(--jp-ui-font-color1);font-size: var(--jp-ui-font-size1);">
<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>
Loading

0 comments on commit 54a1890

Please sign in to comment.