Skip to content

Commit

Permalink
Merge pull request #7252 from jasongrout/sessionpersistence
Browse files Browse the repository at this point in the history
Rework kernel and session architecture
  • Loading branch information
Steven Silvester committed Dec 20, 2019
2 parents 0c2ef44 + f18e0fd commit 28cef06
Show file tree
Hide file tree
Showing 127 changed files with 7,356 additions and 7,289 deletions.
42 changes: 26 additions & 16 deletions examples/cell/src/index.ts
Expand Up @@ -10,7 +10,7 @@ import '@jupyterlab/cells/style/index.css';
import '@jupyterlab/theme-light-extension/style/index.css';
import '../index.css';

import { ClientSession, Toolbar } from '@jupyterlab/apputils';
import { SessionContext, Toolbar } from '@jupyterlab/apputils';

import { CodeCellModel, CodeCell } from '@jupyterlab/cells';

Expand All @@ -28,15 +28,25 @@ import {
standardRendererFactories as initialFactories
} from '@jupyterlab/rendermime';

import { SessionManager } from '@jupyterlab/services';
import {
SessionManager,
KernelManager,
KernelSpecManager
} from '@jupyterlab/services';

import { CommandRegistry } from '@lumino/commands';

import { BoxPanel, Widget } from '@lumino/widgets';

function main(): void {
const manager = new SessionManager();
const session = new ClientSession({ manager, name: 'Example' });
const kernelManager = new KernelManager();
const specsManager = new KernelSpecManager();
const sessionManager = new SessionManager({ kernelManager });
const sessionContext = new SessionContext({
sessionManager,
specsManager,
name: 'Example'
});
const mimeService = new CodeMirrorMimeTypeService();

// Initialize the command registry with the bindings.
Expand All @@ -60,23 +70,23 @@ function main(): void {
model: new CodeCellModel({})
}).initializeState();

// Handle the mimeType for the current kernel.
session.kernelChanged.connect(() => {
void session.kernel.ready.then(() => {
const lang = session.kernel.info.language_info;
// Handle the mimeType for the current kernel asynchronously.
sessionContext.kernelChanged.connect(() => {
void sessionContext.session?.kernel?.info.then(info => {
const lang = info.language_info;
const mimeType = mimeService.getMimeTypeByLanguage(lang);
cellWidget.model.mimeType = mimeType;
});
});

// Use the default kernel.
session.kernelPreference = { autoStartDefault: true };
sessionContext.kernelPreference = { autoStartDefault: true };

// Set up a completer.
const editor = cellWidget.editor;
const model = new CompleterModel();
const completer = new Completer({ editor, model });
const connector = new KernelConnector({ session });
const connector = new KernelConnector({ session: sessionContext.session });
const handler = new CompletionHandler({ completer, connector });

// Set the handler's editor.
Expand All @@ -88,10 +98,10 @@ function main(): void {
// Create a toolbar for the cell.
const toolbar = new Toolbar();
toolbar.addItem('spacer', Toolbar.createSpacerItem());
toolbar.addItem('interrupt', Toolbar.createInterruptButton(session));
toolbar.addItem('restart', Toolbar.createRestartButton(session));
toolbar.addItem('name', Toolbar.createKernelNameItem(session));
toolbar.addItem('status', Toolbar.createKernelStatusItem(session));
toolbar.addItem('interrupt', Toolbar.createInterruptButton(sessionContext));
toolbar.addItem('restart', Toolbar.createRestartButton(sessionContext));
toolbar.addItem('name', Toolbar.createKernelNameItem(sessionContext));
toolbar.addItem('status', Toolbar.createKernelStatusItem(sessionContext));

// Lay out the widgets.
const panel = new BoxPanel();
Expand Down Expand Up @@ -120,7 +130,7 @@ function main(): void {
}
});
commands.addCommand('run:cell', {
execute: () => CodeCell.execute(cellWidget, session)
execute: () => CodeCell.execute(cellWidget, sessionContext)
});

commands.addKeyBinding({
Expand All @@ -135,7 +145,7 @@ function main(): void {
});

// Start up the kernel.
void session.initialize().then(() => {
void sessionContext.initialize().then(() => {
console.log('Example started!');
});
}
Expand Down
4 changes: 3 additions & 1 deletion examples/chrome-example-test.js
Expand Up @@ -20,9 +20,11 @@ async function main() {

const handleMessage = async msg => {
const text = msg.text();
console.log(`>> ${text}`);
if (msg.type() === 'error') {
console.log(`ERROR>> ${text}`);
errored = true;
} else {
console.log(`>> ${text}`);
}
const lower = text.toLowerCase();
if (lower === 'example started!' || lower === 'test complete!') {
Expand Down
14 changes: 6 additions & 8 deletions examples/notebook/src/commands.ts
Expand Up @@ -121,26 +121,24 @@ export const SetupCommands = (
});
commands.addCommand(cmdIds.interrupt, {
label: 'Interrupt',
execute: async () => {
if (nbWidget.context.session.kernel) {
await nbWidget.context.session.kernel.interrupt();
}
}
execute: async () =>
nbWidget.context.sessionContext.session?.kernel?.interrupt()
});
commands.addCommand(cmdIds.restart, {
label: 'Restart Kernel',
execute: () => nbWidget.context.session.restart()
execute: async () =>
nbWidget.context.sessionContext.session?.kernel?.restart()
});
commands.addCommand(cmdIds.switchKernel, {
label: 'Switch Kernel',
execute: () => nbWidget.context.session.selectKernel()
execute: async () => nbWidget.context.sessionContext.selectKernel()
});
commands.addCommand(cmdIds.runAndAdvance, {
label: 'Run and Advance',
execute: () => {
return NotebookActions.runAndAdvance(
nbWidget.content,
nbWidget.context.session
nbWidget.context.sessionContext
);
}
});
Expand Down
4 changes: 3 additions & 1 deletion examples/notebook/src/index.ts
Expand Up @@ -111,7 +111,9 @@ function createApp(manager: ServiceManager.IManager): void {
nbWidget.content.activeCell && nbWidget.content.activeCell.editor;
const model = new CompleterModel();
const completer = new Completer({ editor, model });
const connector = new KernelConnector({ session: nbWidget.session });
const connector = new KernelConnector({
session: nbWidget.sessionContext.session
});
const handler = new CompletionHandler({ completer, connector });

// Set the handler's editor.
Expand Down
41 changes: 25 additions & 16 deletions examples/test_examples.py
Expand Up @@ -5,46 +5,55 @@
in ../packages/services/examples. We import each of the applications
and add instrument them with a puppeteer test that makes sure
there are no console errors or uncaught errors prior to a sentinel
string being printed.
string being printed (see chrome-example-test.js for the sentinel strings
checked before the browser.close() call).
"""
import argparse
import glob
import os.path as osp
import subprocess
import sys
import tempfile

here = osp.abspath(osp.dirname(__file__))

def header(path):
def header(path, cwd = ''):
test_name = osp.basename(path)
print('\n'.join((
'\n',
'*' * 40,
'Starting %s test' % test_name,
'Starting %s test in %s' % (test_name, cwd),
'*' * 40
)), flush=True)

def main():
parser = argparse.ArgumentParser()
parser.add_argument("--testPath", help="paths containing this string are tested")
args = parser.parse_args()

paths = [i for i in glob.glob('%s/*' % here) if osp.isdir(i)]

services_dir = osp.abspath(osp.join(here, '../packages/services/examples'))
paths += [i for i in glob.glob('%s/*' % services_dir)]
if args.testPath:
paths = [p for p in paths if args.testPath in p]

print('Testing %s'%paths)

count = 0
for path in sorted(paths):
if osp.basename(path) == 'node':
header(path)
runner = osp.join(path, 'main.py')
subprocess.check_call([sys.executable, runner])
count += 1
continue

if not osp.exists(osp.join(path, 'main.py')):
continue

count += 1
header(path)
runner = osp.join(here, 'example_check.py')
subprocess.check_call([sys.executable, runner, path])
with tempfile.TemporaryDirectory() as cwd:
header(path)
runner = osp.join(path, 'main.py')
subprocess.check_call([sys.executable, runner])
count += 1
elif osp.exists(osp.join(path, 'main.py')):
with tempfile.TemporaryDirectory() as cwd:
header(path)
runner = osp.join(here, 'example_check.py')
subprocess.check_call([sys.executable, runner, path], cwd=cwd)
count += 1

print('\n\n%s tests complete!' % count)

Expand Down
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -69,11 +69,13 @@
"remove:package": "node buildutils/lib/remove-package.js",
"remove:sibling": "node buildutils/lib/remove-package.js",
"test": "lerna run test --scope \"@jupyterlab/test-*\" --concurrency 1 --stream",
"test:all": "lerna run test --scope \"@jupyterlab/test-*\" --concurrency 1 --stream --no-bail",
"test:chrome": "lerna run test:chrome --scope \"@jupyterlab/test-*\" --concurrency 1 --stream",
"test:chrome-headless": "lerna run test:chrome-headless --scope \"@jupyterlab/test-*\" --concurrency 1 --stream",
"test:examples": "python examples/test_examples.py",
"test:firefox": "lerna run test:firefox --scope \"@jupyterlab/test-*\" --concurrency 1 --stream",
"test:ie": "lerna run test:ie --scope \"@jupyterlab/test-*\" --concurrency 1 --stream",
"test:summary": "lerna run test --scope \"@jupyterlab/test-*\" --parallel --no-bail | grep -Ei '.* test.*(failed|passed|total|completed|skipped)' | sort",
"tslint": "tslint --fix -c tslint.json --project tsconfigbase.json '**/*{.ts,.tsx}'",
"tslint:check": "tslint -c tslint.json --project tsconfigbase.json '**/*{.ts,.tsx}'",
"update:dependency": "node buildutils/lib/update-dependency.js --lerna",
Expand Down
2 changes: 1 addition & 1 deletion packages/apputils/src/index.ts
@@ -1,7 +1,6 @@
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

export * from './clientsession';
export * from './clipboard';
export * from './collapse';
export * from './commandlinker';
Expand All @@ -14,6 +13,7 @@ export * from './inputdialog';
export * from './mainareawidget';
export * from './printing';
export * from './sanitizer';
export * from './sessioncontext';
export * from './spinner';
export * from './splash';
export * from './styling';
Expand Down

0 comments on commit 28cef06

Please sign in to comment.