Created
November 5, 2011 15:25
-
-
Save sweemeng/1341656 to your computer and use it in GitHub Desktop.
Bottle Json Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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