|
@@ -114,237 +114,12 @@ examples below are written in TypeScript using ES6 syntax. Simply
|
|
|
omit the type declarations when using a language other than TypeScript.
|
|
|
A translator such as Babel can be used to convert from ES6 -> ES5.
|
|
|
|
|
|
-**Kernel**
|
|
|
-
|
|
|
-```typescript
|
|
|
-import { KernelMessage, Kernel } from '@jupyterlab/services';
|
|
|
-
|
|
|
-// Get a list of available kernels and connect to one.
|
|
|
-Kernel.listRunning().then(kernelModels => {
|
|
|
- const kernel = Kernel.connectTo(kernelModels[0]);
|
|
|
- console.log(kernel.name);
|
|
|
-});
|
|
|
-
|
|
|
-// Get info about the available kernels and start a new one.
|
|
|
-Kernel.getSpecs().then(kernelSpecs => {
|
|
|
- console.log('Default spec:', kernelSpecs.default);
|
|
|
- console.log('Available specs', Object.keys(kernelSpecs.kernelspecs));
|
|
|
- // use the default name
|
|
|
- let options: Kernel.IOptions = {
|
|
|
- name: kernelSpecs.default
|
|
|
- };
|
|
|
- Kernel.startNew(options).then(kernel => {
|
|
|
- // Execute and handle replies.
|
|
|
- let future = kernel.requestExecute({ code: 'a = 1' });
|
|
|
- future.done.then(() => {
|
|
|
- console.log('Future is fulfilled');
|
|
|
- });
|
|
|
- future.onIOPub = msg => {
|
|
|
- console.log(msg.content); // Print rich output data.
|
|
|
- };
|
|
|
-
|
|
|
- // Restart the kernel and then send an inspect message.
|
|
|
- kernel.restart().then(() => {
|
|
|
- let request: KernelMessage.IInspectRequest = {
|
|
|
- code: 'hello',
|
|
|
- cursor_pos: 4,
|
|
|
- detail_level: 0
|
|
|
- };
|
|
|
- kernel.requestInspect(request).then(reply => {
|
|
|
- console.log(reply.content.data);
|
|
|
- });
|
|
|
- });
|
|
|
-
|
|
|
- // Interrupt the kernel and then send a complete message.
|
|
|
- kernel.interrupt().then(() => {
|
|
|
- kernel.requestComplete({ code: 'impor', cursor_pos: 4 }).then(reply => {
|
|
|
- console.log(reply.content.matches);
|
|
|
- });
|
|
|
- });
|
|
|
-
|
|
|
- // Register a callback for when the kernel changes state.
|
|
|
- kernel.statusChanged.connect(status => {
|
|
|
- console.log('status', status);
|
|
|
- });
|
|
|
-
|
|
|
- // Kill the kernel.
|
|
|
- kernel.shutdown().then(() => {
|
|
|
- console.log('Kernel shut down');
|
|
|
- });
|
|
|
- });
|
|
|
-});
|
|
|
-```
|
|
|
-
|
|
|
-**Session**
|
|
|
-
|
|
|
-```typescript
|
|
|
-import { Session } from '@jupyterlab/services';
|
|
|
-
|
|
|
-// Get a list of available sessions and connect to one.
|
|
|
-Session.listRunning().then(sessionModels => {
|
|
|
- const session = Session.connectTo(sessionModels[0]);
|
|
|
- console.log(session.kernel.name);
|
|
|
-});
|
|
|
-
|
|
|
-// Start a new session.
|
|
|
-let options = {
|
|
|
- kernelName: 'python',
|
|
|
- path: '/tmp/foo.ipynb'
|
|
|
-};
|
|
|
-
|
|
|
-Session.startNew(options).then(session => {
|
|
|
- // Execute and handle replies on the kernel.
|
|
|
- let future = session.kernel.requestExecute({ code: 'a = 1' });
|
|
|
- future.done.then(() => {
|
|
|
- console.log('Future is fulfilled');
|
|
|
- });
|
|
|
-
|
|
|
- // Rename the session.
|
|
|
- session.setPath('/local/bar.ipynb').then(() => {
|
|
|
- console.log('Session renamed to', session.path);
|
|
|
- });
|
|
|
-
|
|
|
- // Register a callback for when the session dies.
|
|
|
- session.terminated.connect(() => {
|
|
|
- console.log('session died');
|
|
|
- });
|
|
|
-
|
|
|
- // Kill the session.
|
|
|
- session.shutdown().then(() => {
|
|
|
- console.log('session closed');
|
|
|
- });
|
|
|
-});
|
|
|
-```
|
|
|
-
|
|
|
-**Comm**
|
|
|
-
|
|
|
-```typescript
|
|
|
-import { Kernel } from '@jupyterlab/services';
|
|
|
-
|
|
|
-// Create a comm from the server side.
|
|
|
-//
|
|
|
-// Get info about the available kernels and connect to one.
|
|
|
-Kernel.getSpecs()
|
|
|
- .then(kernelSpecs => {
|
|
|
- return Kernel.startNew({
|
|
|
- name: kernelSpecs.default
|
|
|
- });
|
|
|
- })
|
|
|
- .then(kernel => {
|
|
|
- let comm = kernel.createComm('test');
|
|
|
- comm.open('initial state');
|
|
|
- comm.send('test');
|
|
|
- comm.close('bye');
|
|
|
- });
|
|
|
-
|
|
|
-// Create a comm from the client side.
|
|
|
-Kernel.getSpecs()
|
|
|
- .then(kernelSpecs => {
|
|
|
- return Kernel.startNew({
|
|
|
- name: kernelSpecs.default
|
|
|
- });
|
|
|
- })
|
|
|
- .then(kernel => {
|
|
|
- kernel.registerCommTarget('test2', (comm, commMsg) => {
|
|
|
- if (commMsg.content.target_name !== 'test2') {
|
|
|
- return;
|
|
|
- }
|
|
|
- comm.onMsg = msg => {
|
|
|
- console.log(msg); // 'hello'
|
|
|
- };
|
|
|
- comm.onClose = msg => {
|
|
|
- console.log(msg); // 'bye'
|
|
|
- };
|
|
|
- });
|
|
|
-
|
|
|
- let code = [
|
|
|
- 'from ipykernel.comm import Comm',
|
|
|
- 'comm = Comm(target_name="test2")',
|
|
|
- 'comm.send(data="hello")',
|
|
|
- 'comm.close(data="bye")'
|
|
|
- ].join('\n');
|
|
|
- kernel.requestExecute({ code: code });
|
|
|
- });
|
|
|
-```
|
|
|
-
|
|
|
-**Contents**
|
|
|
-
|
|
|
-```typescript
|
|
|
-import { ContentsManager } from '@jupyterlab/services';
|
|
|
-
|
|
|
-let contents = new ContentsManager();
|
|
|
-
|
|
|
-// Create a new python file.
|
|
|
-contents.newUntitled({ path: '/foo', type: 'file', ext: 'py' }).then(model => {
|
|
|
- console.log('new file:', model.path);
|
|
|
-});
|
|
|
-
|
|
|
-// Get the contents of a directory.
|
|
|
-contents.get('/foo/bar').then(model => {
|
|
|
- console.log('files:', model.content);
|
|
|
-});
|
|
|
-
|
|
|
-// Rename a file.
|
|
|
-contents.rename('/foo/bar.txt', '/foo/baz.txt');
|
|
|
-
|
|
|
-// Save a file.
|
|
|
-contents.save('/foo/test.ipynb');
|
|
|
-
|
|
|
-// Delete a file.
|
|
|
-contents.delete('/foo/bar.txt');
|
|
|
-
|
|
|
-// Copy a file.
|
|
|
-contents.copy('/foo/bar.txt', '/baz').then(model => {
|
|
|
- console.log('new path', model.path);
|
|
|
-});
|
|
|
-
|
|
|
-// Create a checkpoint.
|
|
|
-contents.createCheckpoint('/foo/bar.ipynb').then(model => {
|
|
|
- let checkpoint = model;
|
|
|
-
|
|
|
- // Restore a checkpoint.
|
|
|
- contents.restoreCheckpoint('/foo/bar.ipynb', checkpoint.id);
|
|
|
-
|
|
|
- // Delete a checkpoint.
|
|
|
- contents.deleteCheckpoint('/foo/bar.ipynb', checkpoint.id);
|
|
|
-});
|
|
|
-
|
|
|
-// List checkpoints for a file.
|
|
|
-contents.listCheckpoints('/foo/bar.txt').then(models => {
|
|
|
- console.log(models[0].id);
|
|
|
-});
|
|
|
-```
|
|
|
-
|
|
|
-**Configuration**
|
|
|
-
|
|
|
-```typescript
|
|
|
-import { ConfigWithDefaults, ConfigSection } from '@jupyterlab/services';
|
|
|
-
|
|
|
-// The base url of the Jupyter server.
|
|
|
-
|
|
|
-ConfigSection.create({ name: 'notebook' }).then(section => {
|
|
|
- let config = new ConfigWithDefaults({
|
|
|
- section,
|
|
|
- defaults: { default_cell_type: 'code' },
|
|
|
- className: 'Notebook'
|
|
|
- });
|
|
|
- console.log(config.get('default_cell_type')); // 'code'
|
|
|
- config.set('foo', 'bar').then(data => {
|
|
|
- console.log(data); // "{ 'foo': 'bar' }"
|
|
|
- });
|
|
|
-});
|
|
|
-```
|
|
|
-
|
|
|
-**Terminals**
|
|
|
-
|
|
|
-```typescript
|
|
|
-import { TerminalSession } from '@jupyterlab/services';
|
|
|
-
|
|
|
-// Create a named terminal session and send some data.
|
|
|
-TerminalSession.startNew().then(session => {
|
|
|
- session.send({ type: 'stdin', content: ['foo'] });
|
|
|
-});
|
|
|
-```
|
|
|
+- [Comms](./examples/browser/src/comm.ts)
|
|
|
+- [Config](./examples/browser/src/config.ts)
|
|
|
+- [Contents](./examples/browser/src/contents.ts)
|
|
|
+- [Kernel](./examples/browser/src/kernel.ts)
|
|
|
+- [Session](./examples/browser/src/session.ts)
|
|
|
+- [Terminal](./examples/browser/src/terminal.ts)
|
|
|
|
|
|
## Overview
|
|
|
|