11

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 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm a bit late to this, but wouldn't spawning a child process be enough or something like python-shell rather than having to embed it by hand? I'm not very familiar with node.js so I could be wrong here
@SSBakh, I'm VERY late too :-D , the OP states 'React-Native', so, that will most probably be a mobile environment, such as IOS or Android, neither of which has a Python interpreter installed. I assume you're thinking about a desktop installation, like Linux. Personally, I might like to run some Python on mobile, cross-compilation is an idea (see Brython or Transcrypt), as it finding some kind of mobile embeddable version of Python, if such a thing were to exist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.