且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在React Native中使用python库

更新时间:2023-12-03 18:19:46

React Native App由两个主要部分组成

The React Native App consists of two major portions

  • Business Logic,这是一个NodeJs应用程序.这个应用程式控制着另一片
  • 使用Javascript编写的前端,但是它链接到本机接口(对于Android,则为Java,对于iOS,为Obj-C)

在此框架中,集成 Python代码,机器学习或其他方式的***方法是将NodeJS应用程序与Python解释器连接.实施起来也很复杂.这样会变成这样

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;
}

嵌入教程

现在这有点棘手,所以让我们看一下辅助选项,例如使用C ++与模型连接.像 Tensorflow 一样,它也具有C ++ API,可用于将模型集成到NodeJS中.当然,最终的选择是将其用作单独的子进程或服务器端调用.

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.