Skip to content

272 call be to get kernel #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions server/app/routes/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

logging.basicConfig(level=logging.INFO)

@kernel_blueprint.route('/kernel/<path:kernel_id>', methods=['GET'])
def get_kernel_by_id(kernel_id):
logging.info(f"Getting kernel with id: {kernel_id}")
return Kernel.get_kernel_by_id(kernel_id)

@kernel_blueprint.route('/kernel/restart/<path:kernel_id>', methods=['POST'])
def restart_kernel(kernel_id):
logging.info(f"Restarting kernel with id: {kernel_id}")
Expand Down
22 changes: 22 additions & 0 deletions server/app/services/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@

class Kernel:

@staticmethod
def get_kernel_by_id(kernel_id):
try:
response = requests.get(app.config['JUPYTER_KERNEL_API_PATH'] + f"/{kernel_id}")
except Exception as e:
logger.error(f"Met exception getting all kernels: {e}")
return Response(
response=json.dumps({'message': 'Error getting all kernels from Jupyter Server: ' + str(e)}),
status=404)

if response.status_code != 200:
logger.error(f"Error getting kernel: {response.content}")
return Response(
response=json.dumps({'message': 'Error getting kernel'}),
status=404)

return Response(
response=response,
status=200,
mimetype='application/json'
)

@staticmethod
def restart_kernel(kernel_id):
path = app.config['JUPYTER_KERNEL_API_PATH'] + f"/{kernel_id}/restart"
Expand Down
23 changes: 23 additions & 0 deletions server/tests/services/test_kernel_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@ def setUp(self):
self.app = create_app()
self.client = self.app.test_client()

def test_get_kernel_by_id(self):
with self.app.app_context():
# Get non-exist kernel
response_0 = Kernel.get_kernel_by_id('kernel_id')
self.assertEqual(response_0.status_code, 404)

# Create Notebook
response_1 = Notebook.create_notebook_with_init_cells(notebook_name='Notebook_1.ipynb', notebook_path='')
self.assertEqual(response_1.status_code, 200)

notebook_1 = json.loads(response_1.data.decode('utf-8'))
notebook_path_1 = notebook_1['path']

# Create Session
response_2 = Session.create_session(notebook_path_1)
self.assertEqual(response_2.status_code, 200)
session = json.loads(response_2.data.decode('utf-8'))
kernelId = session['kernel']['id']

# Get kernel
response_3 = Kernel.get_kernel_by_id(kernelId)
self.assertEqual(response_3.status_code, 200)

def test_restart_kernel(self):
with self.app.app_context():
# Restart non-exist kernel
Expand Down
4 changes: 2 additions & 2 deletions webapp/src/components/notebook/header/NotebookKernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useState } from 'react';
import { JupyterKernelIcon } from '@datalayer/icons-react';
import { VscTriangleDown } from "react-icons/vsc";
import LoadingButton from '@mui/lab/LoadingButton';
import KernelModel from '../../../models/KernelModel';
import KernelModel from '../../../models/KernelModel'
import config from '../../../config';

const NotebookKernel = ({
Expand All @@ -21,7 +21,7 @@ const NotebookKernel = ({
setIsRestarting(true);
setMenuOpen(false);
setSparkAppId(null);
await KernelModel.restartKernel(config.jupyterBaseUrl, kernelId);
await KernelModel.restartKernel(kernelId);
setIsRestarting(false);
} catch (error) {
console.error('Failed to restart kernel:', error);
Expand Down
4 changes: 2 additions & 2 deletions webapp/src/models/KernelModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class KernelModel {
constructor() {
}

static async restartKernel(basePath = '', kernelId = '') {
static async restartKernel(kernelId = '') {
try {
// Wait for the kernel to restart
await fetch(`http://localhost:5002/kernel/restart/${kernelId}`, {
Expand All @@ -14,7 +14,7 @@ class KernelModel {

let status;
do {
const response = await fetch(`${basePath}/api/kernels/${kernelId}`);
const response = await fetch(`http://localhost:5002/kernel/${kernelId}`);
const data = await response.json();
status = data.execution_state;
if (status === 'busy') {
Expand Down
Loading