6 ms·
I understand that Flask has become popular because it is easy to learn, but my experience is that as your knowledge progresses it just keeps getting in your way
by BerislavLopac 9y ago
I understand that Flask has become popular because it is easy to learn, but my experience is that as your knowledge progresses it just keeps getting in your way. I particularly dislike some design choices which look like afterthought hacks, such as global variables for current request and using abort() functions instead of raising exceptions directly.
EDIT: To avoid hollow naysaying, here are some alternatives to Flask I find much better designed:
- Tornado Web Framework [0]
- Falcon [1]
[0] http://www.tornadoweb.org/en/stable/webframework.html http://www.tornadoweb.org/en/stable/webframework.html
[1] https://falconframework.org/ https://falconframework.org/
- weberc2 9y agoI agree. I particularly like Go's web ecosystem. A web server is just a function, and that function is often a composition of other functions (for example, a tree of router functions and then a handler for each route). By contrast, it seems like Flask goes to great lengths to obscure such a simple concept (decorators for routes, global variables for state and resource management, etc).
- icebraining 9y agoI wouldn't call it obscure, it's just an abstraction. But you can always use the underlying library (http://werkzeug.pocoo.org/ http://werkzeug.pocoo.org/), which does send everything through a function. Or just implement WSGI directly.
- weberc2 9y agoIt's not a very good abstraction, because (as previously mentioned) it depends on global variables to manage shared resources (e.g., connection pools) and even request state. Hence "obscure". It would be much cleaner to just define a handler type with the signature: `def handler(r: http.Request) -> http.Response`. Those handlers could be passed to routers (which themselves could be handlers), for example (not tested): class Route(typing.NamedTuple): path: typing.re.Pattern method: str handler: http.Handler class Router: def __init__(self, routes: typing.List[Route]) -> None: self.routes = routes def serve_http(self, r: http.Request) -> http.Response: """serve_http implements the http.Handler interface""" for route in self.routes: if route.path.matches(r.path): return route.handler(r) return http.NotFound("404 NOT FOUND") Note that since handlers are just functions, they can also be methods with object-level state. They needn't depend on global state at all, for example, notice how the following routes don't depend on the connection pool (or the request state) to be global: class Server(typing.NamedTuple): db: DBConnPool def first_route(self, r: http.Request) -> http.Response: """first_route uses shared `db` resource.""" pass def second_route(self, r: http.Request) -> http.Response: """second_route uses shared `db` resource too!""" pass
- dfee 9y agoPyramid is great, lacks the problems (magic) you mentioned, and is now what pypi.org is built on: https://docs.pylonsproject.org/projects/pyramid/en/latest/index.html https://docs.pylonsproject.org/projects/pyramid/en/latest/in... I’ve used it off and on since 2010 (as Pylons) and have been happy with it.
- hathawsh 9y agoWow, I didn't make the connection until now that pypi.org is built on Pyramid. That's cool. :-) Some more links for anyone interested: https://github.com/pypa/warehouse https://github.com/pypa/warehouse https://trypyramid.com/community-powered-by-pyramid.html https://trypyramid.com/community-powered-by-pyramid.html
- ckuhl 9y agoThe real value of Flask is that it makes you appreciate what Django does by default. When I first started learning Python / web frameworks, I went with Flask because it was smaller and "simpler". As my project grew however, I had to organize it. I was basically imitating what Django gives you by default, though less cleanly.
- scardine 9y ago+1 You nailed it, this mirrors my experience.
- robertlagrant 9y ago"The real value of Flask is that it makes you appreciate what Django does by default" I have exactly the opposite experience: Flask is nicer than Django; SQLAlchemy is better than Django ORM. Love a bit of Flask.
- hobolord 9y agothis only applies if your project can be organised within Django's requirements though right? I use Flask and Django a bit but it usually depends on how "cookie cutter" the project scope is
- ckuhl 9y agoThat is a fair point. Since I was doing something kind of CMS focused, Django fit the bill. I suppose if you were doing something orthogonal to that, it would be better to have the flexibility of Flask.
- cglace 9y agoI have the exact opposite opinion. Flask just gets out of my way and allows me to design my app just the way I want it.
- ddorian43 9y agoDjango has nicely integrated components, but each of flask components are better (jinja2, sqlalchemy, migrations, etc).
- eurleif 9y ago>global variables for current request Not really global variables. I would analogize it to dynamic variables from Lisp, which are a feature I wish every language had. I strongly disagree it's a hack or gets in the way. Having access to the request from anywhere without explicitly passing it around is really useful.
- BerislavLopac 9y agoOK, fair point, I used a wrong term here. So instead of global variables we have dynamic modules that represent a stateful context object which is different depending on where they are imported and when. Very un-Pythonic, and honestly I'd prefer global variables here. :-/
- eurleif 9y ago>different depending on where they are imported and when Er, no. Their value depends on where in the callstack you read it, not on where/how they're imported. They're like global variables but with thread locality, and with a stack of values.
- CuriouslyC 9y agoThe context local request object is nice (once you accept it), but it definitely complicates the testing story. Whether that is a worthwhile trade-off depends on how many tests you write I suppose.
- wybiral 9y agoI've never seen Falcon, thanks. Flask is still one of the most approachable frameworks I've used. It's great for getting something hacked out really quickly, but for asynchronous requests Tornado is where it's at. That being said, when I really want performance I usually reach for Golang.
- discordance 9y agoCheck out Sanic if you want an async python framework with a similar API to flask
- SmirkingRevenge 9y agoIts pretty hard to beat Tornado's api, IMHO. Its a really rich api for how simple and quick it is to learn. Flask is a definite downgrade to me, when it comes to the api. But Flask wins hands down on the richness of its ecosystem.
- BerislavLopac 9y agoMost of the "Flask ecosystem" are wrappers for some generic Python libraries which can easily be introduced to any other framework: SQLAlchemy for RDBMS abstraction and ORM, Jinja2 for templating, and so on.
- malloryerik 9y agoDo you find Golang to be that much harder to develop in, or is it that Python has libraries whose equivalents are missing in Golang? When would you actually choose one over the other?
- wybiral 9y agoPython is generally less to type. Go isn't hard, per se, but it is statically typed and tends to be a bit more verbose. For me: Python if there's a specific library I want to use, I'm just hacking out a proof of concept, or I'm working with text. Golang for higher performance, concurrency, stream processing, and cryptography (it has good libraries for that kind of stuff).
- surbas 9y agoFalcon is just as simple to learn as Flask but so much more pythonic. It also blazing fast. With that said good to see Flask get to 1.0. Long time coming.
- mixmastamyk 9y agoWhat about the ecosystem around it?
- danielhochman 9y agoIt is possible to register error handlers for exceptions in Flask. See http://flask.pocoo.org/docs/1.0/patterns/apierrors/ http://flask.pocoo.org/docs/1.0/patterns/apierrors/.
- otterlicious 9y agoThere are also exceptions corresponding to HTTP errors, e.g.: raise werkzeug.exceptions.NotFound instead of abort(404) http://werkzeug.pocoo.org/docs/0.14/exceptions/ http://werkzeug.pocoo.org/docs/0.14/exceptions/
- deleted 9y ago[deleted]
- nkozyra 9y agoIt's my go-to prototype framework/server, but I think it's going to complicate things unnecessarily down the road if you want to scale. You can make it work for prod if need be, though. But you can go from 0-59 with ease and easily translatable templates, etc.
- theptip 9y agoAnother interesting API framework from the DRF author: https://github.com/encode/apistar https://github.com/encode/apistar Uses Python3 type annotations to do serialization. Not sure it's ready for production, but it has some very interesting new concepts. Also worth mentioning Sanic which is fast and I believe dispenses with Flask warts like the thread-local Request object (though it does bill itself as "Flask-like").
- BerislavLopac 9y agoYeah, Apistar has an interesting approach, but that's just more unpredictable magic.
- cup-of-tea 9y agoWhat about Pyramid?
- giancarlostoro 9y agoI've always preferred CherryPy[0]. It's simple and if you write simple Object Oriented code (or have written some) you can turn it into a website, just need to "return" URLs. CherryPy has also been around 10+ years by this point (longer than Flask it seems). I like that I can stay high level, and when needed get really low level with CherrPy. I've used Flask in one other instance, and it wasn't too bad, but I missed CherryPy through the process (cleaner looking code, and obviously more familiarity for me) but I was participating at a Hackathon so maybe my experience is a little shifted. Though I never picked Flask due to never liking how it looked from even just the Hello World I was not a fan of how it's written, but that is just me coming from other languages and looking at frameworks like Sinatra (though I don't do any Ruby it was pretty clean) and Nancy (C#). [0] https://cherrypy.org/ https://cherrypy.org/