bundling python "back end" code in a phonegap app
Asked Answered
S

1

7

Short version:

Is there a standard way to bundle Python "back-end server" type code in with a phonegap client application?

Long version:

I have a number of applications that I'm considering porting to phonegap. In general, the apps are written in Python. Some of them have web front-ends, some of them are stand-alone Python apps with interfaces based on wxpython.

So each application has some client-side stuff, which is already in html+javascript+css, or which I'm happy to translate to html+javascript+css.

For the server side, for some of the applications it's okay to leave the server code on a server. But for some/most, I'd like to package the server-side logic in with the phonegap app, so it can be a standalone app. This requirement comes from two needs. One is that many of these apps are used for emergency response, and need to work even when cell towers and other network infrastructure is not available. The other is simply that some of the apps are quite simple, and don't warrant a client/server architecture -- they just happen to have a lot of "back end logic" type code written in Python.

Now, I know that I could just port all of that back-end Python logic to javascript, but I was hoping to find a solution where this sizable code base could remain in Python.

My idea is that I could write a phonegap plugin that actually contains the complete Python interpreter (at least complete enough to handle most of the stuff in my code base). (That is, both iOS and Android allow native C code, so I should be able to compile Python -- or at least much of it -- from source, or just link to iOS and Android Python libraries that others have built.)

So in the javascript code, I could have the client call some function like "InvokeBackEndMethod()". This would act much like an ajax call, but instead of calling out on the network, it would send the query/url/message to the Python plugin. My understanding is that phonegap plugins can maintain persistent state (e.g., a database plugin lets you make one call to open the database and subsequent calls to read from it and close it). So the Python "server" code could maintain its state just like it does on the real server. In fact the Python code might be running a web framework like cherrpy, so it would truly be like running both the client and the server in the same mobile app.

My questions are:

(1) Does that plan sound reasonable?

(2) Has someone already solved this problem? I was hoping to find a project called, say, "phonegap server", and it would essentially be a "universal" PhoneGap extension, in the sense that it would take arbitrary calls from the client, and would dispatch those calls to your choice of various mechanisms: Python, Java, mono, etc. (i.e., this universal phonegap extension would get "extended" by various language "plugins" and then those plugins in turn would get "extended" by whatever business logic the user added in the given language). It may be that such a project isn't needed by most people because they don't have a requirement to run disconnected and/or they don't have a big code base of "back end" logic that they'd like to deploy in a stand-alone app but leave in the original language. But it seems like some people must need that, no?

Sperrylite answered 26/6, 2013 at 8:19 Comment(3)
No, The PhoneGap app is supposed to be client side code only. But you could write logic within the Java/Python code that wraps your PhoneGap code.Loraleeloralie
@limelights: right, my extension idea is essentially putting the Java/Python code in the wrapper (i.e., the part of phonegap that's outside the embedded browser). My question is whether someone has already set up a framework for that.Sperrylite
Related: Is it possible to use Python to write cross-platform apps for both iOS and Android?Stuffed
D
2

Two very different initiatives you should check out: http://omz-software.com/pythonista/ Allows the export to an Xcode project.

And https://github.com/brython-dev/brython Use Python instead of Javascript for HTML5 development.

Duyne answered 26/6, 2013 at 12:46 Comment(4)
Thanks. pythonista is too much of a "package" and just for iOS, but brython looks quite interesting. I'm looking into how well it performs...Sperrylite
Well, I tried brython, and also pyjaco and py2js/pyjamas. In all cases, I found them "not ready for prime time". Py2js is quite sophisticated, but still it doesn't support all of the python syntax that's used in my code (this is workable because I can general modify the code to remove what they don't support). But the killer is that with all of them it's very hard to track down your bugs. They give minimal error reports and it's hard to set breakpoints in your compiled javascript code because it's injected dynamically (at least I don't know how to set breakpoints in such code).Sperrylite
@MKatz Did you meanwhile find another solution or test more recent versions? I think Brython's come a long wayStuffed
@TobiasKienzler, we are mostly moving to a system using Chromium Embedded Framework with the server part in C++ and the client part in javascript. We still have some projects with server code in Python. I will check out the changes to brython.Sperrylite

© 2022 - 2024 — McMap. All rights reserved.