Skip to content
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

Console logic #2

Merged
merged 7 commits into from
Mar 26, 2024
13 changes: 1 addition & 12 deletions schema/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,7 @@
"title": "available_kernel_menu",
"description": "available_kernel_menu settings.",
"jupyter.lab.menus": {
"main": [
{
"id": "get_kernels_menu",
"label": "Available Kernels Widget",
"rank": 80,
"items": [
{
"command": "widgets:open-kernel-info-tab"
}
]
}
]
"main": []
},
"type": "object",
"properties": {},
Expand Down
84 changes: 15 additions & 69 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,9 @@
import { JupyterFrontEndPlugin } from '@jupyterlab/application';
import { KernelManager } from '@jupyterlab/services';
import { ICommandPalette } from '@jupyterlab/apputils';
import { Menu, Widget } from '@lumino/widgets';
import { Menu } from '@lumino/widgets';
import { ISettingRegistry } from '@jupyterlab/settingregistry';
import { IMainMenu } from '@jupyterlab/mainmenu';
import { EditorLanguageRegistry } from '@jupyterlab/codemirror';
class KernelInfoWidget extends Widget {
constructor(serviceManager: any) {
super();
this.addClass('kernel-info-widget');
this.id = `kernel-info-widget-${Date.now()}`;
this.title.label = 'Kernel Information';
this.title.closable = true;
this.fetchKernelInfo(serviceManager);
}
async fetchKernelInfo(serviceManager: any) {
try {
const kernelSpecs = await serviceManager.kernelspecs.specs;
const kernelspecs = kernelSpecs.kernelspecs;

const content = document.createElement('div');
content.classList.add('kernels-container');
Object.entries(kernelspecs).forEach(([key, value]: [string, any]) => {
console.log(value);
const logo = value.resources['logo-64x64'];
const kernelDiv = document.createElement('div');
kernelDiv.classList.add('kernel-info');
kernelDiv.innerHTML = `
<p>${value.display_name}</p>
<div class="kernel-logo">
<img src="${logo}" alt="${value.display_name}"></img>
</div>
`;
kernelDiv.addEventListener('click', async () => {
try {
const kernelManager = new KernelManager();
await kernelManager.startNew({ name: key });
console.log(`Kernel '${key}' started successfully.`);
} catch (error) {
console.error(`Error starting kernel '${key}':`, error);
}
});

content.appendChild(kernelDiv);
});
this.node.appendChild(content);
} catch (error) {
console.error('Error retrieving kernel information:', error);
this.node.textContent = 'Error retrieving kernel information';
}
}
}

function addKernelMenuItems(
app: any,
Expand All @@ -62,8 +15,10 @@ function addKernelMenuItems(
const menu = new Menu({ commands: app.commands });
menu.title.label = 'Available Kernels Menu';
mainMenu.addMenu(menu);

Object.entries(serviceManager.kernelspecs.specs.kernelspecs).forEach(
([key, value]: [string, any]) => {
let count: number = 0;
const kernelMenu = new Menu({ commands: app.commands });
kernelMenu.title.label = value.display_name;
const language =
Expand All @@ -78,6 +33,7 @@ function addKernelMenuItems(
});

const startNotebookCommand = `widgets:start-notebook-${key}`;

commands.addCommand(startNotebookCommand, {
label: `New ${key} notebook`,
execute: async () => {
Expand All @@ -94,16 +50,20 @@ function addKernelMenuItems(
});
}
});

const startConsoleCommand = `widgets:start-console-${key}`;

commands.addCommand(startConsoleCommand, {
label: `New ${key} console`,
execute: async () => {
await app.commands.execute('console:create', {
path: '.',
factory: 'Console',
kernel: key
});
count++;
try {
await commands.execute('console:create', {
name: `Console ${count}`,
kernelPreference: { name: key }
});
} catch (error) {
console.error('Error starting console:', error);
}
}
});

Expand Down Expand Up @@ -150,21 +110,7 @@ const plugin: JupyterFrontEndPlugin<void> = {
optional: [ISettingRegistry],
requires: [ICommandPalette, IMainMenu],
activate: (app, palette, mainMenu: IMainMenu) => {
const { commands, shell, serviceManager } = app;
const openKernelInfoCommand = 'widgets:open-kernel-info-tab';
commands.addCommand(openKernelInfoCommand, {
label: 'Get Available Kernels',
caption: 'Open the Widgets to get available kernels',
execute: () => {
const widget = new KernelInfoWidget(serviceManager);
shell.add(widget, 'main');
}
});

palette.addItem({
command: openKernelInfoCommand,
category: 'Kernel Info Extension Examples'
});
const { serviceManager } = app;

serviceManager.ready.then(() => {
serviceManager.kernelspecs.ready.then(() => {
Expand Down
Loading