368 points · by welder · 10y ago · 126 comments
The minimalist nature of Flask, not forcing you to do things in one way and one way only, gives lots of power, but also lots of chances to mess it up and end up with a totally unmaintainable crap. Great power comes with great responsibility. Personally, I quite liked https://github.com/mattupstate/overholt (although some dependencies are out dated!). Also, having some starting point with the boilerplate code already in place helps you to prototype faster, etc.
Speaking of modules, the Flask modules ecosystem is pretty great and familiarizing yourself with it might save plenty of time you'd spend on re-implementing them. Some popular modules that are worth mentioning:
1) Flask-assets https://flask-assets.readthedocs.io/en/latest/ allows to easily integrate processing of the assets (JS minification, SASS compilation, etc) into your development pipeline;
2) Flask-login https://github.com/maxcountryman/flask-login simple users management (roles, authentication, etc.);
3) Flask-SQLAlchemy http://flask-sqlalchemy.pocoo.org/2.1/ simple integration with SQLAlchemy
4) Flask-Script https://flask-script.readthedocs.io/en/latest/ makes it easy to write command line tools which run in the context of Flask application (e.g. some cron jobs).
That said, you shouldn't have to manually test anything—that's what automated tests are for. With regard to testing Flask applications specifically, there is a guide[0] in the docs that covers everything you need to get started.
I'd love a re-write of http://web2py.com that conforms to pythonista expectations.
It's the flexibility versus batteries included tradeoff, and I don't think it's a negative that Flask doesn't ship most of that. You could probably somewhat get there with a number of third party additions (gedrap mentioned some) but you might as well use Django if you find yourself needing that stuff baked in.
cookiecutter https://github.com/sloria/cookiecutter-flask.git
Then customise all you want.https://github.com/GoogleCloudPlatform/appengine-python-flas...
This got me going with Flask. It's not quite everything you asked for but it lays a foundation upon which to build and explore the different features you've listed. And App Engine's free tier provides a nice platform for prototyping and simplifies deployment.
Flask-Assets makes you list every static file in a giant config, aka registering your assets in bundles. There is no need for that mess.
In any case: Congrats :)
https://github.com/pallets/flask/issues?q=is%3Aopen+is%3Aiss...
Glad to see Flask's development is back on track, and under formal management!
Blog post about it: https://www.palletsprojects.com/blog/flask-011-released/
It's been sitting for three years because it was just working and given it's minimal nature, there were not many things that should be changed. So not much point to make a release.
For another, any bugfixes/improvements that had been requested by the community were inaccessible on a release branch, even after they were committed – this sounds extremely frustrating.
Most libraries out there are "just working" – doesn't mean they can't ship improvements, and most do – typically no more irregularly than every 6mo.
All the new features/fixes, contributions finally being released, the increase in development activity, it's all bullshit, people only care about the digits.
Sigh. You're projecting.
My belief is that when you are learning, start with a minimal framework so that not everything is already done for you a.k.a magic. That will make you think and make some choices. Sure it may be amateur choices in the beginning but you will become a better developer. So if you want to learn python for web, go with Flask.
So having used both, I'd nitpick you on this assertion.
from flask import request
to get at the individual request objects is sooooo much more magical than def my_view(request):
(IMO)Nevertheless, it's a good framework!
It's quite nice that you can just do
@route("/<post_id>")
def my_view(post_id):
....
never having to register anything and just decorating your functions with these path decorators gets you started really really fast. That's how a flask hello world is just 6 LOC and `python ./my_server.py`.Flask is a fantastic, minimal framework, and Mitsuhiko did so much to help new developers integrate into the ecosystem he created. For example, I was able to use his documentation templates https://johnwheeler.org/flask-ask/, and he had multiple versions for small and large projects. When you dive in it's pretty amazing what they've accomplished over the last 5 years.
https://github.com/level09/enferno
This is a Flask based framework designed with best practices in mind, it comes with many nice preconfigured modules and modern features:
* Gulp integration (sass, minify etc ..)
* Fully configured flask security out of the box (register/login/reset etc ..)
* Cache, Blueprints, Mysql/Nosql, Mail, Background tasks, ORM etc ..
* Debug toolbar
* Command line (Flask script) to help you control your app
* A deployment script that fully configures your server in minutes (Ubuntu):
https://github.com/level09/enferno-ansible
edit: I'll get this project upgraded to 0.11 to integrate the nice new features, any recommendations/notes are welcome.
ps: well thanks for the hints below guys.
Here is for example my Hearthstone simulator, with ~600 pytest-compatible tests: https://github.com/jleclanche/fireplace/tree/master/tests
It's just plain asserts. You run it with `py.test`. Nothing simpler.
People say "it's Pythonic, you just use asserts", but asserts aren't Pythonic. For one, asserts are removed when you run Python with "-O", so, uh, good luck running your test suite in that configuration. Secondly, crawling up the stack to inspect the bytecode to figure out what condition was actually asserted so you can print a nice error... that's the opposite of Pythonic.
Sure, the unittest module in the standard library is a bit Java-ish, but it's much less magic, and I feel like keeping per-test state in a class that gets instantiated multiple times is more Pythonic than having everything be top-level functions.
And of course unittest is in the standard library, which means that most apps and libraries are going to use it, most people are going to be familiar with it, so your app or library will be more approachable to co-workers and/or contributors if it's tested the same way. Heck, a lot of Python frameworks have their own test helpers built on top of unittest (hello, Twisted and Django!) so you don't get to choose anyway.
You could write Python code with three-tab indents, but you should use 4-spaces like everybody else. You could write your own custom packaging and dependency management system, but you should use setuptools like everybody else. You could any number of custom test harnesses, but you should use unittest like everybody else.
Which is not something anyone has ever wanted.
> Sure, the unittest module in the standard library is a bit Java-ish, but it's much less magic, and I feel like keeping per-test state in a class that gets instantiated multiple times is more Pythonic than having everything be top-level functions.
It's really really unpythonic. It looks horrible and leads to messy tests. With pytest you get dependency injection via fixtures which cuts down on so much repetitive initialisation code.
We've got both kinds of tests in our codebase, and the pytest ones are a lot nicer to write and refactor.
In fact, a lot of the arguments still apply. "urllib3 is in the stdlib, why use requests?". I can get behind that argument in specific cases, too - I have a library which uses urllib3 because it only ever needs to GET from a couple of URLs and introducing an extra dependency just for that is a hassle.
But here's the thing about tests. You want them to be clear, you want them to be easy, painless and even fun to write. pytest achieves that. unittest doesn't. If you're serious about testing, unittest will harm your project.
no one ever does this
Yes Pytest is black magic(I tried and failed to understand it for my own dark purposes). But it lets you get context without violating repeating yourself over and over.
That's almost entirely all python -O actually does. The only other thing it does is set __debug__ to 0. There's essentially no real value in running code with the -O flag.
For those who don't know fixtures are a type of dependency injection. You just make a function (e.g game) in a file called conftest.py, and wrap it with a '@pytest.fixture'. Then any tests you write that have an argument named 'game' will have the result of the 'game' function passed as a value.
So 'game' could return 'prepare_game()', and other fixtures can do the same with their arguments to build more complex fixtures. I find it saves a load of initialization code being repeated.
[0] http://stackoverflow.com/a/11837973
[1] https://robots.thoughtbot.com/lets-not#will-our-mystery-gues...
I should definitely use fixtures - been meaning to! It's why I love pytest, you don't need to go deep into it to make a high quality test suite.
Both are best of breed, and complementary, IMHO.
While looking at nose, I ran across this: https://nose.readthedocs.io/en/latest/
> Note to Users: Nose has been in maintenance mode for the past several years and will likely cease without a new person/team to take over maintainership. New projects should consider using Nose2, py.test, or just plain unittest/unittest2.
That made me not want to investigate nose any further, and I decided I had to just decide and move on, and chose unittest. The suggestion to use unittest in the Note helped make that easier.
Maybe next time I'll use pytest, but I think the issue in the Note will dissuade me from using nose unless it's resolved.
As such, you need pytest, nose or unittest + ddt|nose-parameterized.
https://talkpython.fm/episodes/show/13/flask-web-framework-a...
I ended up getting the Flask Cookbook from O'reilly which helped me understand how to structure a Flask App.
I also recommend Flask-SQLAlchemy as it made writing and manipulating MySQL queries really straightforward.
One reason you might use Flask-Diamond instead of Django is because you might not need everything Django provides, so you want to remove libraries one at a time. Or, perhaps, you want to upgrade libraries one at a time. I've found it's easier to start with a full-blown Flask app, then remove things, rather than starting with a barebones app that I add to.
When I know I need an admin and lots of built in items out of the box, I choose Django.
But a quick prototype, always start with Flask.
If I was a python dev it seems like it might be worth learning but since I'm a web dev and already know a few frameworks I'm struggling to find a reason to teach my self flask.
Does Flask have any advantage over something like Angular?
I don’t know what you’d consider high traffic, but we have used and continue to use Flask in production for a variety of systems. Those include:
- providing the back ends for relatively large and complicated single-page applications;
- implementing more traditional web sites, with very little JS on the front end, which use Flask to do almost all of the routing and rendering; and
- implementing dedicated HTTP-based APIs for integration with third party services.
Flask is an excellent tool, and that is not a description I use for very much in the software world. It provides widely useful functionality, it has a tidy design and a reliable implementation, it’s well documented, and it imposes few overheads or unnecessary dependencies. If you’re purely a front-end dev then that functionality isn’t what you need and neither is Flask. If you’re interested in the back end as well, you could do a lot worse than to learn Flask.
There are two main features of Flask for me, over something like RoR: it's Python (my preference over Ruby); and it's a very light framework with a ton of modules, compared to my perception that RoR is pretty big and heavy and opinionated.
YMV, I only do this stuff in my evenings so I'm not terribly familiar with it all.
That's because Flask is simple. You advertise RoR when you want somebody who doesn't need to spend a lot of time learning Rails. But nobody needs to spend a lot of time learning Flask.
That's also a drawback of Flask, in that you have to build or add more to get the same higher-level expressiveness of something like Rails. But if what the project needs is a relatively low-level web framework, it's a fine, easy-to-learn choice.
Uber uses Flask & Nginx for part of their site.
The 2012 Obama campaign team (who really shook up the use of internet & tech in campaigning) used Flask too.
Mailgun uses Flask for their API (reported). Stack is verified here, but no details given: http://stackshare.io/mailgun/mailgun
Angular = Front end
Bizarre you are comparing them. Suggests you have not used either.
I have no idea where you are looking for jobs but where I am demand for Flask and Django are booming and there is a shortage of them.
https://www.quora.com/What-challenges-has-Pinterest-encounte...
You should be comparing it to micro web frameworks like Nodejs' Express, Ruby's Sinatra, PHP's Slim, Java's Spark, etc.