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.
npm run import -- "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
};
// 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,
};
This code exports a set of functions for generating JSON objects used in kernel communication.
pythonJson(kernel_json)
Returns a JSON object for a Python kernel, with the following properties:
argv
: An array containing the command to start the Python kernel.kernel_json
: The kernel JavaScript object, merged with the argv
property.function pythonJson(kernel_json) {
return pathJson(Object.assign({
argv: ['python3', '-m', 'IPython.kernel', '-f', '{connection_file}']
}, kernel_json))
}
nodeJson(kernel_json)
Returns a JSON object for a Node.js kernel, with the following properties:
path
: The path to the kernel executable (defaulting to the first command-line argument).argv
: An array containing the command to start the Node.js kernel, concatenated with the kernel_json.argv
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))
}
notebookJson(kernel_json)
Returns a JSON object for a Jupyter Notebook kernel, with the following properties:
argv
: An array containing the command to start the Notebook kernel.function notebookJson(kernel_json) {
return jsonInterface(Object.assign({
argv: ['npm', 'run', 'import', '--', 'get javascript kernel', '["{connection_file}"]'],
}, kernel_json))
}
bashJson(kernel_json)
Returns a JSON object for a Bash kernel, with the following properties:
argv
: An array containing the command to start the Bash kernel.function bashJson(kernel_json) {
return jsonInterface(Object.assign({
argv: ['npm', 'run', 'import', '--', 'get bash kernel', '["{connection_file}"]'],
}, kernel_json))
}
processingJson(kernel_json)
Returns a JSON object for a Processing kernel, with the following properties:
argv
: An array containing the command to start the Processing kernel.function processingJson(kernel_json) {
return jsonInterface(Object.assign({
argv: ['npm', 'run', 'import', '--', 'get processing kernel', '["{connection_file}"]'],
}, kernel_json))
}
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
};