The framework only need to handle request, and spit data in json(maybe xml later). It does not need a template, it is json. It don't need an ORM, the data most probably scrape from somewhere else. It do not need session, it is meant to be use by library. The data is open anyway.
The first framework I try out is Bottle.
The first thing I notice is the amount of setup that I have to do, coming from a django background. Which is well known for the big settings.py file. The amount of setup is small. Just install using 'pip install bottle'.
Essentially just an application defined, with the object Bottle()
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) |
And pass to the run function.
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') |
By default bottle already have a default application, so you don't strictly need it, I just to put it there to show that it is there.
Another thing I have noticed is, there is no url route in a separate file. A route decorator is added to a function that I want to serve in the web app. The route is part of the application(the Bottle() object), and I can limit the type of request I can do on it, like POST/GET. I found that this approach is pretty clean, it reduces the boiler plate like in django views.
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 | |
Another thing to notice is. I do not specify a response method/object(like django). That is another nice thing about bottle. If the function returns a dict, the response will be in json. If string then the mimetype is text, etc. There is no need to specify a function for response.
Finally to run the app, just run python server.py (or any python file with the bottle run function). You have an webapp.
Finally to run the app, just run python server.py (or any python file with the bottle run function). You have an webapp.
For this project I didn't test the template, but from the doc, it is specified with a view decorator, which I think is nice, but I don't need it now. From the doc, I found that it is pretty clean.
Because bottle is a micro framework, there is no manage.py script like django, no ORM, I uses sqlalchemy here. There is no session support too. But interestingly I don't feel that I missed anything. In fact, it is pretty pleasant to use. Though session will definitely bite me if I ever have to implement login, but solution is on the documentation.
Overall, it is a fun framework to use, even though this is a small project. The documentation is pretty good. I might use it for future project.
No comments:
Post a Comment