kernels | test node meta kernel | Cell 21 | Search

The code imports various modules and functions, including a utility function to parse version numbers, and defines a nodeKernel function that uses the imported modules to create a Node.js kernel. The nodeKernel function is then exported as a module, making it available for use in other parts of the application.

Run example

npm run import -- "node wire kernel"

node wire kernel

var importer = require('../Core');
var {nodeJson, notebookJson} = importer.import("get kernel json");
var {wireKernel} = importer.import("jupyter wire kernel");
var {nativeKernelInfo} = importer.import("get kernel info");
var {processMetaKernel} = importer.import("process meta kernel");
var {languageInterface} = importer.import("get kernel language");

function getVersion(str) {
    return str.split('.').map(v => parseInt(v, 10)).join('.')
}

function nodeKernel(config, options) {
    return wireKernel({
        kernel_config: config,
        start_config: options,
        install_config: notebookJson,
        kernel_info: {
            banner: 'Node JS',
            // TODO: automatically create this from installation intructions
            help_links: ['https://nodejs.org']
        },
        language_info: {
            name: 'node',
            file_extension: '.js',
            mimetype: 'application/javascript',
            version: getVersion(process.versions.node),
            codemirror_mode: 'javascript'
        },
        child_process: [],
    });
}

module.exports = nodeKernel;

What the code could have been:

const { importModules } = require('../Core');

const { nodeJson, notebookJson } = importModules('get kernel json');
const { wireKernel } = importModules('jupyter wire kernel');
const { nativeKernelInfo } = importModules('get kernel info');
const { processMetaKernel } = importModules('process meta kernel');
const { languageInterface } = importModules('get kernel language');

/**
 * Extracts the version from a string in the format'major.minor.patch'
 * @param {string} str - The version string
 * @returns {string} The version in the format'major.minor.patch'
 */
function getVersion(str) {
  const versionParts = str.split('.');
  const version = versionParts.map((part) => parseInt(part, 10));
  return version.join('.');
}

/**
 * Creates a Node.js kernel configuration
 * @param {object} config - The kernel configuration
 * @param {object} options - The start configuration
 * @returns {object} The Node.js kernel configuration
 */
function nodeKernel(config, options) {
  const languageInfo = {
    name: 'node',
    fileExtension: '.js',
    mimetype: 'application/javascript',
    version: getVersion(process.versions.node),
    codemirrorMode: 'javascript',
  };

  const kernelInfo = {
    banner: 'Node JS',
    helpLinks: ['https://nodejs.org'], // TODO: automatically create this from installation instructions
  };

  return wireKernel({
    kernelConfig: config,
    startConfig: options,
    installConfig: notebookJson,
    kernelInfo,
    languageInfo,
    childProcess: [],
  });
}

module.exports = nodeKernel;

Code Breakdown

Importing Modules

var importer = require('../Core');
var {nodeJson, notebookJson} = importer.import('get kernel json');
var {wireKernel} = importer.import('jupyter wire kernel');
var {nativeKernelInfo} = importer.import('get kernel info');
var {processMetaKernel} = importer.import('process meta kernel');
var {languageInterface} = importer.import('get kernel language');

Utility Function

function getVersion(str) {
    return str.split('.').map(v => parseInt(v, 10)).join('.');
}

nodeKernel Function

function nodeKernel(config, options) {
    return wireKernel({
        kernel_config: config,
        start_config: options,
        install_config: notebookJson,
        kernel_info: {
            banner: 'Node JS',
            help_links: ['https://nodejs.org']
        },
        language_info: {
            name: 'node',
            file_extension: '.js',
            mimetype: 'application/javascript',
            version: getVersion(process.versions.node),
            codemirror_mode: 'javascript'
        },
        child_process: [],
    });
}

Exporting the nodeKernel Function

module.exports = nodeKernel;