Skip to content

Instantly share code, notes, and snippets.

@amundo
Forked from sweemeng/base.py
Created April 14, 2013 17:47

Revisions

  1. @sweemeng sweemeng revised this gist Nov 5, 2011. 2 changed files with 8 additions and 2 deletions.
    3 changes: 3 additions & 0 deletions base.py
    Original file line number Diff line number Diff line change
    @@ -3,8 +3,11 @@
    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)
    7 changes: 5 additions & 2 deletions mp_profile.py
    Original file line number Diff line number Diff line change
    @@ -5,10 +5,12 @@
    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():
    @@ -18,11 +20,12 @@ def mp_list():

    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')
    print id

    query = swdata.select(swdata.c.key == int(id))
    result = engine.execute(query)
    data = {}
  2. @sweemeng sweemeng revised this gist Nov 5, 2011. No changes.
  3. @sweemeng sweemeng created this gist Nov 5, 2011.
    15 changes: 15 additions & 0 deletions base.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    from bottle import Bottle
    from sqlalchemy import create_engine
    from sqlalchemy import MetaData
    from sqlalchemy import Table

    app = Bottle()

    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)
    34 changes: 34 additions & 0 deletions mp_profile.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    from base import app
    from base import engine
    from base import swdata
    from bottle import request
    from bottle import response
    from bottle import route

    @app.route('/mp/api/list/')
    def mp_list():
    result = engine.execute('select * from swdata')
    all_data = []
    for item in result.fetchall():
    data = {}
    for key in result.keys():
    data[key] = item[key]

    all_data.append(data)

    return {'data':all_data}

    @app.route('/mp/api/view/',method='GET')
    def mp_view():

    id = request.GET.get('id')
    print 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

    8 changes: 8 additions & 0 deletions server.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    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')