python-js-executor
Version:
Execute Python code and files from JavaScript with full import support
73 lines (47 loc) • 1.38 kB
Markdown
Execute Python code and files from JavaScript with full import support.
```bash
npm install python-js-executor
```
- Node.js >= 14.0.0
- Python 3.x installed and accessible in PATH
```javascript
const PythonRunner = require('python-js-executor');
const runner = new PythonRunner();
// Execute Python code directly
const code = `
import math
print(math.sqrt(16))
`;
runner.runCode(code)
.then(output => console.log(output))
.catch(err => console.error(err));
// Execute Python file
runner.runFile('./script.py', ['arg1', 'arg2'])
.then(output => console.log(output))
.catch(err => console.error(err));
```
```javascript
const runner = new PythonRunner();
```
Execute Python code directly from a string.
- `code`: Python code to execute
- `args`: Optional array of arguments to pass to the Python code
- Returns: Promise resolving to array of output lines
Execute a Python file.
- `pythonFile`: Path to the Python file
- `args`: Optional array of arguments to pass to the Python script
- Returns: Promise resolving to array of output lines
MIT
Abhinav Tiwari (PINAKA)