Category Archives: web framework

Adventure in Bottle( the web framework)

So I have been scraping data online for sometime. While scraperwiki have an API that allow third party app to get data in json/xml form. I think I can make it easier, because scraperwiki query involve doing a sql query on the sqlite datastore. Thus I take the opportunity to learn new python web framework.

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()
And pass to the run function. 
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. 
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. 

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.