Wondering how to use Python within Node.js / Electron - javascript

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.

Related

Embed Python/C++ code in React Native?

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.

can I make a ruby GUI in javascript/html/css?

this is not a ruby on rails question
I just finished up a ruby basics course on teamtreehouse and I'm sitting
here thinking of what I want to make to demonstrate to myself that I
grasped what I learned.
I have a plan to write a program to automate setting up new hire files
and folders on a network directory at my work at the click of a button.
It might be nice to have a GUI for it instead of running it through the
command line.
First off, is this possible to run ruby with javascript and html without
running it through a server?
I'm trying to think of different ways I would get ruby to speak with
javascript. My first thought was to use a JSON file to produce url links
from in my webpage, but I have no idea how I would run the ruby program
from the web page.
probably a dumb question. Im trying to come up with a good project to
get some confidence in bare bones ruby before I pursue ruby on rails or
sinatra.
One easy way to run Ruby and many other languages in the browser would be to use a Ruby to JS compiler like http://opalrb.org/. Here's a huge list of more languages then you can shake a stick at: https://github.com/jashkenas/coffeescript/wiki/list-of-languages-that-compile-to-js#ruby.
The only trick is to make sure that the compiler can also be run in the browser not just the code it produces.
Hopefully that helps.
---- edit ----
I think I perhaps didn't read your question as much as I should have. As for comunication between the server and the client goes, I would say go with sinatra. It's really very simple, and you don't even have to use a database if it's just a project to learn with. Just modify files and save your data that way.
If you want a more dynamic front end making AJAX calls then you can just make a route in sinatra that would return back the information in an easy to parse form for JS such as JSON.

Absolute simplest server-side Javascript mechanism?

I've got a simple Javascript application with a JSON API. Currently it runs in the client, but I'd like to move it from the client to the server. I am accustomed to learning new platforms, but in this case, my time is very limited - so I need to find the absolute simplest way possible.
This should be an easy task, but all I'm finding are solutions that are way overcomplicated:
The application is currently hosted on an extremely basic server. Node.js is not available, and I do not have install privileges. I'll eventually move it to a different server, but I really don't know what will be available there.
Many solutions require installing and running a standalone server. Really? Just to evaluate Javascript server-side and spit out some data?
I can run Python and PHP, and I see that it's possible to call Javascript from inside a Python or PHP script. However, the specific Python solution that I've found also require installing some Python support via pip or easy-install, so probably not an option. Also, this just feels overcomplicated, and I'm concerned about setting myself up for errors such as data conversion or permissions, etc.
Any help?
#Quentin is correct. There is no way to run javascript on a server without a javascript interpreter on the server.
Node.js is not only the most robust and widely used one, it's also the simplest. It is certainly possible to write your own javascript interpreter in PHP or Python, but that would be much more complicated than using Node.js.
Try really hard to find a server solution that allows you to use Node. In the end, it's going to save you (and any other stakeholders interested in the project) a lot of time and money.

Avoiding writing the same algorithm in multiple different languages

I am a web developer, and I have observed that many times I need the same function on both client and server. So I write it in JS as well as in PHP or whichever server side language. I am fed up with this. If I have to change it then I need to change it in both places. If I want to use it for some hand held devices, then I will have to rewrite that code yet again using objective-C or Java etc. Then if I need to change that function then I will need to change it everywhere.
Is there a solution for this? If I will call some webservice via ajax, then the client will have a delay. If it will be in JS then it can't be accessed from within PHP or Java, etc. If I use some service in PHP from another language then that can also become a performance issue.
It is also possible that some time we need such functions output from some paramters as input using db or without db.
I know there would be some pretty simpler solution but I am not aware of that. Please tell some language independent solution as I don't have VPS always.
I am not sure if my question actually belongs to stackoverflow.com or programmers.stackexchange.com so please transfer it to programmers.stackexchange.com instead of closing this question if it belongs to there.
Typically, the solution to this problem is to write common code in one language and use translators or library linking to allow access from other languages.
Node.js allows you to write server-side code in JavaScript.
Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
You can also use JavaScript to write HTML5 apps for mobile devices.
"Building iPhone Apps with HTML, CSS, and JavaScript"
Now web designers and developers can join the iPhone app party without having to learn Cocoa's Objective-C programming language. It's true: You can write iPhone apps quickly and efficiently using your existing skills with HTML, CSS, and JavaScript. This book shows you how with lots of detailed examples, step-by-step instructions, and hands-on exercises.
If you don't want to try to write large complex applications in JavaScript, GWT provides a way to write Java and via-translation, run it on the client.
The GWT SDK contains the Java API libraries, compiler, and development server. It lets you write client-side applications in Java and deploy them as JavaScript.
If you develop in .Net languages: C# -> JavaScript ScriptSharp
Script# is a free tool that enables developers to author C# source code and subsequently compile it into regular script that works across all modern browsers
you could use the spidermonkey extension to translate php into javascript. this way you can write your functions in php then simply convert them to javascript and re-use them at the browser.
here is a good tutorial to show you how this is done

Is it possible to execute server-side javascript from a *Python* Google App Engine instance?

A lot of questions have been asked and answered about running server-side javascript on Google App Engine, but all of the answers deal with Java instances in order to make use of Java-based JS interpreters like Rhino, Rhino for Webapps, etc.
Is there any way to execute server-side javascript code on a Python GAE instance? I'm thinking something exactly along the lines of pyv8, but with support for App Engine (which I guess would mean a pure python implementation of the interpreter).
The only solution I can come up with at the moment is to use some sort of gross hack to run a Java and Python GAE instance side-by-side (via different versions) so they can both talk to the same datastore, let the Java instance host the JS code, and use an API to talk back'n'forth. Not very appealing.
No need to get into all the "this is unnecessary, you shouldn't be doing this" discussion -- I know this isn't ideal and I'm simply curious if it can be done.
As far as I can find: No
I've done a bit of searching, but it seems that nobody has tried to implement a pure Python Javascript engine, and I can't blame them: it would be a huge amount of work for very few use cases (unfortunately, yours is one of those). A couple of projects—Grailbrowser and Pybrowser—have Python code to render HTML, so might one day aim to run javascript, but it's not even started, and neither of them look in active development.
The most likely way it would ever happen is if Google were to offer the Parrot VM (which can run various dynamic languages) on Appengine. That's a cool idea, but I'm not holding my breath.
What might work is to run Jython (and Rhino) in a Java instance. Of course, then you'd have to get to any App services through the Java API, not the Python one, which would be ugly.
Actually, it can indeed be done, using either AppEngineJs or ESXX:
http://www.appenginejs.org/
http://esxx.blogspot.com/2009/06/esxx-on-google-app-engine.html
I am currently trying to solvevthe same problem with PyJON
http://code.google.com/p/pyjon/
Seems to be a pure Python JavaScrit parser an interpreter.

Categories