0

I am designing a website using node.js and I have a python file with multiple python functions in it that I am using to modify a postgreSQL database. Is it possible to run those functions using node.js or react and set the return of my python function as a variable in the javascript program? (Ex. enter primary key value in js, run a python search function to find data associated with that key value in postgres, then store that value in a list in js)

I know that very similar questions have been asked and answered but they either are running entire files or using flask/django.

If this is impossible to do with all of my limitations would it be possible to create a different python file that imports my already made file, takes arguments as shown here and use those arguments to run functions from my original file?

1 Answers1

0

You can use child process in node js

for eg : -

app.js


var spawn = require("child_process").spawn;
const dataTobeSendToPython = {id : 1000}
var process = spawn('python',["./mypythonFile.py", dataTobeSendToPython]); 

Process data in Python file

python.py


data_from_node = sys.argv[1]
def process_data_from_node(data_from_node):
      process data
      return data

After processing data is returned to js file

app.js


process.stdout.on('data', (data)=> {
  process the data returned from python to js file
}

Ashish Choubey
  • 407
  • 1
  • 5
  • 17
  • I tried that, the problem that I am having is twofold as explained above. I can't access the individual functions using the child processes, I have to run the entire file meaning that it needs a print statement, and if I set up a python file that runs the functions of another file using imports, it doesn't get the desired outputs. Is there a way around these issues? Thank you fr your time. – Austin Camps Jun 16 '20 at 15:41