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

Add a pyp5js kernel backed by pyodide #52

Closed
wants to merge 6 commits into from
Closed
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
21 changes: 16 additions & 5 deletions src/app/plugins/notebook/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,18 @@ export const addCommands = (
});

commands.addCommand(CommandIDs.new, {
label: 'New Notebook',
execute: async () => {
label: (args: any) => {
const kernelName = args['kernelName'];
if (args['isLauncher'] && kernelName) {
return kernelName;
}
return 'New Notebook';
},
execute: (args: any) => {
const kernelName = args['kernelName'] as string;
commands.execute(CommandIDs.open, {
name: 'untitled.ipynb'
name: 'untitled.ipynb',
kernelName
});
}
});
Expand All @@ -108,13 +116,16 @@ export const addCommands = (
label: (args: any) => {
const name = args['name'] as string;
if (name) {
return `Open ${name}`;
return `${name}`;
}
return 'Open Example';
},
execute: async args => {
const name = args['name'] as string;
const notebook = docManager.open(name) as NotebookPanel;
const kernelName = args['kernelName'] as string;
const notebook = docManager.open(name, 'default', {
name: kernelName
}) as NotebookPanel;
const sessionContext = notebook.sessionContext;
await sessionContext.ready;

Expand Down
93 changes: 93 additions & 0 deletions src/resources/pyp5js.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 2 additions & 18 deletions src/server/contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,7 @@ namespace Private {
*/
const EMPTY_NB: INotebookContent = {
metadata: {
orig_nbformat: 4,
kernelspec: {
name: 'p5.js',
display_name: 'p5.js'
},
language_info: {
codemirror_mode: {
name: 'javascript',
version: 3
},
file_extension: '.js',
mimetype: 'text/javascript',
name: 'javascript',
nbconvert_exporter: 'javascript',
pygments_lexer: 'javascript',
version: 'es2017'
}
orig_nbformat: 4
},
nbformat_minor: 4,
nbformat: 4,
Expand All @@ -149,7 +133,7 @@ namespace Private {
created: '2020-03-18T18:41:01.243007Z',
content: EMPTY_NB,
format: 'json',
mimetype: '',
mimetype: 'application/json',
size: JSON.stringify(EMPTY_NB).length,
writable: true,
type: 'notebook'
Expand Down
37 changes: 30 additions & 7 deletions src/server/kernels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import {

import { Server as WebSocketServer } from 'mock-socket';

import { KernelIFrame } from './kernel';
import { KernelIFrame } from './kernels/kernel';

import { IJupyterServer } from '../tokens';
import { PyP5KernelIFrame } from './kernels/pyp5Kernel';

/**
* A class to handle requests to /api/kernels
Expand All @@ -20,10 +21,10 @@ export class Kernels implements IJupyterServer.IRoutable {
/**
* Start a new kernel.
*
* @param sessionId The session id.
* @param options The kernel start options.
*/
startNew(sessionId: string): Kernel.IModel {
const id = sessionId;
startNew(options: Kernels.IStartOptions): Kernel.IModel {
const { id, name } = options;
const kernelUrl = `${Kernels.WS_BASE_URL}/api/kernels/${id}/channels`;
const wsServer = new WebSocketServer(kernelUrl);

Expand All @@ -34,7 +35,14 @@ export class Kernels implements IJupyterServer.IRoutable {
socket.send(message);
};

const kernel = new KernelIFrame({ id, sendMessage, sessionId });
let kernel: IJupyterServer.IKernelIFrame;

// TODO: more generic kernel instantiation
if (name === 'pyp5') {
kernel = new PyP5KernelIFrame({ id, sendMessage, sessionId: id });
} else {
kernel = new KernelIFrame({ id, sendMessage, sessionId: id });
}
this._kernels.set(id, kernel);

socket.on('message', (message: string | ArrayBuffer) => {
Expand All @@ -53,7 +61,7 @@ export class Kernels implements IJupyterServer.IRoutable {

const model = {
id,
name: 'p5.js'
name: name ?? 'p5.js'
};
return model;
}
Expand Down Expand Up @@ -81,13 +89,28 @@ export class Kernels implements IJupyterServer.IRoutable {
return new Response(null);
}

private _kernels = new ObservableMap<KernelIFrame>();
private _kernels = new ObservableMap<IJupyterServer.IKernelIFrame>();
}

/**
* A namespace for Kernels statics.
*/
export namespace Kernels {
/**
* Options to start a new options.
*/
export interface IStartOptions {
/**
* The kernel id.
*/
id: string;

/**
* The kernel name.
*/
name?: string;
}

/**
* The base url for the Kernels manager
*/
Expand Down
2 changes: 1 addition & 1 deletion src/server/kernel.ts → src/server/kernels/kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import p5 from '!!raw-loader!p5/lib/p5.min.js';

import p5Sound from '!!raw-loader!p5/lib/addons/p5.sound.min.js';

import { IJupyterServer } from '../tokens';
import { IJupyterServer } from '../../tokens';

/**
* A kernel that executes code in an IFrame.
Expand Down
Loading