Skip to content

Commit a5e17ca

Browse files
Refactor: widgets module
1 parent 7bf7190 commit a5e17ca

File tree

5 files changed

+23
-27
lines changed

5 files changed

+23
-27
lines changed

src/codeflare_sdk/__init__.py

Lines changed: 1 addition & 1 deletion
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 renamed to src/codeflare_sdk/common/widgets/widgets.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@
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
)
3636

3737

38-
def cluster_up_down_buttons(cluster: "codeflare_sdk.cluster.Cluster") -> widgets.Button:
38+
def cluster_up_down_buttons(cluster: "codeflare_sdk.ray.cluster.cluster.Cluster"):
3939
"""
4040
The cluster_up_down_buttons function returns two button widgets for a create and delete button.
4141
The function uses the appwrapper bool to distinguish between resource type for the tool tip.
@@ -56,7 +56,7 @@ def cluster_up_down_buttons(cluster: "codeflare_sdk.cluster.Cluster") -> widgets
5656
icon="trash",
5757
)
5858

59-
wait_ready_check = wait_ready_check_box()
59+
wait_ready_check = _wait_ready_check_box()
6060
output = widgets.Output()
6161

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

8383

84-
def wait_ready_check_box():
84+
def _wait_ready_check_box():
8585
"""
8686
The wait_ready_check_box function will return a checkbox widget used for waiting for the resource to be in the state READY.
8787
"""
@@ -115,7 +115,7 @@ def view_clusters(namespace: str = None):
115115
)
116116
return # Exit function if not in Jupyter Notebook
117117

118-
from ..ray.cluster.cluster import get_current_namespace
118+
from ...ray.cluster.cluster import get_current_namespace
119119

120120
if not namespace:
121121
namespace = get_current_namespace()
@@ -278,7 +278,7 @@ def _on_ray_dashboard_button_click(
278278
"""
279279
_on_ray_dashboard_button_click handles the event when the Open Ray Dashboard button is clicked, opening the Ray Dashboard in a new tab
280280
"""
281-
from codeflare_sdk.ray.cluster import Cluster
281+
from codeflare_sdk import Cluster
282282

283283
cluster_name = classification_widget.value
284284
namespace = ray_clusters_df[ray_clusters_df["Name"] == classification_widget.value][
@@ -309,7 +309,7 @@ def _on_list_jobs_button_click(
309309
"""
310310
_on_list_jobs_button_click handles the event when the View Jobs button is clicked, opening the Ray Jobs Dashboard in a new tab
311311
"""
312-
from codeflare_sdk.ray.cluster import Cluster
312+
from codeflare_sdk import Cluster
313313

314314
cluster_name = classification_widget.value
315315
namespace = ray_clusters_df[ray_clusters_df["Name"] == classification_widget.value][
@@ -342,7 +342,7 @@ def _delete_cluster(
342342
_delete_cluster function deletes the cluster with the given name and namespace.
343343
It optionally waits for the cluster to be deleted.
344344
"""
345-
from ..ray.cluster.cluster import _check_aw_exists
345+
from ...ray.cluster.cluster import _check_aw_exists
346346

347347
try:
348348
config_check()
@@ -400,7 +400,7 @@ def _fetch_cluster_data(namespace):
400400
"""
401401
_fetch_cluster_data function fetches all clusters and their spec in a given namespace and returns a DataFrame.
402402
"""
403-
from ..ray.cluster.cluster import list_all_clusters
403+
from ...ray.cluster.cluster import list_all_clusters
404404

405405
rayclusters = list_all_clusters(namespace, False)
406406
if not rayclusters:

src/codeflare_sdk/ray/cluster/cluster.py

Lines changed: 2 additions & 2 deletions
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

Lines changed: 8 additions & 12 deletions
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)