GAE+bottle+jinja2+beaker快速开发demo

    相对于Django,bottle可以看成是一个非常精巧的python web framework了,只有一个文件就可以使用了。于是想用这个东西在gae做个简单的demo。

1. GAE+bottle

logo_nav.png

http://pypi.python.org/pypi/bottle

    copy那个bottle.py到gae工程的目录里,现在,可以写一个很简短的代码来测试一下:

main.py

from bottle import route, default_app
from google.appengine.ext.webapp.util import run_wsgi_app

@route
def index():
    return 'Hello world!'

def main():
    '''Remove this when on production '''
    bottle.debug(True)
    app = default_app()    
    run_wsgi_app(app)
if __name__ == '__main__':
    main()

    同时,要修改app.yaml文件:

handlers:
- url: /.*
  script: main.py

    现在,就可以直接运行GAE,查看结果了!就这么简单!

2. GAE+bottle+beaker

    在GAE+bottle的组合中,如果要使用session的话,查询到bottle的原话如下:

How to implement sessions?

There is no build in support for sessions because there is no right way to do it. Depending on requirements and environment you could use beaker middleware with a fitting backend or implement it yourself.

     很清楚的告诉我们,如果要使用session的话,可以考虑beaker,从这里下载:

http://pypi.python.org/pypi/Beaker

     下载下来后,把里面的一个beaker文件夹,copy到GAE工程目录中,会作为一个package来使用。

    在上面的程序修改如下:

main.py

from bottle import route, default_app
from beaker.middleware import SessionMiddleware
from google.appengine.ext.webapp.util import run_wsgi_app

@route('/')
def index():
    session = request.environ['beaker.session']
    if 'refrush_times' in session:
        refrush_times = int(session['refrush_times'])
    else:
        refrush_times = 0
    refrush_times = refrush_times + 1
    session['refrush_times'] = refrush_times
    return 'Hello world! You have refrush this page for %s times.' % str(refrush_times)

def main():
    '''Remove this when on production '''
    bottle.debug(True)
    app = default_app()

    session_opts = {
                    'session.type': 'ext:google',
                    'session.cookie_expires': True,
                    'session.auto': True,
                    }
    app = SessionMiddleware(app, session_opts)
    run_wsgi_app(app)
if __name__ == '__main__':
    main()

    注意,session_opts里的session.type,如果在GAE下使用,一定要选ext:google,这个是我测试了几个选项之后才发现的。如果你有更好的方法,也欢迎告诉我,谢谢。

3. GAE+bottle+beaker+jinja2

jinja.png

    因为我之前只用过django的template engine,所以我在这里使用和django template用法差不多的jinja2,下载jiaja2:

http://pypi.python.org/pypi/pip

    把里面的jinja2的目录copy到GAE工程目录。

main.py

from bottle import route, default_app
from beaker.middleware import SessionMiddleware
from google.appengine.ext.webapp.util import run_wsgi_app
from bottle import jinja2_view as view

@route('/')
@view('templates/index.html')
def index():
    session = request.environ['beaker.session']
    if 'refrush_times' in session:
        refrush_times = int(session['refrush_times'])
    else:
        refrush_times = 0
    refrush_times = refrush_times + 1
    session['refrush_times'] = refrush_times
    return {'hello_world_str':'Hello world! You have refrush this page for %s times.' % str(refrush_times)}

def main():
    '''Remove this when on production '''
    bottle.debug(True)
    app = default_app()

    session_opts = {
                    'session.type': 'ext:google',
                    'session.cookie_expires': True,
                    'session.auto': True,
                    }
    app = SessionMiddleware(app, session_opts)
    run_wsgi_app(app)
if __name__ == '__main__':
    main()

接下来需要在GAE工程目录中创建一个templates目录,然后在里面创建一个index.html的模板

template/index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>SNS User OAUTH Demo</title>
</head>
<body>
    <p>{{ hello_world_str }}</p>
</body>
</html>

    OK,现在这个DEMO就算完成了。

    ps:发现bottle几个不方便的地方:

    1. request没有像django的request有一些对url进行方便操作的方法,这个需要自己去实现,比如django的request.build_absolute_uri(),我是这样去实现的:

    from urlparse import urlparse
    o = urlparse(request.url)
    callback = '%s://%s/%s' % (o.scheme, o.netloc, 'oauth_callback')

    2. 在这种组合下,我还没有发现如何去实现i18n,对于django来说,只需要在locate目录下创建对应语言的资源文件就行了,同时django还提供了makemessages这样的工具,非常方便,而bottle+jinja2我还没有发现如何去实现,也请已经实现了的朋友告诉我如何实现,非常感谢。

    总之,bottle是一个非常精巧的python web框架,对于一些快速的开发demo来说,非常方便,毕竟django里的很多功能是用不上的!

转载于:https://www.cnblogs.com/lettoo/archive/2011/02/25/1965200.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值