Interactive Form Example
Here is an example interactive form application. The interface UI is driven by the built-in webserver. It's based on some of the code in Ole's file transfer script. However, in this case, the pages are served directly to the web browser component inside Pythonista. You do not need to view it from another computer--although, if you know the device's address you can do that too.
I am a newbie to Python and GitHub, so my apologies if there are style or formatting errors. I have tested this on my iPhone and two iPads (it was written on a mini) as well as a desktop and laptop.
I think using an HTML forms interface is a good way to produce easy to use, interactive applications. I hope this example will demonstrate some of the possibilities. Let me know what you think.
Note: the gist above is now outdated. I have made a number of improvements since this initial post. Please use the newer gist shown in my second post, below.
New to Pythonista but old to Python. Stunningly useful app on an iPad2.
Been following this thread as a means to build UI for a prototype medical data collection form. Although web interface not ideal, understand that wxPython, iOS native controls all largely blocked by Apple so best available option for now.
Thought I'd throw bottle (import bottle dir(bottle)) into the mix... It's in Pythonista by default and makes REST based web interfaces trivial and has templating libs.
Need to find a way to create a web browser from within a bottle based app (thread?) but does Ajax and much that would be useful for interactive interface like the google search predictor etc.
Hope this helps someone
@Landyquack You can start the web browser via <code>webbrowser.open(...)</code> <em>before</em> you start the (blocking) bottle server. Opening the web browser is slightly delayed, so by the time it shows up, the server should typically be running already.
Thanks for this - most of the time the browser beat the server but occasionally it didn't so bunged the browser into a threading.Timer() which solved things.
As a solution, it's a bit kludgy. Sometimes closing the webbrowser closes the whole script, sometimes it doesn't.
Had a look at what's possible within Scene - only at early stages but this may interest - http://omz-software.com/pythonista/forums/discussion/comment/715#Comment_715
@Landyquack, I am very interested to see what come of this. (your GUI tests) I tested your Gists a little, nice to see. What I'd love , as with you, is a UI kit or something like that. I could really use that for whipping up quick-n-dirty , data-based apps for myself or clients. But I am not interested in running a local web server and using web forms. If I were to do that, I'd rather just make a normal web app.
I tried playing with ScriptKit and did manage to get some JSON forms and output working, but I like Python better, and I like the libs available too. But more importantly, I like the forums and user base! I also am tinkering with Codea, its cool, but no UI components, and if I have to build any, I'd rather use Python.
Keep us posted on your GUI work, sounds cool!
I liked this idea when I read it. I have found an other way to implement it. Using pipista, I just installed bottle, a small framework, and it is really easy to write simple code that launch both the webserver and the webbrowser. It is a simpler way to write it, and a lot more powerfull (you can use templates, route, etc.).
I will try to post the sample script, but I don't know how to post something on gist.github from pythonista. I will look up.
I just succeeded to publish a file this way:
Bottle (but not Flask) is built into the current Pythonista so the pipista install step is not required for bottle.
Lately I have been doing a bit of Interactive Forms work in Pythonista and one gotcha that I see is that the bottle server keeps running even after the logic of your script has ended. You have to manually click the stop script button after your script logic completes to stop the bottle server (or you can wait for it to timeout on its own). Adding the Pythonista-only stop_when_done=True parameter to your last call to webbrowser.open() will gracefully shutdown the bottle server.
I posted a change request to your github code above so you could see this proposed change in action.
**NOTE: corrected parameter based on onionradish's comment below.
As a note to omz, Bottle's inclusion isn't documented and should be. It's a great mini-framework for web apps, and likely isn't getting as much use as it should. I discovered Bottle independently a few weeks ago and installed it on my device manually. I did a double-take after reading ccc's reply that it's already included.
Regarding the Pythonista-only 'stop_when_done' option (ccc linked to and made the github change correctly but typo'd above), you can use os.name to optionally add the parameter so your script will run on the desktop without throwing an error. This is handy if you do development or testing on the desktop before copying the script to your device.
if os.name is 'posix': webbrowser.open('http://localhost:8080', stop_when_done=True) else: webbrowser.open('http://localhost:8080')
@onionradish Agreed, bottle is pretty nice, and I plan to add documentation for it.
I wouldn't recommend using
os.namefor platform detection, it'll also be
'posix'on a Mac. The most pythonic way to do this would probably be to just catch the exception:
try: webbrowser.open('http://localhost:8080', stop_when_done=True) except TypeError: webbrowser.open('http://localhost:8080')
You could also use
That's even better -- I updated my code and learned something. Thanks!