Skip to content

Commit

Permalink
Revert "Add a disconnect button to the context widgets in notebooks (r…
Browse files Browse the repository at this point in the history
  • Loading branch information
rkooo567 authored and sihanwang41 committed May 18, 2023
1 parent 4ea1866 commit 33f8b2a
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 114 deletions.
90 changes: 16 additions & 74 deletions python/ray/_private/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@
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 @@ -1020,10 +1019,6 @@ 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 @@ -1041,73 +1036,6 @@ 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="http://" + 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 @@ -1119,10 +1047,10 @@ class RayContext(BaseContext, Mapping):
python_version: str
ray_version: str
ray_commit: str
protocol_version: Optional[str]
protocol_version = Optional[str]
address_info: Dict[str, 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 @@ -1164,6 +1092,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="http://" + 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 @@ -19,6 +19,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 @@ -85,6 +86,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="http://" + 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: 0 additions & 4 deletions python/ray/tests/test_widgets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import logging
from unittest import mock

import pytest
Expand All @@ -8,7 +7,6 @@
@mock.patch("importlib.import_module")
def test_ensure_notebook_dep_missing(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 @@ -32,7 +30,6 @@ def dummy_ipython_display(self):
@mock.patch("importlib.import_module")
def test_ensure_notebook_dep_outdated(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 @@ -52,7 +49,6 @@ def dummy_ipython_display():
@mock.patch("importlib.import_module")
def test_ensure_notebook_valid(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: 3 additions & 2 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:
HTML template with the keys of the kwargs replaced with corresponding
str: HTML template with the keys of the kwargs replaced with corresponding
values.
"""
rendered = self.template
Expand All @@ -34,6 +34,7 @@ def list_templates() -> List[pathlib.Path]:
"""List the available HTML templates.
Returns:
A list of files with .html.j2 extensions inside ../templates/
List[pathlib.Path]: A list of files with .html.j2 extensions inside
./templates/
"""
return (pathlib.Path(__file__).parent / "templates").glob("*.html.j2")
37 changes: 34 additions & 3 deletions python/ray/widgets/templates/context.html.j2
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
<div class="lm-Widget p-Widget lm-Panel p-Panel jp-Cell-outputWrapper">
<div>
<div style="margin-left: 50px;display: flex;flex-direction: row;align-items: center">
{{ context_logo }}
{{ context_table }}
<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>
</div>
</div>
13 changes: 0 additions & 13 deletions python/ray/widgets/templates/context_logo.html.j2

This file was deleted.

11 changes: 0 additions & 11 deletions python/ray/widgets/templates/context_table.html.j2

This file was deleted.

16 changes: 9 additions & 7 deletions python/ray/widgets/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ def ensure_notebook_deps(
) -> Callable[[F], F]:
"""Generate a decorator which checks for soft dependencies.
This decorator is meant to wrap _repr_mimebundle_ methods. If the dependency is not
found, or a version is specified here and the version of the package is older than
the specified version, the original repr is used.
This decorator is meant to wrap repr methods. If the dependency is not found,
or a version is specified here and the version of the package is older than the
specified version, the original repr is used.
If the dependency is missing or the version is old, a log message is displayed.
Args:
Expand Down Expand Up @@ -155,11 +155,11 @@ def _has_missing(
message = f"Run `pip install {' '.join(missing)}` for rich notebook output."

if sys.version_info < (3, 8):
logger.info(f"Missing packages: {missing}. {message}")
logger.warning(f"Missing packages: {missing}. {message}")
else:
# stacklevel=3: First level is this function, then ensure_notebook_deps,
# then the actual function affected.
logger.info(f"Missing packages: {missing}. {message}", stacklevel=3)
logger.warning(f"Missing packages: {missing}. {message}", stacklevel=3)

return missing

Expand Down Expand Up @@ -190,11 +190,13 @@ def _has_outdated(
message = f"Run `pip install -U {install_str}` for rich notebook output."

if sys.version_info < (3, 8):
logger.info(f"Outdated packages:\n{outdated_str}\n{message}")
logger.warning(f"Outdated packages:\n{outdated_str}\n{message}")
else:
# stacklevel=3: First level is this function, then ensure_notebook_deps,
# then the actual function affected.
logger.info(f"Outdated packages:\n{outdated_str}\n{message}", stacklevel=3)
logger.warning(
f"Outdated packages:\n{outdated_str}\n{message}", stacklevel=3
)

return outdated

Expand Down

0 comments on commit 33f8b2a

Please sign in to comment.