|
| 1 | +What can QPython do? |
| 2 | +==================================== |
| 3 | + |
| 4 | + |
| 5 | +Why should I choose QPython |
| 6 | +------------------------ |
| 7 | +QPython offer **an amazing developing experience**, Now you could write & test & run your programs from your anroid, not the PC, Laptop. |
| 8 | + |
| 9 | +We think QPython's programming style may become popular in the future. |
| 10 | + |
| 11 | +QPython's main features |
| 12 | +------------------------- |
| 13 | +You can do most jobs through QPython just like the way that Python does on PC/Laptop. |
| 14 | + |
| 15 | + |
| 16 | +**Libraries** |
| 17 | + |
| 18 | +- QPython supports most stardard Python libraries. |
| 19 | + |
| 20 | +- QPython supports many 3rd Python libraries which implemented with pure Python code. |
| 21 | + |
| 22 | +- QPython supports some Python libraries mixed with C/C++ code which pre-compiled by QPython develop team. |
| 23 | + |
| 24 | +- QPython allows you put on the libraries by yourself. |
| 25 | + |
| 26 | +Besides these, QPython offers some extra features which Python doesn't offer, Like: |
| 27 | + |
| 28 | +- Android APIs Access(Like SMS, GPS, NFC, BLUETOOTH etc) |
| 29 | + |
| 30 | +*Why QPython require so many permissions?* |
| 31 | + |
| 32 | +QPython need these permissions to access Android's API. |
| 33 | + |
| 34 | + |
| 35 | +**Runtime modes** |
| 36 | + |
| 37 | +QPython supports several runtime modes for android. |
| 38 | + |
| 39 | +**Console mode** |
| 40 | + |
| 41 | +It's the default runtime mode in QPython, it's very common in PC/laptop. |
| 42 | + |
| 43 | + |
| 44 | +**Kivy mode** |
| 45 | + |
| 46 | +QPython supports `Kivy <http://kivy.org>`_ as the GUI programming solution. |
| 47 | + |
| 48 | + |
| 49 | +Kivy is an open source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps. |
| 50 | + |
| 51 | +Your device should support opengl2.0 for supporting kivy mode. |
| 52 | + |
| 53 | +By insert into the following header in your script, you can let your script run with kivy mode. |
| 54 | + |
| 55 | +:: |
| 56 | + |
| 57 | + #qpy:kivy |
| 58 | + |
| 59 | + |
| 60 | +If your library require the opengl driver, you shoule declare the kivy mode header in your script, like the jnius. |
| 61 | + |
| 62 | +*NOTE: QPython3 didn't support this mode yet* |
| 63 | + |
| 64 | +**WebApp mode** |
| 65 | + |
| 66 | +We recommend you implement WebApp with QPython for it offer the easy to accomplish UI and Take advantage of Python's fast programming strong point. |
| 67 | + |
| 68 | +WebApp will start a webview in front, and run a python web service background. |
| 69 | +You could use *bottle*(QPython built-in library) to implement the web service, or you could install *django* / *flask* framework also. |
| 70 | + |
| 71 | + |
| 72 | +By insert into the following header in your script, you can let your script run with webapp mode. |
| 73 | + |
| 74 | +:: |
| 75 | + |
| 76 | + #qpy:webapp:<app title> |
| 77 | + #qpy:<fullscreen or remove this line> |
| 78 | + #qpy://<ip:port:path> |
| 79 | + |
| 80 | +For example |
| 81 | + |
| 82 | +:: |
| 83 | + |
| 84 | + #qpy:webapp:Hello QPython |
| 85 | + #qpy://localhost:8080/hello |
| 86 | + |
| 87 | + |
| 88 | +The previous should start a webview which should load the *http://localhost:8080/hello* as the default page, and the webview will keep the titlebar which title is "Hello QPython", if you add the *#qpy:fullscreen* it will hide the titlebar. |
| 89 | + |
| 90 | + |
| 91 | +:: |
| 92 | + |
| 93 | + #qpy:webapp:Hello Qpython |
| 94 | + #qpy://127.0.0.1:8080/ |
| 95 | + """ |
| 96 | + This is a sample for qpython webapp |
| 97 | + """ |
| 98 | + |
| 99 | + from bottle import Bottle, ServerAdapter |
| 100 | + from bottle import run, debug, route, error, static_file, template |
| 101 | + |
| 102 | + |
| 103 | + ######### QPYTHON WEB SERVER ############### |
| 104 | + |
| 105 | + class MyWSGIRefServer(ServerAdapter): |
| 106 | + server = None |
| 107 | + |
| 108 | + def run(self, handler): |
| 109 | + from wsgiref.simple_server import make_server, WSGIRequestHandler |
| 110 | + if self.quiet: |
| 111 | + class QuietHandler(WSGIRequestHandler): |
| 112 | + def log_request(*args, **kw): pass |
| 113 | + self.options['handler_class'] = QuietHandler |
| 114 | + self.server = make_server(self.host, self.port, handler, **self.options) |
| 115 | + self.server.serve_forever() |
| 116 | + |
| 117 | + def stop(self): |
| 118 | + #sys.stderr.close() |
| 119 | + import threading |
| 120 | + threading.Thread(target=self.server.shutdown).start() |
| 121 | + #self.server.shutdown() |
| 122 | + self.server.server_close() #<--- alternative but causes bad fd exception |
| 123 | + print "# qpyhttpd stop" |
| 124 | + |
| 125 | + |
| 126 | + ######### BUILT-IN ROUTERS ############### |
| 127 | + @route('/__exit', method=['GET','HEAD']) |
| 128 | + def __exit(): |
| 129 | + global server |
| 130 | + server.stop() |
| 131 | + |
| 132 | + @route('/__ping') |
| 133 | + def __ping(): |
| 134 | + return "ok" |
| 135 | + |
| 136 | + |
| 137 | + @route('/assets/<filepath:path>') |
| 138 | + def server_static(filepath): |
| 139 | + return static_file(filepath, root='/sdcard') |
| 140 | + |
| 141 | + |
| 142 | + ######### WEBAPP ROUTERS ############### |
| 143 | + @route('/') |
| 144 | + def home(): |
| 145 | + return template('<h1>Hello {{name}} !</h1><a href="/assets/qpython/projects/WebApp Sample/main.py">View source</a><br /><br /> <a href="http://wiki.qpython.org/doc/program_guide/web_app/">>> About QPython Web App</a>',name='QPython') |
| 146 | + |
| 147 | + |
| 148 | + ######### WEBAPP ROUTERS ############### |
| 149 | + app = Bottle() |
| 150 | + app.route('/', method='GET')(home) |
| 151 | + app.route('/__exit', method=['GET','HEAD'])(__exit) |
| 152 | + app.route('/__ping', method=['GET','HEAD'])(__ping) |
| 153 | + app.route('/assets/<filepath:path>', method='GET')(server_static) |
| 154 | + |
| 155 | + try: |
| 156 | + server = MyWSGIRefServer(host="127.0.0.1", port="8080") |
| 157 | + app.run(server=server,reloader=False) |
| 158 | + except Exception,ex: |
| 159 | + print "Exception: %s" % repr(ex) |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | +.. image:: ../_static/guide_program_pic1.png |
| 164 | + :alt: QPython WebApp Sample |
| 165 | + |
| 166 | +In the other part of the code, you could implement a webserver whish serve on localhost:8080 and make the URL /hello implement as your webapp's homepage. |
| 167 | + |
| 168 | + |
| 169 | +**Q mode** |
| 170 | + |
| 171 | +If you don't want the QPython display some UI, pelase try to use the QScript mode, it could run a script background, just insert the following header into your script: |
| 172 | + |
| 173 | +:: |
| 174 | + |
| 175 | + #qpy:qpyapp |
0 commit comments