python-js-executor
Version:
Execute Python code and files from JavaScript with full import support
31 lines (26 loc) • 713 B
JavaScript
const PythonRunner = require('./pythonRunner');
// Example usage
const runner = new PythonRunner();
// Example 1: Running Python code directly
const pythonCode = `
import math
from datetime import datetime
print("Current time:", datetime.now())
print("Square root of 16:", math.sqrt(16))
`;
runner.runCode(pythonCode)
.then(output => {
console.log("Direct code execution output:", output);
})
.catch(err => {
console.error("Error:", err);
});
// Example 2: Running a Python file
const pythonFile = './example.py';
runner.runFile(pythonFile, ['arg1', 'arg2'])
.then(output => {
console.log("File execution output:", output);
})
.catch(err => {
console.error("Error:", err);
});