JupyterLab Services
Javascript client for the Jupyter services REST APIs
API Docs
REST API Docs
Note: All functions and classes using the REST API allow an ajaxOptions
parameter to configure requests.
Package Install
Prerequisites
npm install --save @jupyterlab/services
conda install notebook
Source Build
Prerequisites
git clone https://github.com/jupyterlab/services.git
cd services
npm install
npm run build
conda install notebook
Rebuild
npm run clean
npm run build
Run Tests
Follow the source build instructions first.
npm test
Build Docs
Follow the source build instructions first.
npm run docs
Navigate to docs/index.html
.
Supported Runtimes
The runtime versions which are currently known to work are listed below.
Earlier versions may also work, but come with no guarantees.
- Node 0.12.7+
- IE 11+
- Firefox 32+
- Chrome 38+
Note: "requirejs" must be included in a global context for Comm targets.
This can be as a <script>
tag in the browser or by using the requirejs
package in node (npm install requirejs
and setting
global.requirejs = require('requirejs');
). See the examples
folder
for usage.
Starting the Notebook Server
Follow the package install instructions first.
The library requires a running Jupyter Notebook server, launched as:
python -m notebook --NotebookApp.allow_origin="*"
The origin can be specified directly instead of using *
if desired.
Bundling for the Browser
Follow the package install instructions first.
See examples/browser
for an example of using Webpack to bundle the library.
Note: Some browsers (such as IE11), require a polyfill for Promises.
The example demonstrates the use of the polyfill.
Usage from Node.js
Follow the package install instructions first.
npm install --save xmlhttprequest ws
Use XMLHttpRequest
and WebSocket
in the server settings (in ES5 syntax):
var services = require('@jupyterlab/services');
var ws = require('ws');
var xhr = require('xmlhttprequest');
var serverSettings = services.ServerConnection.makeSettings({
xhrFactory: function () { return new xhr.XMLHttpRequest(); },
wsFactory: function (url, protocol) { return new ws(url, protocol); }
});
Kernel.startNew({ serverSettings }).then(...);
See examples/node
for an example of using an ES5 node script.
Usage Examples
Note: This module is fully compatible with Node/Babel/ES6/ES5. The
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
import {
KernelMessage, Kernel
} from '@jupyterlab/services';
Kernel.listRunning().then(kernelModels => {
Kernel.connectTo(kernelModels[0].id).then((kernel) => {
console.log(kernel.name);
});
});
Kernel.getSpecs().then(kernelSpecs => {
console.log('Default spec:', kernelSpecs.default);
console.log('Available specs', Object.keys(kernelSpecs.kernelspecs));
let options: Kernel.IOptions = {
name: kernelSpecs.default
};
Kernel.startNew(options).then(kernel => {
let future = kernel.requestExecute({ code: 'a = 1' } );
future.done.then(() => {
console.log('Future is fulfilled');
});
future.onIOPub = (msg) => {
console.log(msg.content);
};
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);
});
});
kernel.interrupt().then(() => {
kernel.requestComplete({ code: 'impor', cursor_pos: 4 } ).then((reply) => {
console.log(reply.content.matches);
});
});
kernel.statusChanged.connect((status) => {
console.log('status', status);
});
kernel.shutdown().then(() => {
console.log('Kernel shut down');
});
});
});
Session
import {
Session
} from '@jupyterlab/services';
Session.listRunning().then(sessionModels => {
Session.connectTo(sessionModels[0].id).then((session) => {
console.log(session.kernel.name);
});
});
let options = {
kernelName: 'python',
path: '/tmp/foo.ipynb'
};
Session.startNew(options).then(session => {
let future = session.kernel.requestExecute({ code: 'a = 1' });
future.done.then(() => {
console.log('Future is fulfilled');
});
session.setPath('/local/bar.ipynb').then(() => {
console.log('Session renamed to', session.path);
});
session.terminated.connect(() => {
console.log('session died');
});
session.shutdown().then(() => {
console.log('session closed');
});
});
Comm
import {
Kernel
} from '@jupyterlab/services';
Kernel.getSpecs().then(kernelSpecs => {
return Kernel.startNew({
name: kernelSpecs.default,
});
}).then(kernel => {
let comm = kernel.connectToComm('test');
comm.open('initial state');
comm.send('test');
comm.close('bye');
});
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);
};
comm.onClose = (msg) => {
console.log(msg);
};
});
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
import {
ContentsManager
} from '@jupyterlab/services';
let contents = new ContentsManager();
contents.newUntitled({ path: '/foo', type: 'file', ext: 'py' }).then(
(model) => {
console.log('new file:', model.path);
}
);
contents.get('/foo/bar').then(
(model) => {
console.log('files:', model.content);
}
);
contents.rename('/foo/bar.txt', '/foo/baz.txt');
contents.save('/foo/test.ipynb');
contents.delete('/foo/bar.txt');
contents.copy('/foo/bar.txt', '/baz').then((model) => {
console.log('new path', model.path);
});
contents.createCheckpoint('/foo/bar.ipynb').then((model) => {
let checkpoint = model;
contents.restoreCheckpoint('/foo/bar.ipynb', checkpoint.id);
contents.deleteCheckpoint('/foo/bar.ipynb', checkpoint.id);
});
contents.listCheckpoints('/foo/bar.txt').then((models) => {
console.log(models[0].id);
});
Configuration
import {
ConfigWithDefaults, ConfigSection
} from '@jupyterlab/services';
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'));
config.set('foo', 'bar').then(data => {
console.log(data);
});
});
Terminals
import {
TerminalSession
} from '@jupyterlab/services';
TerminalSession.startNew().then(session => {
session.send({ type: 'stdin', content: ['foo'] });
});