Skip to content

Commit

Permalink
add module for remote connect to workbench
Browse files Browse the repository at this point in the history
  • Loading branch information
arckrish committed Oct 2, 2024
1 parent 323ee6a commit db15753
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/modules/ROOT/assets/images/vscode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
145 changes: 145 additions & 0 deletions content/modules/ROOT/pages/09_remote_connect_notebook.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# Connect to Workbench kernel from local VSCode

Some of the clients have expressed interest in connecting to remote workbench from their local environment (IDE's) and execute the Jupyter Notebooks. The standard feature of most IDEs to connect to remote kernel uses token based authentication. Workbench pods running on RHOAI contain an authentication mechanism that sits in front of the workbench container and handles the authentication of the user connecting to the workbench. This container uses Openshift Authentication mechanism and is not compatible with the standard connection feature of most IDEs.

## Steps to connect local VS Code to RHOAI Workbench kernel:

* Create a Data Science Project ai-bootcamp
[.bordershadow]
image::create_projecti.png[width=35%]

* Create a workbench with following parameters:
**Name** workbench-1
**image** Standard Data Science
[.bordershadow]
image::create_workbench.png[width=35%]

* Open VS Code in local. Create a new jupyter notebook.
* **Principal Component Analysis Plots:** This example uses https://en.wikipedia.org/wiki/Iris_flower_data_set[Iris Data], performs a PCA and plots the data against the first two principal components in a scatter plot. It then prints the eigenvalues and eigenvectors of the covariance matrix and finally prints the precentage of total variance explained by each component.
* In a new cell, add the below contents.
+
[source, python]
----
from __future__ import division
import math
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import sklearn.datasets
import sklearn.metrics as metrics
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
# load the iris dataset
dataset = sklearn.datasets.load_iris()
# define feature vectors (X) and target (y)
X = dataset.data
y = dataset.target
labels = dataset.target_names
----

+
[source, python]
----
# <help:scikit_pca>
# define the number of components to compute, recommend n_components < y_features
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X)
# plot the first two principal components
fig, ax = plt.subplots()
plt.scatter(X_pca[:,0], X_pca[:,1])
plt.grid()
plt.title('PCA of the dataset')
ax.set_xlabel('Component #1')
ax.set_ylabel('Component #2')
plt.show()
----

+
[source, python]
----
# <help:scikit_pca>
# eigendecomposition on the covariance matrix
cov_mat = np.cov(X_pca.T)
eig_vals, eig_vecs = np.linalg.eig(cov_mat)
print('Eigenvectors \n%s' %eig_vecs)
print('\nEigenvalues \n%s' %eig_vals)
----

+
[source, python]
----
# <help:scikit_pca>
# prints the percentage of overall variance explained by each component
print(pca.explained_variance_ratio_)
----

* Login to OpenShift from terminal on your laptop.
[.bordershadow]
image::terminal_oclogin.png[width=35%]

* Switch to project and view pods
[.bordershadow]
image::terminal_getpods.png[width=35%]

* Start port-forwarding to the workbench pod
You need to forward to the port the pod is listening on. It is usually 8888 for RHOAI workbench. You can find this port from the service in your project with name same as your workbench.
[.bordershadow]
image::terminal_portforward.png[width=35%]

* Open the Jupyter Notebook in your VSCode
[.bordershadow]
image::vscode.png[width=35%]

* Click on **Select Kernel** in the top right corner of the notebook.
[.bordershadow]
image::vscode_kernel.png[width=35%]

* From the options, select **Existing Jupyter Server** and then enter the url as follows: localhost [:port] /context-path
In this case url is:
[source, html]
----
http://localhost:8888/notebook/ai-bootcamp/workbench-1/lab
----

[.bordershadow]
image::vscode_connect_url.png[width=35%]

* Select Yes for the prompt: _Connecting over HTTP without a token may be an insecure connection. Do you want to connect to a possibly insecure server?_
[.bordershadow]
image::vscode_connect_prompt.png[width=35%]

* Choose a new name or click 'Enter' to accept the suggested server name.
[.bordershadow]
image::vscode_kernel_name.png[width=35%]

* Choose **Python 3.9** from the list of available kernels.
[.bordershadow]
image::vscode_recommended_kernel.png[width=35%]

* We should be able to see the selected kernel in top-right corner. Execute the cells in the notebook. This will execute using the remote kernel on the RHOAI workbench.
[.bordershadow]
image::vscode_selected_kernel.png[width=35%]

## Limitations to this approach:

. Jupyter notebooks in your local VSCode environment will not be saved to the workbench.

. If your notebook uses any files (models, inputdata etc.), they should be present on the workbench and their path should match the path specified in your notebook.

0 comments on commit db15753

Please sign in to comment.