Skip to content

Commit d4d0dce

Browse files
Refactor: widgets module
1 parent ffbdfec commit d4d0dce

File tree

5 files changed

+22
-26
lines changed

5 files changed

+22
-26
lines changed

src/codeflare_sdk/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RayJobClient,
1313
)
1414

15-
from .cluster import view_clusters
15+
from .common.widgets import view_clusters
1616

1717
from .common import (
1818
Authentication,

src/codeflare_sdk/cluster/widgets.py src/codeflare_sdk/common/widgets/widgets.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
import ipywidgets as widgets
2727
from IPython.display import display, HTML, Javascript
2828
import pandas as pd
29-
from ..ray.cluster.config import ClusterConfiguration
30-
from ..ray.cluster.status import RayClusterStatus
31-
from ..common import _kube_api_error_handling
32-
from ..common.kubernetes_cluster.auth import (
29+
from ...ray.cluster.config import ClusterConfiguration
30+
from ...ray.cluster.status import RayClusterStatus
31+
from ..kubernetes_cluster import _kube_api_error_handling
32+
from ..kubernetes_cluster.auth import (
3333
config_check,
3434
get_api_client,
3535
)
@@ -58,7 +58,7 @@ def cluster_up_down_buttons(
5858
icon="trash",
5959
)
6060

61-
wait_ready_check = wait_ready_check_box()
61+
wait_ready_check = _wait_ready_check_box()
6262
output = widgets.Output()
6363

6464
# Display the buttons in an HBox wrapped in a VBox which includes the wait_ready Checkbox
@@ -83,7 +83,7 @@ def on_down_button_clicked(b): # Handle the down button click event
8383
delete_button.on_click(on_down_button_clicked)
8484

8585

86-
def wait_ready_check_box():
86+
def _wait_ready_check_box():
8787
"""
8888
The wait_ready_check_box function will return a checkbox widget used for waiting for the resource to be in the state READY.
8989
"""
@@ -117,7 +117,7 @@ def view_clusters(namespace: str = None):
117117
)
118118
return # Exit function if not in Jupyter Notebook
119119

120-
from ..ray.cluster.cluster import get_current_namespace
120+
from ...ray.cluster.cluster import get_current_namespace
121121

122122
if not namespace:
123123
namespace = get_current_namespace()
@@ -280,7 +280,7 @@ def _on_ray_dashboard_button_click(
280280
"""
281281
_on_ray_dashboard_button_click handles the event when the Open Ray Dashboard button is clicked, opening the Ray Dashboard in a new tab
282282
"""
283-
from codeflare_sdk.ray.cluster import Cluster
283+
from codeflare_sdk import Cluster
284284

285285
cluster_name = classification_widget.value
286286
namespace = ray_clusters_df[ray_clusters_df["Name"] == classification_widget.value][
@@ -311,7 +311,7 @@ def _on_list_jobs_button_click(
311311
"""
312312
_on_list_jobs_button_click handles the event when the View Jobs button is clicked, opening the Ray Jobs Dashboard in a new tab
313313
"""
314-
from codeflare_sdk.ray.cluster import Cluster
314+
from codeflare_sdk import Cluster
315315

316316
cluster_name = classification_widget.value
317317
namespace = ray_clusters_df[ray_clusters_df["Name"] == classification_widget.value][
@@ -344,7 +344,7 @@ def _delete_cluster(
344344
_delete_cluster function deletes the cluster with the given name and namespace.
345345
It optionally waits for the cluster to be deleted.
346346
"""
347-
from ..ray.cluster.cluster import _check_aw_exists
347+
from ...ray.cluster.cluster import _check_aw_exists
348348

349349
try:
350350
config_check()
@@ -402,7 +402,7 @@ def _fetch_cluster_data(namespace):
402402
"""
403403
_fetch_cluster_data function fetches all clusters and their spec in a given namespace and returns a DataFrame.
404404
"""
405-
from ..ray.cluster.cluster import list_all_clusters
405+
from ...ray.cluster.cluster import list_all_clusters
406406

407407
rayclusters = list_all_clusters(namespace, False)
408408
if not rayclusters:

src/codeflare_sdk/ray/cluster/cluster.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 IBM, Red Hat
1+
# Copyright 2024 IBM, Red Hat
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -45,7 +45,7 @@
4545
AppWrapper,
4646
AppWrapperStatus,
4747
)
48-
from ...cluster.widgets import (
48+
from ...common.widgets.widgets import (
4949
cluster_up_down_buttons,
5050
is_notebook,
5151
)

tests/unit_test.py

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 IBM, Red Hat
1+
# Copyright 2024 IBM, Red Hat
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -79,7 +79,7 @@
7979
is_openshift_cluster,
8080
)
8181

82-
import codeflare_sdk.cluster.widgets as cf_widgets
82+
import codeflare_sdk.common.widgets.widgets as cf_widgets
8383
import pandas as pd
8484

8585
import openshift
@@ -2959,24 +2959,20 @@ def test_cluster_up_down_buttons(mocker):
29592959

29602960
@patch.dict("os.environ", {}, clear=True) # Mock environment with no variables
29612961
def test_is_notebook_false():
2962-
from codeflare_sdk.cluster.widgets import is_notebook
2963-
2964-
assert is_notebook() is False
2962+
assert cf_widgets.is_notebook() is False
29652963

29662964

29672965
@patch.dict(
29682966
"os.environ", {"JPY_SESSION_NAME": "example-test"}
29692967
) # Mock Jupyter environment variable
29702968
def test_is_notebook_true():
2971-
from codeflare_sdk.cluster.widgets import is_notebook
2972-
2973-
assert is_notebook() is True
2969+
assert cf_widgets.is_notebook() is True
29742970

29752971

29762972
def test_view_clusters(mocker, capsys):
29772973
from kubernetes.client.rest import ApiException
29782974

2979-
mocker.patch("codeflare_sdk.cluster.widgets.is_notebook", return_value=False)
2975+
mocker.patch("codeflare_sdk.common.widgets.widgets.is_notebook", return_value=False)
29802976
with pytest.warns(
29812977
UserWarning,
29822978
match="view_clusters can only be used in a Jupyter Notebook environment.",
@@ -2985,7 +2981,7 @@ def test_view_clusters(mocker, capsys):
29852981
# Assert the function returns None when not in a notebook environment
29862982
assert result is None
29872983

2988-
mocker.patch("codeflare_sdk.cluster.widgets.is_notebook", return_value=True)
2984+
mocker.patch("codeflare_sdk.common.widgets.widgets.is_notebook", return_value=True)
29892985

29902986
# Mock Kubernetes API responses
29912987
mocker.patch("kubernetes.client.ApisApi.get_api_versions")
@@ -3030,7 +3026,7 @@ def test_view_clusters(mocker, capsys):
30303026

30313027
# Mock the _fetch_cluster_data function to return a test DataFrame
30323028
mocker.patch(
3033-
"codeflare_sdk.cluster.widgets._fetch_cluster_data", return_value=test_df
3029+
"codeflare_sdk.common.widgets.widgets._fetch_cluster_data", return_value=test_df
30343030
)
30353031

30363032
# Mock the Cluster class and related methods
@@ -3048,7 +3044,7 @@ def test_view_clusters(mocker, capsys):
30483044
) as mock_display, patch(
30493045
"IPython.display.HTML"
30503046
), patch(
3051-
"codeflare_sdk.cluster.widgets.Javascript"
3047+
"codeflare_sdk.common.widgets.widgets.Javascript"
30523048
) as mock_javascript:
30533049
# Create mock widget instances
30543050
mock_toggle = MagicMock()

0 commit comments

Comments
 (0)