Use python libraries in React Native

余生颓废 提交于 2020-12-04 03:13:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!