Kyle S.
Kyle S.

Reputation: 29

Can I use Node.js for the back end and Python for the AI calculations?

I am trying to create a website in Node.js. Though, as I am taking a course on how to use Artificial Intelligence and would like to implement such into my program. Therefore, I was wondering if it was feasible to connect Python Spyder to a Node.js based web application with somewhat ease.

Upvotes: 1

Views: 3066

Answers (2)

Patrick Lumenus
Patrick Lumenus

Reputation: 1722

Yes. That is possible. There are a few ways you can do this. You can use the child_process library, as mentioned above. Or, you can have a Python API that takes care of the AI stuff, which your Node app communicates with.

The latter example is what I prefer as most my projects run on containers as micro services on Kubernates.

Upvotes: 1

Abid Ahmad
Abid Ahmad

Reputation: 136

There are 2 ways i am aware of which you can do this 1) Use child_process library

const { exec } = require('child_process');
exec('python yourscript.py', (err, stdout, stderr) => {
if (err) {
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});

You can check this article if you want step by step guide. https://www.geeksforgeeks.org/run-python-script-node-js-using-child-process-spawn-method/

2) Better method would be to create a API which you can later call using AJAX or whichever library you prefer. You can use microframework such as flask to create a server which then you can call using NodeJs. Tutorial to convert your machine learning code to API: https://towardsdatascience.com/publishing-machine-learning-api-with-python-flask-98be46fb2440

axios.post( your_api_address , {
MLparametre1: 'somevalue',
MLparametre2: 'somevalue',
MLparametre3: 'somevalue',
MLparametre4: 'somevalue',
})

Or you can just pass a list or dictionary object. https://flaviocopes.com/node-axios/ you can learn more about axios here.

Upvotes: 2

Related Questions