Pythonista like HTML
0942v8653 last edited by
What do you mean? If you want to serve a website you can use a script like this:
import SimpleHTTPServer import SocketServer import webbrowser handler = SimpleHTTPServer.SimpleHTTPRequestHandler httpd = SocketServer.TCPServer(("", 0), handler) port = httpd.server_address webbrowser.open('http://localhost:' + str(port)) httpd.serve_forever()
and put your index.html file in the same directory.
reefboy1 last edited by
Ok cool thanks
briarfox last edited by
You might want to check out bottle. It's included in pythonista but I do not believe the documentation is. http://bottlepy.org/docs/dev/index.html
@briarfox and forum, as a simple pythonic web framework do you happen to know the relative strengths and weaknesses of other, similar WSGI-based, lightweight web frameworks/servers for their use within Pythonista. I mean a comparison say of Tornado, CherryPy, Bottle, Waitress, Gunicorn, Spawning, Chausette, etc.
Two good sources of info I have found are:
I am messing around with some server-based utilities in/for Pythonista and some of these that are pure python frameworks may be better suited than others for this usage.
If it wasn't clear, the intent is to run BOTH clients and servers in Pythonista, something that in itself is a problem as I see it currently. Is their any way at all in the current Pythonista to run a service as a background thread of the main Pythonista interpreter process but return so that other script(s) can be run? Nothing I have tried can start say a server in background but bring Pyhonista main window back to be able run anything else -- the "x" remains and there is no "arrow" icon. Any tricks to supporting the appearance of multiple processes?
ccc last edited by
Why not look at @Gerzer's
Servrwhich is in the Apple AppStore?
It was built with Pythonista but it is now
a standalone appso you can run it and still have Pythonista available to you. Even I f Servr does not offer the features that you need, you could still follow the same model for your own server. Build a standalone server app and then use Pythonista for the client or whatever.
I like Bottle and Flask which both are available in Pythonista v1.6 while the others that you mentioned are not.
If you use SebastianJarsve's excellent Pythonista-Webbrowser, this presents in a ui panel; the console is still fully "alive" allowing you to run whatever you want from the console, and swap back and forth if you so choose. Obviously, start the Webbrowser first, then the server.
I have to admit, a lot of people want to do it, but I've never seen the appeal of serving up html over a port only to be consumed within pythonista (it is not well documented, but you can open html files directly using abspath's, at least in WebView). In old pythonista versions this was necessary as webbrowser actually opened up safari.
@cc and @JonB, thanks for some good ideas. A standalone web/sppserver would be alright, but I still find it preferable to have everything easily at my finger tips. Were IOS more multitasking friendly it not be such a big deal but it is clearly not the case. For this reason I am drawn to a one-shop idea.
Gerzer last edited by
@ltddev I'd be happy to give you a build of Servr - mobile edition with a built-in web browser (or a button that launches Safari and automatically loads the webpage) through TestFlight. Just know that Servr - mobile edition is only compatible with iPhone (I am working on an iPad version) and can only serve one file at a time (except you can serve as many Camera Roll pictures as you want). In addition, I am currently debugging version 2.1 which integrates with iCloud Drive and any other iOS 8 storage provider (like Dropbox, Box, Google Drive, Quip, Documents 5 by Readdle, and more). I can also post the core WSGI code if you want.
If none of this appeals to you, I can post another way to do things that works completely inside Pythonista.
I have been working on a new project and faced with these same issues. You can use a variety of back-end webserver packages such as web2py, Flask, and Bottle and they run fine under Pythonista. When you try to build your front-end using ui and webview you run into problems.
For instance running Sebastian's webbrowser with web2py blows up when you try to type into the url edit box:
Traceback (most recent call last): File "/var/mobile/Containers/Data/Application/F76C3EC3-4473-4EC8-A7EF-1CAB2BB0B437/Documents/webbrowser/webbrowser.py", line 260, in textfield_did_begin_editing self.set_url() File "/var/mobile/Containers/Data/Application/F76C3EC3-4473-4EC8-A7EF-1CAB2BB0B437/Documents/webbrowser/webbrowser.py", line 28, in set_url addr_bar.alignment = ui.ALIGN_LEFT NameError: global name 'ui' is not defined
Same thing happens with Flask. Very odd - the global name 'ui' is not defined???
The other problem I have seen is these webserver frameworks wants to log HTTP requests to the console by default and this causes the Pythonista IDE to flip from the webbrowser over to the Console. Figuring out how to configure the webserver to go silent and log to a file should mitigate this.
omz last edited by
@wradcliffe While I haven't looked at the Pythonista-Webbrowser project in much detail, the most likely reason for these errors is this:
When you run a script, Pythonista first clears all global variables. This includes imported modules, which are basically global variables as well, and it's done regardless of whether some UI is visible in a panel or sidebar. If that panel UI later calls an action that relies on the
uimodule being imported, it will fail because the global
uivariable is no longer there. So if you're doing any kind of UI that stays visible beyond the runtime of your script, I would recommend that you import required modules within the action methods etc. and not globally. You might be worried about the performance impact of doing
import uiin every action, but this usually isn't an issue because imported modules are cached anyway.
@omz - I just had a look and it has a couple of globals and uses several modules other then ui (json, os, pickle, urlparse) so I would have to scatter a lot of imports into that code.
It probably makes more sense to just hack the webserver code to create the browser and open it as part of its startup code.
You can turn off the global-clearing behavior in the settings menu.
Or, just run a single script that starts your server, then runs webbrowser (thus, nothing is ever cleared)
Can you detail the steps to reproduce this problem? How are you starting webbrowser? How are you running your server?
@JonB - I was testing things by loading and running scripts one after the other in the editor. The easiest scenario to repro is to run Sebastian's webbrowser.py and then bring up web2py using the web2py.py script. web2py.py ends with a call similar to most of the other frameworks (gluon.widget.start(cron=True)) which puts the console into a "running" state. Flask does something similar. If you now activate the webbrowser panel and try to enter the url (like http://127.0.0.1::8000) you will see the errors related to globals getting reset.
Turning off global-clearing works and verifies that this is the source of the problem.
Your idea of writing a script (using runpy?) to do this seems like a better and more practical method.
Runpy is not really needed. You could just import webbrowser.py (might be best to rename it to avoid conflicts, then make the stuff at the bottom only happen
if __name__=='__main__', then just copy those 3 lines into your own script to launch the view.
Alternatively, if you don't need the actual Sebastian webbrowser features,just use the built in