Home Reference Source
public class | source

JupyterContext

Extends:

Context → JupyterContext

An execution context using Jupyter kernels

This class of execution context acts as a bridge between Stencila and Jupyter kernels. It exposes methods of the Stencila Context API e.g. executeEval which delegate execution to a Jupyter kernel. This is done via the Jupyter Messageing Protocol (JMP) over ZeroMQ sockets.

The discover static method should be called initially to find all Jupyter kernels currently installed on the machine and update JupyterContext.spec.kernels:

JupyterContext.discover()

New Jupyter execution contexts can be constructed using the language option which will search for a kernel with a matching lowercased language property:

new JupyterContext({language:'r'})

Alternively, you can specify a kernel directly:

new JupyterContext({kernel:'ir'})

See https://github.com/jupyter/jupyter/wiki/Jupyter-kernels for a list of available Jupyter kernels.

Many thanks to the nteract community for kernelspecs and spawnteract, and to Nicolas Riesco for (jmp)[https://github.com/n-riesco/jmp], all of which made this implementation far easier!

Static Method Summary

Static Public Methods
public static

Discover Jupyter kernels on the current machine

Constructor Summary

Public Constructor
public

constructor(options: Object)

Construct a Jupyter execution context

Member Summary

Public Members
public

debug: *

public

kernel: *

public

timeout: *

Private Members
private

_config: *

private
private
private
private
private

_requests: {}

private
private
private

_spec: *

Method Summary

Public Methods
public

async execute(cell: *): *

Execute a cell

public

Finalize the context

public

Initialize the context

Private Methods
private

_request(requestType: String, content: Object, responseTypes: String): Promise

Send a request message to the kernal

private

_response(response: Message)

Receive a response message from the kernel

private

Convert a "MIME bundle" within a JMP message (e.g.

Inherited Summary

From class Context
public get

host: *

public get

id: *

public get

location: {"context": *, "host": *, "machine": *}

public get

name: *

private

_host: *

private
private

_name: *

private
public

async compile(cell: *): {"source": *, "expr": *, "global": *, "options": *, "inputs": *, "outputs": *, "messages": *}

public

async evaluate(node: *): *

public

async execute(cell: *): *

public

async libraries(): {}

public

async pack(value: *): *

public

async packPackage(value: *): {"type": *, "data": *}

public

async packPointer(objectPattern: {"type": *, "name": *, "preview": *}): {"type": *, "path": *, "preview": *}

public

async provide(what: *): *

public

async resolve(what: *): *

public

async unpack(node: Object): [type]

Unpack a data node into a native data value

public

async unpackPackage(pkg: *): *

public

async unpackPointer(pointer: *): *

Static Public Methods

public static discover(): Promise source

Discover Jupyter kernels on the current machine

Looks for Jupyter kernels that have been installed on the system and puts that list in JupyterContext.spec.kernels so that peers know the capabilities of this "meta-context".

Return:

Promise

A promise

Public Constructors

public constructor(options: Object) source

Construct a Jupyter execution context

Override:

Context#constructor

Params:

NameTypeAttributeDescription
options Object

Options for specifying which kernel to use

Public Members

public debug: * source

public kernel: * source

public timeout: * source

Private Members

private _config: * source

private _connectionFile: * source

private _ioSocket: * source

private _kernelInfo: * source

private _process: * source

private _requests: {} source

private _sessionId: * source

private _shellSocket: * source

private _spec: * source

Public Methods

public async execute(cell: *): * source

Execute a cell

For cells with expr: true utilises user_expressions property of an execute_request to evaluate expression side-effect free.

Override:

Context#execute

Params:

NameTypeAttributeDescription
cell *

Return:

*

public finalize(): Promise source

Finalize the context

Return:

Promise

A resolved promise

public initialize(): Promise source

Initialize the context

Return:

Promise

A promise

Private Methods

private _request(requestType: String, content: Object, responseTypes: String): Promise source

Send a request message to the kernal

Params:

NameTypeAttributeDescription
requestType String

Type of request e.g. 'execute'

content Object

Content of message

responseTypes String

Types of response message to resolve

Return:

Promise

Promise resolving to the {request, response} messages

private _response(response: Message) source

Receive a response message from the kernel

Params:

NameTypeAttributeDescription
response Message

Response message

private _unbundle(bundle: Object): Promise source

Convert a "MIME bundle" within a JMP message (e.g. a execute_result or display data message) into a data node e.g. {'text/plain': 'Hello'} to {type: 'string', data: 'Hello'}

Params:

NameTypeAttributeDescription
bundle Object

A JMP MIME bundle

Return:

Promise

Promise resolving to a data node