kernels | test notebook kernel json | jupyter meta kernel | Search

This code exports a set of functions that generate JSON objects for different kernel types used in kernel communication, including Python, Node.js, Jupyter Notebook, Bash, and Processing. Each function combines a base configuration with user-provided kernel JSON data to create a complete JSON object for use in kernel communication.

Run example

npm run import -- "custom kernel json"

custom kernel json

var importer = require('../Core');
var {jsonInterface, wireJson, pathJson} = importer.import("notebook kernel json");

function pythonJson(kernel_json) {
    return pathJson(Object.assign({
        argv: ['python3', '-m', 'IPython.kernel', '-f', '{connection_file}']
    }, kernel_json))
}

// derrived from https://github.com/n-riesco/jp-babel/blob/master/lib/kernel.js

function nodeJson(kernel_json) {
    return pathJson(Object.assign({
        path: process.argv[2] || process.argv[0],
        argv: [kernel_json.path].concat(kernel_json.argv || kernel_json.args || [])
    }, kernel_json))
}

function notebookJson(kernel_json) {
    return jsonInterface(Object.assign({
        argv: ['npm', 'run', 'import', '--', 'get javascript kernel',
               '["{connection_file}"]'],
    }, kernel_json))
}

function bashJson(kernel_json) {
    return jsonInterface(Object.assign({
        argv: ['npm', 'run', 'import', '--', 'get bash kernel',
               '["{connection_file}"]'],
    }, kernel_json))
}

function processingJson(kernel_json) {
    return jsonInterface(Object.assign({
        argv: ['npm', 'run', 'import', '--', 'get processing kernel',
               '["{connection_file}"]'],
    }, kernel_json))
}

module.exports = {
    pythonJson,
    nodeJson,
    notebookJson,
    bashJson,
    processingJson
};

What the code could have been:

// Import required modules and functions
const importer = require('../Core');
const { jsonInterface, wireJson, pathJson } = importer.import('notebook kernel json');

// Implement a base function for generating kernel JSON
function generateKernelJson(kernel_json, kernelType) {
    // Use a switch statement to generate the kernel JSON based on the type
    switch (kernelType) {
        case 'python':
            return generatePythonKernelJson(kernel_json);
        case 'node':
            return generateNodeKernelJson(kernel_json);
        case 'notebook':
            return generateNotebookKernelJson(kernel_json);
        case 'bash':
            return generateBashKernelJson(kernel_json);
        case 'processing':
            return generateProcessingKernelJson(kernel_json);
        default:
            throw new Error(`Unsupported kernel type: ${kernelType}`);
    }
}

// Specific kernel JSON generators
function generatePythonKernelJson(kernel_json) {
    // Refactored to simplify the code and reduce duplication
    return pathJson({
        argv: ['python3', '-m', 'IPython.kernel', '-f', '{connection_file}'],
       ...kernel_json,
    });
}

function generateNodeKernelJson(kernel_json) {
    return pathJson({
        path: process.argv[2] || process.argv[0],
        argv: [kernel_json.path].concat(kernel_json.argv || kernel_json.args || []),
       ...kernel_json,
    });
}

function generateNotebookKernelJson(kernel_json) {
    return jsonInterface({
        argv: [
            'npm',
            'run',
            'import',
            '--',
            'get javascript kernel',
            '["{connection_file}"]',
        ],
       ...kernel_json,
    });
}

function generateBashKernelJson(kernel_json) {
    return jsonInterface({
        argv: [
            'npm',
            'run',
            'import',
            '--',
            'get bash kernel',
            '["{connection_file}"]',
        ],
       ...kernel_json,
    });
}

function generateProcessingKernelJson(kernel_json) {
    return jsonInterface({
        argv: [
            'npm',
            'run',
            'import',
            '--',
            'get processing kernel',
            '["{connection_file}"]',
        ],
       ...kernel_json,
    });
}

module.exports = {
    generateKernelJson,
    generatePythonKernelJson,
    generateNodeKernelJson,
    generateNotebookKernelJson,
    generateBashKernelJson,
    generateProcessingKernelJson,
};

Overview

This code exports a set of functions for generating JSON objects used in kernel communication.

Functions

1. pythonJson(kernel_json)

Returns a JSON object for a Python kernel, with the following properties:

function pythonJson(kernel_json) {
    return pathJson(Object.assign({
        argv: ['python3', '-m', 'IPython.kernel', '-f', '{connection_file}']
    }, kernel_json))
}

2. nodeJson(kernel_json)

Returns a JSON object for a Node.js kernel, with the following properties:

function nodeJson(kernel_json) {
    return pathJson(Object.assign({
        path: process.argv[2] || process.argv[0],
        argv: [kernel_json.path].concat(kernel_json.argv || kernel_json.args || [])
    }, kernel_json))
}

3. notebookJson(kernel_json)

Returns a JSON object for a Jupyter Notebook kernel, with the following properties:

function notebookJson(kernel_json) {
    return jsonInterface(Object.assign({
        argv: ['npm', 'run', 'import', '--', 'get javascript kernel', '["{connection_file}"]'],
    }, kernel_json))
}

4. bashJson(kernel_json)

Returns a JSON object for a Bash kernel, with the following properties:

function bashJson(kernel_json) {
    return jsonInterface(Object.assign({
        argv: ['npm', 'run', 'import', '--', 'get bash kernel', '["{connection_file}"]'],
    }, kernel_json))
}

5. processingJson(kernel_json)

Returns a JSON object for a Processing kernel, with the following properties:

function processingJson(kernel_json) {
    return jsonInterface(Object.assign({
        argv: ['npm', 'run', 'import', '--', 'get processing kernel', '["{connection_file}"]'],
    }, kernel_json))
}

Exports

The code exports all the above functions as an object, making them available for use in other modules.

module.exports = {
    pythonJson,
    nodeJson,
    notebookJson,
    bashJson,
    processingJson
};