It is possible to use readline in Node.js to read a line of python code, then send it to a python interactive shell spawned as a child process, then receive the output?
I know this is possible:
import { spawn } from "child_process";
const py = spawn("python", ["-i"]);
py.stdout.on("data", (data) => {
console.log(`stdout: ${data}`);
});
py.stdin.write('print("hello from python!")\n');
// will get 'stdout: hello from python!'
However, this will not work:
import { spawn } from "child_process";
import * as readline from "readline";
const py = spawn("python", ["-i"]);
py.stdout.on("data", (data) => {
console.log(`stdout: ${data}`);
});
py.stdin.write('print("hello from python!")\n');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.prompt();
rl.on("line", (line) => {
py.stdin.write(line);
rl.prompt();
}).on("close", () => {
process.exit()
});
I want to do this because I want to develop a interface for python in Node.js. Executing a standalone python command/file is simple via spawn, but I want to build a fully-featured interface, which will have similar capabilities as Reticulate in R.