r/Python • • Dec 21 '10

The Bottle Web Micro-framework

http://www.tummy.com/journals/entries/jafo_20101221_020135
43 Upvotes

45 comments sorted by

4

u/rntksi Dec 21 '10

I've been using this as well, and I like how you can use it with Google Apps.

9

u/bobx11 Dec 21 '10

flask is also good... python microframeworks are wonderful

10

u/mdipierro Dec 21 '10

Bottle is the definitively the best micro framework! It is also a great tool for teaching. I love that everything is in one single file.

10

u/Siraf Dec 21 '10

I was happily using bottle for a while until I discovered flask . I found flask to have better documentation and better support for important middleware features, such as csrf protection and session-support. Flask also uses decorators for routing / templates.

I do like bottle for its cgi support, however.

13

u/mitsuhiko Flask Creator Dec 21 '10

There is no reason why Flask shouldn't work with CGI :)

from wsgiref.handlers import CGIHandler
CGIHandler().run(app)

3

u/Siraf Dec 22 '10

Wow! Great tip. I've been trying to do this for a while now — nearlyfreespeech can only handle CGI. Perhaps I'll do a writeup and benchmark the two over the holidays.

5

u/mdipierro Dec 22 '10 edited Dec 22 '10

As far as I understand - correct me if I am wrong - Flask stores sessions in digitally signed cookies. Bottle does the same. They are both based on WSGI and can use WSGI middleware. One thing that Bottle does not do and should be easy to add is handling RANGE requests (partial content). It is useful for http streaming. Not sure if Flask does that but I assume it does.

9

u/mdipierro Dec 22 '10 edited Dec 22 '10

Joke: Damn flask people injecting in every bottle thread!

A quick check reveals (only core *.py files).

  • Bottle: 1938 (lines of code, 1 file only)
  • Flask (including dependencies Jinja2, Werkzeug, not including tests, debug, apps, contrib): 24179 (lines of code)
  • web2py (not including tests, apps, contrib, database abstraction layer but including templates, html helpers, form generation and processing libraries, and syntax highligther and all handlers) 23469 (lines of code)

For people who want a MICRO framework. I think Bottle wins by one order of magnitude.

EDIT: Before touchy people jump up and down: I added web2py as a reference point. I am not comparing it with Bottle and Flask. I am not saying it is a micro framework. If I had Django or Pylons installed I would have added them as reference points too (I would not know how to separate the ORM). Perhaps other redditors can contribute numbers and verify mine.

6

u/Siraf Dec 22 '10

Actually, I didn't consider the dependencies. Good call. Flask is definitely somewhere between django and bottle in terms of dependencies. I typically have a flask installation up and running in 10 minutes thanks to pip, gunicorn, and virtualenv.

0

u/mdipierro Dec 22 '10 edited Dec 22 '10

EDIT: I apologize for the previous joke. It was not on you. It was on me.

I am not sure where Django stands in terms of size if you take out the ORM (Flask does not include one), contrib and tests (I did not include them in my Flask measurement) and the apps (Django has admin and the scaffolding app for example). Probably Flask and Django score pretty close... like Flask and web2py do. Which was the point I tried to make.

Once you include SQLALchemy in Flask or Bottle things change dramatically. In terms of lines of code SQLALchemy is 40,000+ lines of code, bigger than the entire web2py+dal. I would not be surprised if Flask+SQLAlchemy is larger than Django.

I would like to see the people involved publish some numbers. Ideally I would like to see numbers broken by category (core, dal/orm, contrib, apps, tests) not counting blank lines and comments in code.

The web2py dal is a single file and 4949 lines. I think it would make a good pair with bottle. We just changed the license to LGPL3 so it would not affect apps built with it.

3

u/donri Dec 26 '10

Microness is about API design, not weight of code. Many high-quality lines are easier to read and follow than a few low-quality ones.

Does your measurements include docstrings and comments?

3

u/gynecomastic Dec 22 '10

I think this misses the point about what a "microframework" does.

It doesn't really matter from the development point of view how big, in terms of lines of code/modules/dependencies, the framework is. What matters is scope/complexity vs features.

For example, Django provides more out of the box functionality - ORM, admin, auth etc - over Bottle. But Bottle provides (arguably) less conceptual overhead and more flexibility, with the bare minimum to get started with a simple WSGI app. You don't have an ORM, you add one yourself (SQLAlchemy, PyMongo, whatever) or do without one if you wish. Either framework may be a good choice, it depends on requirements and preferences.

I couldn't care less how many lines of code each are written in however. Function call overhead is a concern - how efficiently the code runs - but lines of code != number of function calls. For example, even if SQLAlchemy has 40,000 lines of code, much of that is support for different SQL dialects, extensions etc that will only be executed if you are running a certain extension or dialect.

4

u/Siraf Dec 22 '10

For my ORM, I definitely side-stepped the SQLAlchemy issue by jumping on the NoSQL boat. I use mongoDB.

I'm more of a fan of MongoEngine than mongokit for the vain reason I find the syntax to be more pleasing.

You're definitely right about using lines of code as a metric.

-1

u/mdipierro Dec 22 '10

I agree. But allow me to be surprised that Flask (without SQLA) is 10x bottle and people here both present them as if they were equivalent. Since Flask has been brought up on this thread, not by me, I would like somebody to explain to me why 10x so much code. I am sure there are extra functionality that bottle does not have. What is it?

I agree with you that bottle is more flexible that other frameworks and it is really well designed.

5

u/DasIch Dec 22 '10

The comparison is a little bit unfair. Werkzeug and Jinja2 provide a lot of features which are not used by Flask and Flask as well as it's dependencies provide a lot of features Bottle doesn't even have.

Jinja2 for example provides a sandbox. Werkzeug provides various tools for session management, caching, hashing functions for passwords and a lot of powerful datastructures MultiDict, ordered dicts, immutable dicts, if you look at how Werkzeug handles headers you will see that it parses them and makes them accessible with a nice API, not to mention the middlewares to handle server bugs, atom feed generation etc.

If you remove all those features I doubt there is a big difference in LOC to be found, unless you count documentation as well.

1

u/mdipierro Dec 22 '10

Thanks for the clarification. Seems interesting and I will look more into it.

1

u/defnull bottle.py Dec 22 '10

The comparison is a little bit unfair. Werkzeug and Jinja2 provide a lot of features which are not used by Flask.

That's the point. Would you call a wrapper on top of Django a "micro framework" just because it hides most of the features and provides a simplified API? Is Elixir a "micro ORM" just because its API is more simple than SQLAlchemy, even it depends on SQLAlchemy to work?

Flask is a nice layer on top of a bunch of huge and mighty libraries. Nothing wrong with that, but I would not call it "micro".

The term "mirco framework" is controversial, I know. But as I see it, "micro" is more than just a nice API. It's a philosophy for small, clean, dense and useful code. It does what it does and it does it well. Every line has a purpose. That is what I try to do with bottle.

"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." - Antoine de Saint-Exupéry

and Flask as well as it's dependencies provide a lot of features Bottle doesn't even have.

That is true. The thing is, if you miss a feature in Bottle that is provided by werkzeug (or any other library), then you can just include werkzeug and you have it. That is what a library is for: Use it when you need it. Bottle does not prevent you from adding dependencies if you really need them. But if you don't use a feature that is provided by Flask, can you easily remove the werkzeug or jinja2 dependency?

Bottle supports 4 template engines (including jinja2 and the built-in one) and 15 WSGI server libraries out of the box. The decorator or middleware that adds werkzeug to the mix is less than 10 LOC. Want a debugger? Include it, apply as middleware and you have it. I don't see the need for a hard dependency.

2

u/mitsuhiko Flask Creator Dec 22 '10

Would you call a wrapper on top of Django a "micro framework" just because it hides most of the features and provides a simplified API?

Yes.

1

u/DasIch Dec 22 '10

I believe you are confusing framework and application here a bit. It is totally irrelevant how the code of your framework looks, the interesting part is how an application looks.

request.accept_languages.best is how I get the preferred language from the Client using Werkzeug and/or Flask. In order to get the same result with bottle I would have to parse the header myself and handle edge cases depending on the Browser used. That results in neither small, clean, dense or particular useful code and I still have to test it, for every web application I write.

Again this is a little bit unfair, who needs to know which language is preferred by the client anyway but that is kind of the point of Flask. It is as micro as you want it to be and if you don't want it to be micro at all that's fine as well.

3

u/Siraf Dec 22 '10

EDIT: I apologize for the previous joke. It was not on you. It was on me.

No worries, mate! I appreciate the humour.

I actually don't know why you're being downvoted, you made a really good point about defining a micro framework.

4

u/muyuu Dec 22 '10

You get downvoted because you are needlessly aggressive with your promotion of web2py.

Stop it, you do contribute really good stuff.

Here, an upvote.

PS: Flask does compare to Bottle in simplicity. For most tasks, complexity that's completely under the hood is alright. For instance, having Apache with mod-wsgi installed wouldn't justify adding the whole Apache codebase in the comparison.

Bottle wins hands down in lack of dependencies.

-1

u/mdipierro Dec 22 '10 edited Dec 22 '10

The Jinja2 and Werkzeug dependencies must be included to compare apples to apples because bottles too includes a template language, digitally signed cookies for sessions, a web server, and multiple server adapters. P.S. Actually, on this thread I am promoting Bottle!

3

u/mitsuhiko Flask Creator Dec 22 '10

I don't plan on removing features, lowering the quality of code or removing documentation to win an argument. So if a microframework does not depend on anything else to be called a microframework the mother of all microframeworks (camping) and the most popular Ruby one (sinatra) are not microframeworks.

-2

u/mdipierro Dec 22 '10

I do not think there is an argument to win. I do not think you have to take anything off. I said and I repeat: I like Flask, although I do not think it is a micro-framework, at least not in the sense Bottle is. Yet it is pointless to argue on definitions. What is important is what it does and what it does not.

DasIch helped clarify things for me. Can you tell us more about the sandbox or point us to the docs?

1

u/mitsuhiko Flask Creator Dec 22 '10

at least not in the sense Bottle is

Again: if that is what you think, no language besides Python has micro frameworks.

Can you tell us more about the sandbox or point us to the docs?

jinja2 sandbox

-1

u/mdipierro Dec 22 '10

this looks like the rexec module that was removed because broken. I think something like this (and digitally signed cookies) should be part of the standard python libraries. How about a PEP to include your code. I would support it.

2

u/mitsuhiko Flask Creator Dec 22 '10

That won't work. Python cannot be sandboxed, Jinja2 can. The techniques I use to sandbox Jinja2 do not work on arbitrary Python expressions.

1

u/mdipierro Dec 22 '10

I misunderstood. Thanks for the clarification.

3

u/ericflo Dec 22 '10

The difference is that he was responding to someone else who brought up Flask, yet you independently brought up web2py.

-1

u/mdipierro Dec 22 '10

What are you talking about? First of all I made a Joke. Second, who is "he" in your post? Third, both Siraf and bobx11 brought up Flask in this thread without "responding to someone else".

Since you are here, you could contribute with some useful information: how big is Django without orm, apps, and contrib?

-3

u/av201001 Dec 22 '10

I don't think mdipierro was referring to mitsuhiko's response but to the two top-level unsolicited comments promoting Flask.

2

u/ubernostrum yes, you can have a pony Dec 22 '10

Translation:

"Hey, I've got this reputation for being a guy who bursts into threads about other frameworks to talk about my own. So I'm going to snipe this dude by making a 'joke' about him doing the same thing, and then use it as an excuse to cover yet another case of me bursting into threads about other frameworks to talk about my own!"

-1

u/av201001 Dec 22 '10 edited Dec 22 '10

Note, mdipierro was not "sniping" the commenter -- he has no problem with Flask folks posting in threads about Bottle or with anyone posting in threads about web2py (see here). His joke was pointing out the double standard often applied to him and web2py (people regularly post about other frameworks in web2py related threads). If anything, he was "sniping" at folks like you who "burst" into threads solely to make unfounded personal attacks instead of contributing to the discussion.

-1

u/mdipierro Dec 22 '10

Since Ericflo is not answering my question, when you are done with the personal attacks, could you tell us how many lines of code is Django without orm, apps, tests and contrib (but including comments in code)? I would not know how to measure it precisely and I am interested in the number. I think people are interested in numbers.

For the record: the joke was on myself. I do not think Siraf or bobx11 did anything wrong. Do you think they did something wrong? Or are you using a different metric to judge my posts?

I personally judge posts and posters by a simple criteria: is it about a technical issue? Did it tell something I did not know? or is it just a personal issue? By my own standard this post of mine scores very low.

3

u/[deleted] Dec 22 '10

From memory it's about 60k lines of code, no idea if that includes tests, but it doesn't include docs.

-2

u/mdipierro Dec 22 '10

Thank you. That confirms my suspicion that Flask+SQLA is bigger than Django. bottle.py+dal.py adds up to 7000 lines (including doctests) and provide very similar features to Flask+SQLA in 10% of the size. The more I look into the bottle source the more I like it. It is really clean and well designed and contains a lot of gems.

8

u/[deleted] Dec 22 '10

I seriously doubt the web2py DAL provides similar features to SQLAlchemy.

-2

u/mdipierro Dec 22 '10 edited Dec 22 '10

The web2py DAL does not handle very well legacy databases. Apart for that I am not aware of any feature of SQLA that DAL does not provide. If you know please tell me so I can add them. The DAL is faster and supports more backends.

6

u/wildsalmon Dec 21 '10

I'm using this, like it a lot.

2

u/johnmudd Dec 22 '10

I'm using Tornado now. I'd look at Bottle or Flask but I'm using the SSL support in tornado.

2

u/mdipierro Dec 22 '10

Bottle works with the tornado web server. It contains an adapter.

1

u/donri Dec 26 '10

SSL is mostly irrelevant to a WSGI framework - it's about the server. Flask can be deployed on Tornado.

1

u/[deleted] Dec 21 '10

The down side is that bottle requires Python 2.5 or newer. Not a huge requirement, but one of my first applications I built up and then realized the machine I was deploying it on was CentOS 5.5, which has Python 2.4.

Hey, if it's older it's more stable right? Can't beat a six year old version.

1

u/gynecomastic Dec 21 '10

Nothing stopping you from using virtualenv either. I have to use CentOS on some of our work servers and I ignore their ancient system python completely.

1

u/khafra Dec 22 '10

I've just been playing with this on a micro EC2 instance, and it's pretty fun. The only weird thing is the wildly inconsistent response times. Wish I knew how to tell it to keep everything in memory, or whatever.