Embed Python/C++ code in React Native? - javascript

I am planning on working on a personal project where I utilize React native for most of the Business Logic and Front End.
I want it to work on Windows 10, but it would be great if it was compatible with IOS/Android as well.
While I know React native itself has that capability, the catch here is that I want to utilize heavy machine learning throughout the project in Python for now, and then perhaps C++.
I did some research and many people suggested running a flask server and calling the Python code via its API. The problem with that is that that then delegates the machine learning tasks to the server, and not the client.
1) Is there a way to "embed" the python server to only exist in the React-Native back-end (Node js) environment? Or perhaps in this case it just calls a python script and awaits results?
2) Is is possible to achieve this task while also packaging it in a '.exe' or '.apk' file?

I'm interested in re-using some Python code, take a look at http://www.transcrypt.org, which promises cross-compilation of Python to JavaScript.

This may not directly answer your question but I'm sure it does help you take a decision.
So, with my experience I can suggest you these things:
1) Use React-Native bridge: If you're relying on ML algorithms that are readily available and you don't need much of modifications at deeper level, then you can consume the APIs provided in Windows AI and ML. I'm suggesting you this because you are saying that you'll be developing it for Windows. If you want the same for Android and iOS, I guess there are APIs readily available which are similar to Windows'. You can find about react native bridge and how can you consume the above mentioned APIs in these links - Link1 and Link2
2) Use NAPI or C++ Addon for Node.js: As you said you can be relying on C++ in future, I would suggest this to you. This allows you to consume C++ Libraries in Node.js. Also, as Python is C-callable, if I'm not wrong we can call Python in C++ and vice-versa. I suggest you to read about these things before you proceed. You can read about NAPI and C++ Addons for Node.js.
You can also find a lot of blog posts and tutorials on the things I mentioned both the alternative I've given above.

Related

Wondering how to use Python within Node.js / Electron

I am working on an audio editing prototype. At the moment it is very simple, so it currently works as a Web App using JavaScript, HTML and CSS. This makes it possible to build as an Electron app, using Node.js to access the file system.
However, it makes heavy use of a Python program called Gentle, particularly the file align.py. I was wondering if it was possible to integrate this program somehow, given how frequently it is used.
I am not familiar with Python, but I have tried to work out if this can be done. I have read about child_process, python-shell and zerorpc. However, if possible, I do not want to force the user to install Python along with all the dependencies required for Gentle, as it is a difficult process with lots of room for error.
This is where I have become stuck. Ultimately I am looking for a way to use Gentle in a way which gives the appearance of it being part of the functionality as a single self-contained program, rather than butchered on with duct tape.
I realize Gentle includes the option for a REST API and a Python server, but I am more interested in using Gentle offline for faster functionality. I am also too broke to afford hosting.
I realize I am kind of working backwards, as the front-end normally comes after the back-end. If it is easier I can try to rewrite the code base in Python or a lower-level language, but I am trying to avoid this if possible.
Any help or suggestions would be greatly appreciated!
You can compile the Python program and include the resulting binary file into your Electron app and just run the binary via child_process. There are several ways to create executables from Python programs.

Best way for a Node.js application to interact with an external program

I am a beginner to web development - I am building a website that requires some entries from the user, does some complicated mathematical processing on those entries, and returns the result to the client. I was thinking of implementing the mathematical stuff in another separate application which is better suited for such work (like in Java or C++ where there are good math libraries and the implementation would be more robust and faster). I was wondering what is the best way to do this, architecture-wise.
The "dummy" approach would be spawning a process from the Node.js application and waiting on its output from stdout, parsing it (probably in JSON) and then processing it before sending the result to the client. I have trouble believing that this is the best way to do this (it seems too error-prone, no proper error handling, dependent on the output, and just plain bad practice). A slightly better approach would be to have the Java or C++ application listening on a specific port and waiting for requests from the Node.js application. However this requires more thinking in terms of load-balancing (how would it scale with the number of requests?). Finally, the last approach I found online was to use a queuing system such as RabbitMQ as a way to communicate between the Node.js application and the Java application.
Typically (in a "traditional" software), implementing a separate library that holds all the math magic to which we can make calls would be a good way to go.
What is the best approach to achieve this with a Node.js/web application? There must be good practices/models/architectures/designs for such a problem. Thanks!
The best approach I can think of is writing a native nodejs module, you can use C++ and any library you have to write the code and export some APIs to be called from node's javascript.
This also allows you to package your native application as a module for nodejs, which can then be distributed and compiled/installed with npm.
Here's the official tutorial from node's website.
It isn't the easiest approach, but certainly the more elegant and mantainable one.
IF your problem can be structured in a stateless way -- i.e. you send a request out with the data and the associated task, and then later receive the result as a response, I would probably create one angular app for the front end, and multiple node servers for the back end (IF you needed to handle many requests simultaneously).
As pointed out in the first answer below, you can put whatever parts of the problem that you want to into native code, BUT what might be much easier is to use sockets to connect to a server to do the computation, in any language, etc... In one project I use a socket.service to handle all the communication back and forth to the servers. (The angular-fullstack generator in the next paragraph has such a skeleton socket.service piece.)
In terms of the frontend in combination with a node backend, The Yeoman generator (http://yeoman.io/generators/) for creating MEAN stack applications, using MongoDB, Express, AngularJS, and Node - lets you quickly set up a project following best practices. And the angular-fullstack generator will give you a framework that is light years ahead in so many ways. You can use yeoman to generate a sample application frontend (angular, express) and backend (NodeJs, MongoDB) with support for sockets, etc...
This too is not very simple, but in the end, with a few months of work, you would have a structured problem and solution that you would never outgrow. When I first wanted to work into this space, that was the path I took. And in truth the front-end was/is the harder part and the backend, connected with sockets, http, or whatever has gotten to be much simpler. In terms of servers, AWS, or even just nginx on Linux through DigitalOcean or many others is inexpensive and flexible.
I hope this helps.

Serve PyQt4 GUIs in Web-Application using HTML/CSS/JavaScript

Currently, we have a number of GUIs that have been developed using PyQt4 and PyQtGraph.
We need to make these interfaces accessible to a number of remote users and are hoping to do so by serving the GUIs through an HTML/CSS/JavaScript Web-Application.
The link below provides a few suggestions for this task, but these seem to apply to a project that is in its initial stages of creation. I'm familiar with the development of Python-JavaScript Web-Applications using the Pyramid Server and would like to use this same method for providing users access to PyQt4 GUIs that have already been developed.
https://softwareengineering.stackexchange.com/questions/109572/can-i-create-a-desktop-python-gui-application-and-run-it-as-a-web-app
So my question is, can I serve a functional PyQt4 GUI as a widget using HTML/CSS/JavaScript?
This is hard. In theory you could use a VNC-like approach that sends an image of the window to the client browser and sends user events back to the server. This will probably have poor interactivity depending on the networking and would require a lot of server power, depending on the number of users. (For an example of this, see: https://github.com/campagnola/webqt)
You're probably better off distributing the application with something like py2exe, py2app, cx_freeze, etc.
I personally haven't tried it but you could look into Wt: http://www.webtoolkit.eu/wt/
It is a C++ framework that has a Qt flavour. It has a Java version called JWt. Hence, a Jython binding exists for us Python programmers: http://www.webtoolkit.eu/wt/other_language
It is not a direct replacement of Qt, and might need modification to your Qt code to work as a Wt web app, but it certainly seems worth it to look into.

Is it possible to use Node.js socket.io with a web application written in python?

I have a website written in python(flask) which I would like to some realtime functionality to. I have looked at some websockets implementations for python, but none of them seem as straightforward and simple as socket.io for Node.js.
Is it possible(and furthermore, practical) to write the realtime component in Node.js and integrate with python(e.g., when a page is hit in the python code, execute this node.js function), or is it just a waste of time?
any help is appreciated!
Is it possible? Yes. For example, you could have a web server route the websocket-related paths to node.js/express and the rest of them to your python/flask app. Is it practical? Probably not, but it's a case-by-case judgement call. How cleanly separated are the real-time vs traditional aspects of your application? At the end of the day if your integration between 2 daemons in 2 different languages is significantly more complicated that just finding or writing a working python websocket implementation, you might want to stick with python.

Is there a high-level inter-process communications API that is implemented in both C++ and Javascript

I am working on app where I need to pass messages between a C++ application and a Javascript web app.
Certainly I could write sockets code myself in either language and I have done this in the past when necessary.
What I would really like is a higher-level message posting or message queueing API that does a lot of the work for me. Does anyone know of such an API?
I have looked at ICE, and it doesn't appear to have Javascript bindings. I have also looked at Boost message queue, but it only caters for the C++ side of things. If necessary I might roll my own Javascript bindings for either of these technologies.
UPDATE: Sorry should have mentioned this before, I want to run this in a browser.
To give a more complete story what I want is a simple browser-based app that is used to configure and display logging for a C++ application.
I know there are other ways of doing this, but I am specifically interested in a high-level library in both C++ and browser-based Javascript that builds a message queue ontop of the sockets API (if there isn't one then I might consider implementing it myself and writing up a code project article).
ALSO: I'm not bothered about portability in terms of the web browser. Eg if there is a high-level IPC Javascript library that only works in Chrome, I'll be happy with that.
With JavaScript I assume that you are running it in a browser? In this case your C++ application needs to provide a webserver and some kind of JSON based webservice that you can call. On the JavaScript side you just use AJAX to communicate with that webservice.
An alternative would be websockets which might be a little harder to implement on the C++ side though.
To simply answer your question: No, there is no IPC implemented in ECMAscript out of the box.
But you actually answered you question already. If you try to communicate with Javascript that runs in a browser, you indeed should use (web-)sockets connections to pipe date in either direction. Of course you could write a simple HTTP server in C++, but I guess that is overkill and does not have the capabilitys of bi-directional sockets.
It's still some work to implement a web-socket connection in C++ from the scratch (the specs were in flux for a long time), but I guess there are some librarys out already.
If you're trying to communicate with node.js, this is an almost trivial task using real sockets/pipes.
I have found a solution that meets my needs. It isn't exactly perfect but I think it works well enough.
Some people suggested using HTTP and ajax. That turned out to be a useful idea and after some prototyping I think it solves my rather basic needs.
To be more specific I am using the Mongoose HTTP server embedded in my C++ application and I am using the jQuery ajax function to pull data from the server. The jQuery client polls the server continously for new data, not particularly efficient but I think it will do the job good enough for me.
Once my implementation is complete I'll write an article explaining how to do this in detail and then I'll update this answer.
You could try DBus, it has very simple mechanism to define, query and use interfaces, and there are some components for XPCOM and webkit based browsers (for example http://sandbox.movial.com/wiki/index.php/Browser_DBus_Bridge and http://code.google.com/p/v8-dbus/). Also DBus is opensource and cross platform.
For a server side or non-browser implementation how about named pipes?
Yes it's vintage technology and the usage depends which OS you use, but as long as your server side js environment has ability to read and write files it may work, and it fits the description 'high-level' inter-process communication.

Categories