Bottle on GAE April 6th, 2014

 

For simple applications, Bottle is one of the simpliest and handiest options out there. 

Bottle is a fast, simple and lightweight WSGI micro web-framework for Python.

It is distributed as a single file module and has no dependencies other than the Python Standard Library.

That makes Bottle so lovely.  See a simple example from its official site: 

from bottle import route, run, template

@route('/hello/<name>')
def index(name):
    return template('<b>Hello {{name}}</b>!', name=name)

run(host='localhost', port=8080)

So is it possible to get it running on Google App Engine (GAE)? 

Yes, this note simply introduces how to do that.

Since it is super easy, just present an example.

Steps: 

1. put 'bottle.py' into your GAE fold -- 'bottle.py' can be used standalone and be downloaded on its official site.

2. edit app.yaml

 

application: gdict
version: 1
runtime: python27
api_version: 1
threadsafe: yes

builtins:
- remote_api: on

handlers:

- url: /.*
  script: mainbottle.app

Note: the handling script is "mainbottle.app", not "mainbottle". 

 

3. "mainbottle.py" is as follow: 

 

__author__ = 'zheng'

import bottle
from bottle import *
import urllib2
from bs4 import BeautifulSoup

def crawl_page(keyword='happy'):
    url = 'http://dict.youdao.com/search?le=eng&q=%s&keyfrom=dict.index' % keyword
    url = 'http://www.google.com'
    # use try to check network connection
    try:
        page = urllib2.urlopen(url).read()
    except:
        print 'ERROR: network connection failed.'
        sys.exit()
    soup = BeautifulSoup(page)
    return soup

app = Bottle()

@app.get('/bottle/')
def homeGET():
    return "Look mah, Python 2.7 with no training wheels!"


@app.get('/bottle/url')
def get_url():
	soup = crawl_page()
	return str(soup)


bottle.run(app=app, server='gae')

 

 

Only 3 simple steps, just a little bit different from running Bottle (should use 'bottle.run(app=app, server='gae')' for handling).

 

 

 

 

 

 

 


Relate Posts:

Tags: GAE Bottle

Posted in Google App English |

Leave a Reply