Skip to content

Instantly share code, notes, and snippets.

@amundo
Forked from sweemeng/base.py
Created April 14, 2013 17:47
Show Gist options
  • Save amundo/5383571 to your computer and use it in GitHub Desktop.
Save amundo/5383571 to your computer and use it in GitHub Desktop.
from bottle import Bottle
from sqlalchemy import create_engine
from sqlalchemy import MetaData
from sqlalchemy import Table
# Main Web App
app = Bottle()
# Configuration for sqlalchemy
# source https://scraperwiki.com/scrapers/malaysian_mp_profile/
DB_CONN = 'sqlite:///malaysian_mp_profile.sqlite'
engine = create_engine(DB_CONN)
meta = MetaData()
meta.bind = engine
meta.create_all()
swdata = Table('swdata',meta,autoload=True)
from base import app
from base import engine
from base import swdata
from bottle import request
from bottle import response
from bottle import route
# list MP
@app.route('/mp/api/list/')
def mp_list():
result = engine.execute('select * from swdata')
all_data = []
# convert from sqlalchemy to dict
for item in result.fetchall():
data = {}
for key in result.keys():
data[key] = item[key]
all_data.append(data)
return {'data':all_data}
# View Indivisual MP
@app.route('/mp/api/view/',method='GET')
def mp_view():
# get from ?id=n
id = request.GET.get('id')
query = swdata.select(swdata.c.key == int(id))
result = engine.execute(query)
data = {}
output = result.fetchone()
for i in output.keys():
data[i] = output[i]
return data
from bottle import run
from bottle import debug
from base import app
from mp_profile import mp_list
from mp_profile import mp_view
debug(True)
run(app,host='localhost',port='8080')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment