News for hackers. Stuff that matters.
Re:Flask 1.0 Released by BerislavLopac on Thursday April 26, 2018 @05:23PM (#16935950)
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

[1] https://falconframework.org/

Re:Flask 1.0 Released by ckuhl on Thursday April 26, 2018 @05:37PM (#16936079)
The 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.

Re:Flask 1.0 Released by robertlagrant on Thursday April 26, 2018 @05:44PM (#16936138)
"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.

Re:Flask 1.0 Released by ddorian43 on Thursday April 26, 2018 @06:06PM (#16936314)
Django has nicely integrated components, but each of flask components are better (jinja2, sqlalchemy, migrations, etc).
Re:Flask 1.0 Released by bobwaycott on Friday April 27, 2018 @07:36PM (#16945258)
Those aren’t Flask components. They just work well with Flask. And Jinja, SQLAlchemy, and Alembic just happen to be the largest and best of them.

Many of these independent components pale in comparison to their Django counterparts in glaring ways—they are often a pain to use, with some pretty questionable internal code choices. They remain used because they are often the only choice out there, even though their implementations are inferior to Django’s.

I say this as someone who has used Flask for over 5 years, as well as nearly 5 years of Django.

Re:Flask 1.0 Released by ddorian43 on Friday April 27, 2018 @08:11PM (#16945406)
They are flask components cause they work well with flask and work bad with django.

Parts that I mentioned are (or were when I compared) light years ahead (especially sqlalchemy).

Re:Flask 1.0 Released by bobwaycott on Friday April 27, 2018 @10:23PM (#16945776)
I totally agree on SQLAlchemy. It’s very good. As is Alembic.

I feel like most using Flask would consider “Flask components” to be equivalent to Flask extensions—e.g., Flask-SQLAlchemy vs SQLAlchemy proper. Maybe it’s a pedantic nitpick, but I’ve never heard anyone refer to SQLAlchemy as a Flask component in 5+ years, hence my comment about the distinction. But yes, many of the Flask-compatible Python packages you mentioned are quite a joy to use.

Re:Flask 1.0 Released by cglace on Thursday April 26, 2018 @06:00PM (#16936266)
I 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.
Re:Flask 1.0 Released by hobolord on Thursday April 26, 2018 @05:48PM (#16936173)
this 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
Re:Flask 1.0 Released by ckuhl on Thursday April 26, 2018 @06:01PM (#16936278)
That 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.
Re:Flask 1.0 Released by stuaxo on Thursday April 26, 2018 @10:04PM (#16937921)
Especially when you then go to maintain a Flask project where they have re-implemented Django, except you don't know where anything is or how it works.
Re:Flask 1.0 Released by donarb on Friday April 27, 2018 @01:55PM (#16942766)
Exactly. I have used Django quite a bit over the years and have maintained a few prebuilt (by previous developers) Flask apps for clients. Recently I had a need for a small web service so I thought I'd use Flask. After days of trying to get something simple running and installing numerous packages, I gave up on Flask. I had a server with everything I needed in less than a day with Django.

Some of the design decisions of Flask just don't seem logical, such as using the db instance when defining model parent classes. Makes for strange contortions when defining multiple model files to prevent circular references or db not found errors.

  db = SQLAlchemy(app)
  
  class User(db.Model):
    ...
Re:Flask 1.0 Released by qudat on Thursday April 26, 2018 @06:54PM (#16936714)
I disagree. With any sufficiently complex project, Django's batteries included got in the way whereas scaling Flask felt smooth.
Re:Flask 1.0 Released by StavrosK on Thursday April 26, 2018 @07:16PM (#16936866)
Can you elaborate? In all the dozens of Django projects I've worked with, I've never felt like Django got in the way, mainly because everything is pluggable and you can just not use it if you don't want to.
Re:Flask 1.0 Released by citilife on Thursday April 26, 2018 @06:33PM (#16936567)
Both Django and Flask make me yearn for Rails :(
Re:Flask 1.0 Released by methochris on Friday April 27, 2018 @09:15AM (#16940538)
i learned to program with python and flask and promptly started hammering out my project. flask was great for learning piece by piece what goes into a web app but eventually it grew to a point where i was relying on all these extensions that were no longer maintained and i was spending alot of time to configure and setup every last bit of functionality i needed and every extension was like a new DSL to learn.

The obvious move at the time was to move to django and I spent some time familiarizing myself with it's own DSL. But boy was i struggling all along the way to do things how I liked in Flask with django. The Django Templates were extremely limiting (no modules) and couldn't even do regular python things and I came to find that extensions for Django were in no better shape than flask's. There was no clear solution for rate limiting and various other things I was looking for.

I've followed rails from a distance for a long time but, coming from python, ruby seemed so abstract to me i could just never figure it out but towards the end of my django time, i was finding that rails did/had everything i wanted and it all worked just the way i wanted it to and it was calling to me really hard. i took a big leap and spent a couple months diving fully into ruby, doing all the tutorials, reading all the books. at this point i am officially converted and my project app with it is already further along than i ever got with flask or django. when they say "ruby/rails is built for programmer happiness", they really mean it. and i can really feel/appreciate it. it's extremely fun to use and to see real progress without having to rack my brain and figure out some internals before i can proceed every so many hours. i'm just getting shit done and things work just how you'd intuitively think they should. rails ftw

Re:Flask 1.0 Released by scardine on Thursday April 26, 2018 @05:43PM (#16936127)
+1 You nailed it, this mirrors my experience.
Re:Flask 1.0 Released by dfee on Thursday April 26, 2018 @05:39PM (#16936098)
Pyramid 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/in...

I’ve used it off and on since 2010 (as Pylons) and have been happy with it.

Re:Flask 1.0 Released by hathawsh on Thursday April 26, 2018 @07:24PM (#16936921)
Wow, 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://trypyramid.com/community-powered-by-pyramid.html

Re:Flask 1.0 Released by eurleif on Thursday April 26, 2018 @05:42PM (#16936124)
>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.

Re:Flask 1.0 Released by BerislavLopac on Friday April 27, 2018 @02:46AM (#16938865)
OK, 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. :-/
Re:Flask 1.0 Released by eurleif on Friday April 27, 2018 @03:06AM (#16938953)
>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.

Re:Flask 1.0 Released by CuriouslyC on Friday April 27, 2018 @09:01AM (#16940458)
The 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.
Re:Flask 1.0 Released by theptip on Thursday April 26, 2018 @06:58PM (#16936745)
Another interesting API framework from the DRF author:

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").

Re:Flask 1.0 Released by BerislavLopac on Friday April 27, 2018 @02:41AM (#16938842)
Yeah, Apistar has an interesting approach, but that's just more unpredictable magic.
Re:Flask 1.0 Released by wybiral on Thursday April 26, 2018 @05:48PM (#16936175)
I'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.

Re:Flask 1.0 Released by discordance on Thursday April 26, 2018 @08:44PM (#16937478)
Check out Sanic if you want an async python framework with a similar API to flask
Re:Flask 1.0 Released by SmirkingRevenge on Friday April 27, 2018 @12:29AM (#16938447)
Its 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.

Re:Flask 1.0 Released by BerislavLopac on Friday April 27, 2018 @02:51AM (#16938890)
Most 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.
Re:Flask 1.0 Released by malloryerik on Thursday April 26, 2018 @10:41PM (#16938082)
Do 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?
Re:Flask 1.0 Released by wybiral on Thursday April 26, 2018 @10:53PM (#16938132)
Python 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).

Re:Flask 1.0 Released by weberc2 on Thursday April 26, 2018 @05:38PM (#16936089)
I 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).
Re:Flask 1.0 Released by icebraining on Thursday April 26, 2018 @07:04PM (#16936785)
I wouldn't call it obscure, it's just an abstraction.

But you can always use the underlying library (http://werkzeug.pocoo.org/), which does send everything through a function. Or just implement WSGI directly.

Re:Flask 1.0 Released by weberc2 on Thursday April 26, 2018 @09:43PM (#16937823)
It'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
Re:Flask 1.0 Released by giancarlostoro on Friday April 27, 2018 @07:47AM (#16940002)
I'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/

Re:Flask 1.0 Released by danielhochman on Thursday April 26, 2018 @06:26PM (#16936504)
It is possible to register error handlers for exceptions in Flask. See http://flask.pocoo.org/docs/1.0/patterns/apierrors/.
Re:Flask 1.0 Released by otterlicious on Thursday April 26, 2018 @07:12PM (#16936832)
There 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/

Re:Flask 1.0 Released by nkozyra on Thursday April 26, 2018 @10:05PM (#16937923)
It'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.

Re:Flask 1.0 Released by surbas on Thursday April 26, 2018 @05:57PM (#16936243)
Falcon 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.

Re:Flask 1.0 Released by mixmastamyk on Thursday April 26, 2018 @10:15PM (#16937966)
What about the ecosystem around it?
Re:Flask 1.0 Released by cup-of-tea on Friday April 27, 2018 @04:10AM (#16939188)
What about Pyramid?