在React Native中使用python库 [英] Use python libraries in React Native

查看:118
本文介绍了在React Native中使用python库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在React Native应用程序中使用一些Python库(用于机器学习等).是否可以在不使用服务器的情况下(即在移动应用程序中运行Python代码)进行操作,从而不需要互联网?

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?

推荐答案

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.

这篇关于在React Native中使用python库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆