kernels | Cell 15 | TODO: wire meta kernel | Search

The code imports various modules and functions, defines two helper functions addCB and wireKernel, and creates a wireKernel class that extends the kernel object with methods to handle requests. The wireKernel class and its interface are then exported as a module.

Run example

npm run import -- "TODO jupyter wire kernel"

TODO jupyter wire kernel

var importer = require('../Core');
var extend = importer.import("extend prototype")
var {
    setupSockets, parseMessage, collapseMessage
} = importer.import("decode encode ipython zmq protocol");
var {nativeMetaKernel, metaKernelInterface} = importer.import("jupyter meta kernel");
var {processMetaKernel} = importer.import("process meta kernel");
var {wireMetaKernel} = importer.import("wire meta kernel");

// TODO: move this to patterns, improve this flow
function addCB(og, cb) {
    return cb(og.apply(null, Array.from(arguments).slice(2)));
}

function wireKernel(kernel) {
    console.log('starting wire kernel');
    console.log(kernel.kernel_info);
    console.log(this.kernel_info);
    
    var wire_kernel = extend(kernel, wireKernelInterface);
    console.log(wire_kernel.kernel_info);
    
    var meta_kernel = extend(wire_kernel, wireMetaKernel);
    console.log(meta_kernel.kernel_info);
    
    return new processMetaKernel(meta_kernel);
}

wireKernel.prototype = {
    input_request: (kernel, request) => {
        // TODO: finish this
        //this.onReplies[response.header.msg_id] = onReply;
    },
    comm_info_request: (kernel, request) => {
        kernel.do_respond({status: {execution_state: 'busy'}});
        kernel.do_respond({status: {execution_state: 'idle'}});
        kernel.do_respond({comm_info_reply: {comms: {}}});
    },
    kernel_info_request: (kernel, request) => {
        kernel.do_respond({status: {execution_state: 'busy'}});
        kernel.do_respond({status: {execution_state: 'idle'}});
        kernel.do_respond({kernel_info_reply: kernel.kernel_info});
    },
}

module.exports = {
    wireKernelInterface,
    wireKernel
}

What the code could have been:

// Import required modules and functions
const { importFunctions, importModules } = require('../Core');
const {
  setupSockets,
  parseMessage,
  collapseMessage,
} = importModules('decode encode ipython zmq protocol');
const {
  nativeMetaKernel,
  metaKernelInterface,
} = importModules('jupyter meta kernel');
const processMetaKernel = importModules('process meta kernel');
const wireMetaKernel = importModules('wire meta kernel');

// Define a function to add a callback to an existing function
/**
 * Adds a callback to an existing function.
 * @param {Function} og The original function.
 * @param {Function} cb The callback function.
 * @returns {Function} A new function that calls the original function with the given callback.
 */
function addCB(og, cb) {
  return (args) => cb(og.apply(null, args));
}

// Define the wire kernel function
/**
 * Wires a Jupyter kernel using the provided interface and protocols.
 * @param {Object} kernel The kernel to wire.
 * @returns {Object} A new kernel instance.
 */
function wireKernel(kernel) {
  console.log('Starting wire kernel');
  console.log(kernel.kernel_info);
  console.log(processMetaKernel.kernel_info);

  // Create a new kernel instance with the wire kernel interface
  const wireKernelInterface = metaKernelInterface;
  const wire_kernel = addCB(kernel, extend(wireKernelInterface));

  // Create a new kernel instance with the wire meta kernel interface
  const wireMetaKernelInterface = wireMetaKernel;
  const meta_kernel = addCB(wire_kernel, extend(wireMetaKernelInterface));

  return new processMetaKernel(meta_kernel);
}

// Define the wire kernel prototype
wireKernel.prototype = {
  /**
   * Handles an input request from the kernel.
   * @param {Object} kernel The kernel instance.
   * @param {Object} request The input request.
   */
  input_request: async (kernel, request) => {
    // TODO: Finish this
    console.log('Input request received');
  },

  /**
   * Handles a comm info request from the kernel.
   * @param {Object} kernel The kernel instance.
   * @param {Object} request The comm info request.
   */
  comm_info_request: async (kernel, request) => {
    await kernel.do_respond({ status: { execution_state: 'busy' } });
    await kernel.do_respond({ status: { execution_state: 'idle' } });
    await kernel.do_respond({ comm_info_reply: { comms: {} } });
  },

  /**
   * Handles a kernel info request from the kernel.
   * @param {Object} kernel The kernel instance.
   * @param {Object} request The kernel info request.
   */
  kernel_info_request: async (kernel, request) => {
    await kernel.do_respond({ status: { execution_state: 'busy' } });
    await kernel.do_respond({ status: { execution_state: 'idle' } });
    await kernel.do_respond({ kernel_info_reply: kernel.kernel_info });
  },
};

// Export the wire kernel interface and function
module.exports = {
  wireKernelInterface: metaKernelInterface,
  wireKernel,
};

Code Breakdown

Importing Modules

The code starts by importing various modules and functions from the ../Core module.

var importer = require('../Core');
var extend = importer.import('extend prototype')
var {
    setupSockets, parseMessage, collapseMessage
} = importer.import('decode encode ipython zmq protocol');
var {nativeMetaKernel, metaKernelInterface} = importer.import('jupyter meta kernel');
var {processMetaKernel} = importer.import('process meta kernel');
var {wireMetaKernel} = importer.import('wire meta kernel');

Helper Functions

Two helper functions, addCB and wireKernel, are defined.

function addCB(og, cb) {
    return cb(og.apply(null, Array.from(arguments).slice(2)));
}

function wireKernel(kernel) {
    //...
}

The addCB function takes two arguments: og and cb. It applies the cb function to the og function, passing the remaining arguments to cb.

WireKernel Class

The wireKernel class is defined, which extends the kernel object.

wireKernel.prototype = {
    //...
}

The class has three methods: input_request, comm_info_request, and kernel_info_request. These methods take two arguments: kernel and request. They are used to handle requests from the kernel.

Exporting the wireKernel Class

Finally, the wireKernel class and its interface are exported as a module.

module.exports = {
    wireKernelInterface,
    wireKernel
}

Functions and Variables