Created
October 27, 2020 10:11
-
-
Save tinshade/5a220acfe41b8ad1587514c24e6612a1 to your computer and use it in GitHub Desktop.
Here's my script on running a python script right from your NodeJS application without spinning an express server!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script will create a file with the name and extenstion provided in the parameters. | |
# Not very useful but the simplest example I could think of | |
import os, sys | |
path = os.getcwd()+"\\custom_path\\"+sys.argv[1] # I needed a custom path, you can omit that part. You still need the absolute path. | |
open(path,'w+') #Works like a native charm! | |
print("Done") #You will get this in the STDOUT on your Node console. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The python file can be as simple as Hello World or as complicated as a facial recognition script (Which is what I made this for). | |
const {spawn} = require('child_process'); | |
var file = process.cwd()+"\\appended_path\\MyPythonScript.py" //You need the absolute path. | |
const childPython = spawn('python', [`${file}`, 'param1']) | |
//When the script runs successfully | |
childPython.stdout.on('data', (data) =>{ | |
console.log(`STDOUT: ${data}`); | |
}); | |
//When the script runs into an error | |
childPython.stderr.on('data', (data) =>{ | |
console.log(`STDERR: ${data}`); | |
}); | |
//When the script exits | |
childPython.on('close', (code) =>{ | |
console.log(`Exit Code: ${code}`); //0 is good this time. :) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment