Skip to content

Commit 4cd1ff4

Browse files
Add head comments to functions in widgets.py
1 parent b4a1583 commit 4cd1ff4

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

src/codeflare_sdk/cluster/widgets.py

+38-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import contextlib
1919
import io
2020
import os
21-
from time import sleep
21+
import warnings
2222
import time
2323
import codeflare_sdk
2424
from kubernetes import client
@@ -103,7 +103,13 @@ def is_notebook() -> bool:
103103

104104

105105
def view_clusters(namespace: str = None):
106-
"""view_clusters function will display existing clusters with their specs, and handle user interactions."""
106+
"""
107+
view_clusters function will display existing clusters with their specs, and handle user interactions.
108+
"""
109+
if not is_notebook():
110+
warnings.warn("view_clusters can only be used in a Jupyter Notebook environment.")
111+
return # Exit function if not in Jupyter Notebook
112+
107113
from .cluster import get_current_namespace
108114
if not namespace:
109115
namespace = get_current_namespace()
@@ -112,13 +118,13 @@ def view_clusters(namespace: str = None):
112118
raycluster_data_output = widgets.Output()
113119
url_output = widgets.Output()
114120

115-
df = _fetch_cluster_data(namespace)
116-
if df.empty:
121+
ray_clusters_df = _fetch_cluster_data(namespace)
122+
if ray_clusters_df.empty:
117123
print(f"No clusters found in the {namespace} namespace.")
118124
return
119125

120126
classification_widget = widgets.ToggleButtons(
121-
options=df["Name"].tolist(), value=df["Name"].tolist()[0],
127+
options=ray_clusters_df["Name"].tolist(), value=ray_clusters_df["Name"].tolist()[0],
122128
description='Select an existing cluster:',
123129
)
124130
# Setting the initial value to trigger the event handler to display the cluster details.
@@ -132,28 +138,31 @@ def view_clusters(namespace: str = None):
132138
icon='trash',
133139
tooltip="Delete the selected cluster"
134140
)
135-
delete_button.on_click(lambda b: _on_delete_button_click(b, classification_widget, df, raycluster_data_output, user_output, delete_button, list_jobs_button, ray_dashboard_button))
141+
delete_button.on_click(lambda b: _on_delete_button_click(b, classification_widget, ray_clusters_df, raycluster_data_output, user_output, delete_button, list_jobs_button, ray_dashboard_button))
136142

137143
list_jobs_button = widgets.Button(
138144
description='View Jobs',
139145
icon='suitcase',
140146
tooltip="Open the Ray Job Dashboard"
141147
)
142-
list_jobs_button.on_click(lambda b: _on_list_jobs_button_click(b, classification_widget, df, user_output, url_output))
148+
list_jobs_button.on_click(lambda b: _on_list_jobs_button_click(b, classification_widget, ray_clusters_df, user_output, url_output))
143149

144150
ray_dashboard_button = widgets.Button(
145151
description='Open Ray Dashboard',
146152
icon='dashboard',
147153
tooltip="Open the Ray Dashboard in a new tab",
148154
layout=widgets.Layout(width='auto'),
149155
)
150-
ray_dashboard_button.on_click(lambda b: _on_ray_dashboard_button_click(b, classification_widget, df, user_output, url_output))
156+
ray_dashboard_button.on_click(lambda b: _on_ray_dashboard_button_click(b, classification_widget, ray_clusters_df, user_output, url_output))
151157

152158
display(widgets.VBox([classification_widget, raycluster_data_output]))
153159
display(widgets.HBox([delete_button, list_jobs_button, ray_dashboard_button]), url_output, user_output)
154160

155-
# Handles the event when a cluster is selected from the toggle buttons, updating the output with cluster details.
161+
156162
def _on_cluster_click(selection_change, raycluster_data_output: widgets.Output, namespace: str, classification_widget: widgets.ToggleButtons):
163+
"""
164+
_on_cluster_click handles the event when a cluster is selected from the toggle buttons, updating the output with cluster details.
165+
"""
157166
new_value = selection_change["new"]
158167
raycluster_data_output.clear_output()
159168
df = _fetch_cluster_data(namespace)
@@ -163,6 +172,9 @@ def _on_cluster_click(selection_change, raycluster_data_output: widgets.Output,
163172

164173

165174
def _on_delete_button_click(b, classification_widget: widgets.ToggleButtons, df: pd.DataFrame, raycluster_data_output: widgets.Output, user_output: widgets.Output, delete_button: widgets.Button, list_jobs_button: widgets.Button, ray_dashboard_button: widgets.Button):
175+
"""
176+
_on_delete_button_click handles the event when the Delete Button is clicked, deleting the selected cluster.
177+
"""
166178
cluster_name = classification_widget.value
167179
namespace = df[df["Name"]==classification_widget.value]["Namespace"].values[0]
168180

@@ -186,6 +198,9 @@ def _on_delete_button_click(b, classification_widget: widgets.ToggleButtons, df:
186198
classification_widget.options = new_df["Name"].tolist()
187199

188200
def _on_ray_dashboard_button_click(b, classification_widget: widgets.ToggleButtons, df: pd.DataFrame, user_output: widgets.Output, url_output: widgets.Output):
201+
"""
202+
_on_ray_dashboard_button_click handles the event when the Open Ray Dashboard button is clicked, opening the Ray Dashboard in a new tab
203+
"""
189204
from codeflare_sdk.cluster import Cluster
190205
cluster_name = classification_widget.value
191206
namespace = df[df["Name"]==classification_widget.value]["Namespace"].values[0]
@@ -202,6 +217,9 @@ def _on_ray_dashboard_button_click(b, classification_widget: widgets.ToggleButto
202217
display(Javascript(f'window.open("{dashboard_url}", "_blank");'))
203218

204219
def _on_list_jobs_button_click(b, classification_widget: widgets.ToggleButtons, df: pd.DataFrame, user_output: widgets.Output, url_output: widgets.Output):
220+
"""
221+
_on_list_jobs_button_click handles the event when the View Jobs button is clicked, opening the Ray Jobs Dashboard in a new tab
222+
"""
205223
from codeflare_sdk.cluster import Cluster
206224
cluster_name = classification_widget.value
207225
namespace = df[df["Name"]==classification_widget.value]["Namespace"].values[0]
@@ -217,12 +235,17 @@ def _on_list_jobs_button_click(b, classification_widget: widgets.ToggleButtons,
217235
with url_output:
218236
display(Javascript(f'window.open("{dashboard_url}/#/jobs", "_blank");'))
219237

238+
220239
def _delete_cluster(
221240
cluster_name: str,
222241
namespace: str,
223242
timeout: int = 5,
224243
interval: int = 1,
225244
):
245+
"""
246+
_delete_cluster function deletes the cluster with the given name and namespace.
247+
It optionally waits for the cluster to be deleted.
248+
"""
226249
from .cluster import _check_aw_exists
227250

228251
try:
@@ -276,6 +299,9 @@ def _delete_cluster(
276299

277300

278301
def _fetch_cluster_data(namespace):
302+
"""
303+
_fetch_cluster_data function fetches all clusters and their spec in a given namespace and returns a DataFrame.
304+
"""
279305
from .cluster import list_all_clusters
280306
rayclusters = list_all_clusters(namespace, False)
281307
if not rayclusters:
@@ -323,6 +349,9 @@ def _fetch_cluster_data(namespace):
323349

324350

325351
def _format_status(status):
352+
"""
353+
_format_status function formats the status enum.
354+
"""
326355
status_map = {
327356
RayClusterStatus.READY: '<span style="color: green;">Ready ✓</span>',
328357
RayClusterStatus.SUSPENDED: '<span style="color: #007BFF;">Suspended ❄️</span>',

0 commit comments

Comments
 (0)