Call a Python function from NodeJS
I guess you misunderstand the process of calling python. You are not calling python function in node
directly but call the process to run a shell command that run ./ml.py
and collect the console output.
If that's clear then the problem is obvious, you define the function in python but you forget to call it.
def testMLFunction():
return "hello from Python"
print(testMLFunction())
By the way, I guess you are going to refer a machine learning algorithm as it is named as ML.
If that's the case, calling it from shell may be inefficient as loading a model could be time consuming and it will be loaded whenever you call it.
Instead, I recommend you also build a python server, and access these predicted result via internal http request.
How to call a specific python function from nodejs?
Write a short python program calling your function, and run it using the child_process
module as shown in this thread. You'll be able to get standard output back.
How to call python script from NodeJs
There are multiple ways of doing this.
- first way is by doing
npm install python-shell
and here's the code
var PythonShell = require('python-shell');
//you can use error handling to see if there are any errors
PythonShell.run('my_script.py', options, function (err, results) {
//your code
you can send a message to python shell using pyshell.send('hello');
you can find the API reference here-
https://github.com/extrabacon/python-shell
second way - another package you can refer to is node python , you have to do
npm install node-python
third way - you can refer to this question where you can find an example of using a child process-
How to invoke external scripts/programs from node.js
a few more references -
https://www.npmjs.com/package/python
if you want to use service-oriented architecture -
http://ianhinsdale.com/code/2013/12/08/communicating-between-nodejs-and-python/
Can I call a python function from a Node.js Google cloud function
Cloud Functions code runs inside a container that is set up for specific environments. For example, in your question, you are using the Node.js execution environment.
Your goal is to call a Python function (script) from Node.js. This would require the Python interpreter, libraries, and a correctly set up environment.
The Cloud Functions Node.js execution environment does not include the Python execution environment. Therefore the answer is not possible using Google provided execution environments.
If you would like to combine Node.js and Python, I recommend creating a custom Cloud Run container and deploying to Cloud Run, which is the big brother to Cloud Functions.
Call python function from node.js
Turns out there is a solution to this using python-shell. Details can be found here.
Related Topics
Using Quotation Marks Inside Quotation Marks
How to Convert an Xml File to Nice Pandas Dataframe
Why Is My Pygame Application Loop Not Working Properly
Your CPU Supports Instructions That This Tensorflow Binary Was Not Compiled to Use: Avx Avx2
How to Return Dictionary Keys as a List in Python
Create an Empty List with Certain Size in Python
How to Filter Foreignkey Choices in a Django Modelform
What's the Difference Between Globals(), Locals(), and Vars()
How to Find All the Subclasses of a Class Given Its Name
Converting a Pandas Groupby Output from Series to Dataframe
Generate Random Integers Between 0 and 9
How to Run Multiple Python Versions on Windows
How to Urlencode a Querystring in Python
Python Windows Authentication Username and Password Is Not Working