问题
I want to use some Python libraries (for machine learning etc) in a React Native app. Is it possible to do it without using a server (i.e. run the Python code within the mobile app) so that no internet is required?
回答1:
The React Native App consists of two major portions
- Business Logic which is a NodeJs app. This app controls the other piece
- Frontend which is written in Javascript, however it gets linked to native interfaces (Java in case of Android and Obj-C in case of iOS)
In this framework, best way to integrate Python Code, machine learning or otherwise is to connect NodeJS app with Python interpreter. This also happens to be complex to implement. This will go something like this
#include <Python.h>
int main(int argc, char *argv[]){
Py_SetProgramName(argv[0]); /* optional but recommended */
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print 'Today is',ctime(time())\n");
Py_Finalize();
return 0;
}
As seen at Embedding Tutorial
Now this is bit tricky so lets look at secondary options like connecting with the model using C++. Like Tensorflow also has a C++ API which can be used to integrate Models into NodeJS. Final option is ofcourse to use it as a separate child process or server side call.
来源:https://stackoverflow.com/questions/47806024/use-python-libraries-in-react-native