r/Python • u/davidism Flask Maintainer • May 12 '21
News New major versions of Flask, Jinja, Click, and Werkzeug released!
Representing over two years of work from the Pallets team and contributors, new major versions Flask, Werkzeug, Jinja, Click, ItsDangerous, and MarkupSafe have been released on May 11, 2021. Check out our announcement on our blog: https://palletsprojects.com/blog/flask-2-0-released/, and retweet it to spread the word on Twitter as well: https://twitter.com/PalletsTeam/status/1392266507296514048
Every project has significant changes, and while we don't anticipate breaking things, it may take some time for extensions and other projects to catch up. Be sure to use tools like pip-compile and Dependabot to pin your dependencies and control when you upgrade.
Overall changes to every project include:
- Drop Python 2 and Python 3.5 support. Python 3.6 and above is required, the latest version is recommended. Removing the compatibility code also gives a nice speedup.
- Add comprehensive type annotations to all the libraries.
- Better new contributor experience with updated contributing guide and consistent code style with tools like pre-commit and black.
Check out the changelog links for each project to see all of the great new features and changes. I've included some of the highlights here as well.
async defviews and callbacks.- Nested blueprints.
- Shortcut HTTP method route decorators like
@app.post()and@app.delete(). - Static files like CSS will show changes immediately instead of needing to clear the cache.
multipart/form-datais parsed 15x faster, especially for large file uploads.- Getting ready for async support behind the scenes.
- Improved test client experience.
- Routing understands websocket URLs.
- Async support no longer requires patching.
- Lots of weird scoping fixes.
- I18N supports
pgettext.
- Completely rewrote the shell tab completion system to be more accurate, customizable, and extensible to new shells.
- Support for 256 and RGB color output.
- Options can be given as a flag without a value to use a default value or trigger a prompt.
*and~patterns are expanded on Windows since its terminal doesn't do that automatically.- User-facing messages like validation errors can be translated.
- ItsDangerous 2.0
- MarkupSafe 2.0
Four years ago, each project had 150+ open issues, some going back a decade, and pages of open pull requests too. Over time I've grown the maintainer team and the community, and we've managed to cut down the backlog to a much more manageable size. I'm thankful for all contributions and support people have given, and I hope you all continue to build amazing applications with the Pallets projects.
15
u/[deleted] May 12 '21
Great job guys. Couple of notes, if it helps anybody :)
What we get with the async part of this release - If you would like to run an async library or your own async code from a flask route you can do that now. This is super useful, where let's say we have some async code that fetches data from many sources concurrently, or call multiple a few ML prediction endpoints at the same time (as long as they don't time out) using httpx and respond with some sort of outcome, or finally try that cool new async-only database library. A current (v2) limitation is that the you can't make concurrent requests using just the current asyncio implementation (an alternative with Flask API and ASGI: Quart). Typically in production gunicorn or uwsgi + threads/processes/gevent-eventlet is used and this makes Flask behave asynchronously. More here and here if interested.
---
There's always comparisons with other frameworks like Django/Rest or FastAPI, especially for those coming from data science and new-ish at web frameworks
To get Flask + API, Swagger, Pydantic or Marshmallow Validation out of the box that you may know know of:
Flask, Pydantic (Bring your own Swagger implementation)
In Quart, FastAPI, Starlette, AIOHTTP etc. tasks can run in the background within the web-app itself and while still respond to new requests concurrently without needing multiple workers/threads, making it simpler and less resource hungry.
Worth noting that I noticed a lot of online examples showing FastAPI serving Machine Learning models - (inherently cpu intensive, blocking), don't run any actual user code that's async inside the routes, so technically don't benefit from it compared to Flask. They are also often handled in sync routes
def foo():and not async routesasync def foo():which means they aren't even handled async from the get-go. In fact, internally, Starlette (and therefore by extension FastAPI) runs background tasks and non-async routes inThreadPoolExecutor, see code here and here. It's important for those coming from DS/ML to recognize that for these use cases there's great benefit serving concurrent responses, maybe for result lookups of and db queries, but not so much with cpu-bound tasks. Being ASGI won't automatically make your app faster. And with computationally heavy tasks, just being concurrent won't make your app faster. For parallelism, (in python) multiple workers is a better strategy. Also, there's queues. More on serving computationally bound requests here9
u/road_laya May 12 '21
Nice.
8
u/pengekcs May 12 '21
I just read pgjones's website yesterday - and followed a github issue link where it was mentioned that async support was coming to flask as well (so maybe no reason to use quart?) - saw it was close to release but wasn't thinking this close.
sounds great, will definitely use. Also pgjones' has some great ideas on creating rest apis
26
u/[deleted] May 12 '21
Look out, FastAPI!
6
u/inknownis May 12 '21
Well done!!! Many thanks!
4
u/irrelevantPseudonym May 12 '21
Great work. Thanks to everyone involved.
Crazy to think what started as an April's Fool joke is now one of the most popular web frameworks.
9
u/licht1nstein May 12 '21
Thanks for all your work! I don't mostly even code in Python any more, but still think click is a gem! And I still maintain some of the first websites built with Flask :)
3
u/Jeklah May 12 '21
Thanks for Click, it's one of my favourite libraries.
4
u/Zulban May 12 '21
Great job! I use most of these and really appreciate it. I'm also teaching some of these to our new hires. First time I've heard of Pallets.
2
u/NooShoes May 12 '21
This looks amazing - congratulations to everyone on the team.
Great framework and outstanding docs!
2
u/[deleted] May 12 '21
Thanks for the hard work, it's very much appreciated!
2
u/fr33d0ml0v3r May 12 '21
I cant wait to start using the new version.
3
u/schroeder8 May 12 '21
Is there an @app.get decorator, or just @app.route without the methods argument.
2
u/alxcnwy May 12 '21
π
2
u/aes110 May 12 '21
So it's not really worth it imo.. from my experience with asyncio if you want to use it you should really commit to it (and use a framework built around it like Sanic/FastAPI), trying to mix asyncio under something that is sync at its core just doesnt fit
0
u/[deleted] May 12 '21
[deleted] β view deleted comment
0
u/[deleted] May 12 '21
[deleted] β view deleted comment
0
u/[deleted] May 12 '21
[deleted] β view deleted comment