使用web开发框架

更新时间:2015/09/18 访问次数:17280

简介

下面介绍一下不同python web框架的hello world应用在TAE上的使用方式。

Flask

myapp.py
from flask import Flask,render_template
app = Flask(__name__)
@app.route('/hello')
def hello_world():
    return 'Hello World!'
@app.route('/')
def index():
    return render_template('index.html')
@app.route('/hello/<name>')
def hello(name):
    return render_template('index.html',name=name)
templates/index.html
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
</head>
<title>Hello from Flask</title>
{% if name %}
    <h1>Hello {{ name }}!</h1>
{% else %}
    <h1>Hello World!</h1>
{% endif %}
</html>
app.yml
init: /myapp.py
framework: flask

web.py

myapp.py
import web
render = web.template.render('templates/')
urls = (
 '/(.*)', 'index'
)
class index:
    def GET(self, name):
    return render.index(name)
templates/index.html
$def with (name)
$if name:
    I just wanted to say <em>hello</em> to $name.
$else:
    <em>Hello</em>, world!
app.yml
init: /myapp.py
framework: web.py

Pyramid

myapp.py
from pyramid.config import Configurator
from pyramid.response import Response
def index(request):
    return Response('Hello World!')
def hello_world(request):
    return Response('Hello %(name)s!' % request.matchdict)
config = Configurator()
config.add_route('index', '/')
config.add_view(index, route_name='index')
config.add_route('hello', '/hello/{name}')
config.add_view(hello_world, route_name='hello')
app = config.make_wsgi_app()
app.yml
init: /myapp.py
framework: pyramid

Tornado

myapp.py
import tornado.wsgi
class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")
application = tornado.wsgi.WSGIApplication([
(r"/", MainHandler)
 ])
app.yml
init: /myapp.py
framework: tornado

FAQ

关于此文档暂时还没有FAQ
返回
顶部