Skip to content

Commit 2c2266f

Browse files
committed
feat: add ui cluster creation and deletion buttons
Signed-off-by: Bobbins228 <mcampbel@redhat.com>
1 parent fb59ba6 commit 2c2266f

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/codeflare_sdk/cluster/cluster.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
from kubernetes import config
5252
from kubernetes.client.rest import ApiException
5353

54+
import ipywidgets as widgets
55+
from IPython.display import display
56+
5457

5558
class Cluster:
5659
"""
@@ -71,6 +74,8 @@ def __init__(self, config: ClusterConfiguration):
7174
self.app_wrapper_yaml = self.create_app_wrapper()
7275
self._job_submission_client = None
7376
self.app_wrapper_name = self.config.name
77+
if _is_notebook():
78+
self._cluster_up_down_buttons()
7479

7580
@property
7681
def _client_headers(self):
@@ -156,11 +161,42 @@ def up(self):
156161
plural="appwrappers",
157162
body=aw,
158163
)
164+
print(f"AppWrapper: '{self.config.name}' has successfully been created")
159165
else:
160166
self._component_resources_up(namespace, api_instance)
167+
print(
168+
f"Ray Cluster: '{self.config.name}' has successfully been created"
169+
)
161170
except Exception as e: # pragma: no cover
162171
return _kube_api_error_handling(e)
163172

173+
def _cluster_up_down_buttons(self):
174+
delete_button = widgets.Button(
175+
description="Cluster Down",
176+
icon="trash",
177+
)
178+
up_button = widgets.Button(
179+
description="Cluster Up",
180+
icon="play",
181+
)
182+
183+
output = widgets.Output()
184+
# Display the buttons in an HBox
185+
display(widgets.HBox([delete_button, up_button]), output)
186+
187+
def on_up_button_clicked(b):
188+
with output:
189+
output.clear_output()
190+
self.up()
191+
192+
def on_down_button_clicked(b):
193+
with output:
194+
output.clear_output()
195+
self.down()
196+
197+
up_button.on_click(on_up_button_clicked)
198+
delete_button.on_click(on_down_button_clicked)
199+
164200
def _throw_for_no_raycluster(self):
165201
api_instance = client.CustomObjectsApi(api_config_handler())
166202
try:
@@ -198,8 +234,12 @@ def down(self):
198234
plural="appwrappers",
199235
name=self.app_wrapper_name,
200236
)
237+
print(f"AppWrapper: '{self.config.name}' has successfully been deleted")
201238
else:
202239
self._component_resources_down(namespace, api_instance)
240+
print(
241+
f"Ray Cluster: '{self.config.name}' has successfully been deleted"
242+
)
203243
except Exception as e: # pragma: no cover
204244
return _kube_api_error_handling(e)
205245

@@ -905,3 +945,13 @@ def _copy_to_ray(cluster: Cluster) -> RayCluster:
905945
if ray.status == CodeFlareClusterStatus.READY:
906946
ray.status = RayClusterStatus.READY
907947
return ray
948+
949+
950+
def _is_notebook() -> bool:
951+
if (
952+
"PYDEVD_IPYTHON_COMPATIBLE_DEBUGGING" in os.environ
953+
or "JPY_SESSION_NAME" in os.environ
954+
): # If running Jupyter NBs in VsCode or RHOAI/ODH display UI buttons
955+
return True
956+
else:
957+
return False

0 commit comments

Comments
 (0)