Building websites in Python with Flask
maximebf.com
maximebf.com
The only other thing i'd mention for Flask newbies would be the excellent debugger facility in Werkzeug (effectively a part of Flask).
I lied, one more reference - the guy that wrote flask (bit of a genius, also has a bunch of other excellent libraries) has shared slides from some talks he's given, they're really worth a read too: http://lucumr.pocoo.org/talks/
Bucket list 2013, attend a talk by Armin Ronacher.
One more Flask thing: Flask-Login[2] is a popular way of managing sessions.
1. Werkzeug is the WSGI utility library upon which Flask is built. If you do enough stuff in Flask, you'll probably end up learning something about it, which is okay, because the documentation — like Flask's — is excellent.
from flask import current_app
def asdf():
assert current_app.debug == False, "You just called debug!"I'd really love to see/hear some stories of others using Flask at a slightly larger scale. For example ... did your startup build their REST API in Flask? Sure, people say it's possible and outline a basic hello-world style REST app ... but in my experience it became a pain in the ass very quick. Primarily because no structure is enforced.
After being a Python dev for a long time, building quite a few big Django apps and a handful of tiny Flask apps I have finally jumped ship to Rails. I spent a week experimenting with various Pythonic combinations. Ultimately I had to ask myself ... "Why am I doing this? What the hell am I wasting my time with this lightweight framework?" I've built my current REST API in Rails and would not have it any other way. It's been tremendously efficient and performs great on my little Linode in London + PostgreSQL.
Flask apps are supposed to be tiny. I personally use three different stacks for different things: web.py for larger projects, flask for midsized projects and bottlepy for really quick and dirty stateless projects
So it's coming, hold on! :-)
On another note, I'd recommend that you check out my good pal Kenneth's link (also a comment to your comment).
And on a seperate note, I'd recommend watching "Diving into Flask - Head on" from EuroPython 2012: http://www.youtube.com/watch?v=5twaatBqmjE
It's basically a talk about a company that rewrote their current PHP site and took on Python and Flask - kept at it and banged out something pretty advanced. I liked watching it. I hope you do too.
Edit: adding a link to Blueprints in the Flask docs: http://flask.pocoo.org/docs/blueprints/
[0]: http://vertstudios.com/blog/new-flask-site/ [1]: https://github.com/joequery/Vert-Flask
Since you're already using backbone.js why not plug in CSRF protection with Flask-SeaSurf? (http://flask.pocoo.org/extensions/)
If you use this backbone.js change then CSRF is practically invisible: https://github.com/alanhamlett/backbone/commit/91941afe693ae...
Also, if building an api with Flask make sure you use blueprints with a url prefix so your api routes become relative, which means less typing: http://flask.pocoo.org/docs/blueprints/
http://flask.pocoo.org/docs/blueprints/
Going small? Put everything in one folder.
app = Flask(__name__, static_folder='.', static_url_path='', template_folder='')
When I was starting out, I found the source code to the Flask website itself really helpful for getting to grips with the basics: https://github.com/mitsuhiko/flask/tree/website/flask_websit...
I know Java is verbose but the tooling is good. Just put you war(a pre-defined structure) in Tomcat webapps dir and your good to go with a fairly scalable web site. I was so surprised it wasn’t that easy for Python. I think it stems out that Python is more of systemish kinda ecosystem.
I'm still bemused at how much of a free quota you get on GAE. I've had days with a few thousand hits, that doesn't even take any of my daily quotas above 35%.
The other thing for me is that I have now spent time and invested hours into learning the App Engine platform but I get almost nothing in return since moving requires me to re-write or re-learn other tools anyway.
Unless I was a 100% committed to App Engine I don't want to launch a new project on top of it.
It took a while, but I really like Heroku's workflow so far and I feel like I have to jump through a lot less hoops. For me, it's the perfect middle ground between buying and admining some ubuntu VM somewhere and GAE's deploy and hope it works style.
Unless I'm wrong about GAE...
There are other reasons to avoid GAE -- price, datastore latency, lack of flexibility -- but my main one is that GAE just doesn't apply to most of the things I use Flask for.
Both are great but I find them very slow.
Flask is a microframework. You can create a flask app contained entirely in one file, or on spanning several. It requires a minimum of configuration to work.
I'd like that misconception to finally be dispelled. In spite of being installed as an apparently single entity, Django is modular. You can use SQLAlchemy and Jinja2 with Django, and you can use Django's ORM all by itself, or its templating system standalone. You can use Django without manage.py, and while the only actual requirement is settings.py, that is in the process of being done with.
Flask is a microframework. It lets you build your website in a single file if that's what you need, or scale up to multiple files/sub-websites if your website is more complex. It doesn't have its own ORM, but it's very easy to use SQLAlchemy (the best ORM/SQL abstraction framework out there, IMHO). Its templating engine (Jinja2) is superior to Django's. On the other hand, the list of plugins available is smaller than Django's.
Both have very good documentation.