WEB framework in QPython¶
By using the WEB development framework, developers can efficiently and quickly develop Web applications. Similarly, in QPython, you can quickly develop WebApp with the following web frameworks.
Bottle¶
Bottle is a QPython built-in WebApp development framework, you do not need to install additional, just follow the WebApp’s specifications, reference bottle development methods can be carried out.
Look at one of the most simple Hello world with bottle
#qpy:webapp:Hello Qpython
#qpy://127.0.0.1:8080/
"""
This is a sample for qpython webapp
"""
from bottle import Bottle, ServerAdapter
from bottle import run, debug, route, error, static_file, template
######### QPYTHON WEB SERVER ###############
class MyWSGIRefServer(ServerAdapter):
server = None
def run(self, handler):
from wsgiref.simple_server import make_server, WSGIRequestHandler
if self.quiet:
class QuietHandler(WSGIRequestHandler):
def log_request(*args, **kw): pass
self.options['handler_class'] = QuietHandler
self.server = make_server(self.host, self.port, handler, **self.options)
self.server.serve_forever()
def stop(self):
import threading
threading.Thread(target=self.server.shutdown).start()
self.server.server_close()
######### BUILT-IN ROUTERS ###############
@route('/__exit', method=['GET','HEAD'])
def __exit():
global server
server.stop()
@route('/assets/<filepath:path>')
def serverstatic(filepath):
return static_file(filepath, root='/sdcard')
######### WEBAPP ROUTERS WRITE YOUR CODE BELOW###############
@route('/')
def home():
return template('<h1>Hello {{name}} !</h1><a href="/assets/qpython/projects/WebAppSample/main.py">View source</a><br /><br /> <a href="http://edu.qpython.org/qpython-webapp/index.html">>> About QPython Web App</a>',name='QPython')
######### WEBAPP ROUTERS ###############
app = Bottle()
app.route('/', method='GET')(home)
app.route('/__exit', method=['GET','HEAD'])(__exit)
app.route('/assets/<filepath:path>', method='GET')(serverstatic)
try:
server = MyWSGIRefServer(host="127.0.0.1", port="8080")
app.run(server=server,reloader=False)
except Exception,ex:
print "Exception: %s" % repr(ex)
Click Run, copy the code to the editor and Run to check out the result.
Tornado¶
QPython also supports the asynchronous Communication Web Development Framework - Tornado, which you can install with QPYPI.

To install tornado library by QPYPI
After the installation is complete, click on the bottom of the following code to run the code can be copied to the editor,save the run to check out Tornado-based WebApp sample effects.
#qpy:webapp:Tornado Sample
#qpy://127.0.0.1:8080/
import tornado
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
class ExitHandler(tornado.web.RequestHandler):
def get(self):
import os,signal
os.kill(os.getpid(), signal.SIGKILL)
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
(r"/__exit", ExitHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(8080)
tornado.ioloop.IOLoop.current().start()
Also worth mentioning is that in QPython’s AIPY extension,matlibplot also supports Tornado as the default drawing support backend by default.
Django¶
As the most commonly used framework for Python Web development, Django also received QPython support, QPython provides a djangoman.py scripts to help you quickly install and initialize django library project,By running this tool,You can quickly create or manage django projects.

Django projects can be managed by Django manage script
After djangoman.py to complete the project’s initialization,You can follow the WebApp’s specifications to develop your WebApp and run it on QPython.
Other¶
In addition to the above framework,Other frameworks such as Flask, Cherrypy, etc. are also likely to be supported by QPython,If you are interested, you can explore through INSTALL FROM PYTHON’S PYPI in QPYPI.