
Building websites in Python with Flask - dabent
http://maximebf.com/blog/2012/10/building-websites-in-python-with-flask/#.UHc5pLTc3w4
======
3amOpsGuy
Great tutorial, a good level of detail too.

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.

~~~
davidjohnstone
Yes, the interactive debugger in Werkzeug[1] is fantastic! I couldn't tell you
how many times I've deliberately put "asdf.asdf" in a method to deliberately
throw an exception so that I can play around inside the debugger.

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.

2\. <http://packages.python.org/Flask-Login/>

~~~
eric_bullington
Stick this in your app and you can call the debugger however you wish by
customizing the function name, and it will only throw in debugger mode (in
case you forget it):

    
    
      from flask import current_app
    
      def asdf():
          assert current_app.debug == False, "You just called debug!"

------
whalesalad
I wish more people would contribute information on using Flask at a larger
scale. Everyone and their brother has written a basic tutorial outlining how
to get up and running with Flask. This is a GREAT post and I do not want to
demean it ... however I think that a lot of this can be gleaned from following
along with the docs and experimenting with Flask in general.

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.

~~~
chewxy
Having no structure enforced is also a good thing. It allows you to get into
and work on the project faster without having to set up all your boilerplates
that Django comes with.

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

------
joeshaw
A good tutorial, but it was missing any mention of blueprints, which are
essential to building a Flask site larger than a few endpoints. Blueprints
also allow you to reuse components across multiple apps, or as releasing a set
of endpoints as open source libraries that can be easily plugged into other
apps.

Edit: adding a link to Blueprints in the Flask docs:
<http://flask.pocoo.org/docs/blueprints/>

~~~
emixam
I'm the author of the article. I plan to write another full tutorial on
blueprints. I use them a lot but I already found this post long enough :)

~~~
joeshaw
Great, looking forward to it. I love Flask, and am working on a backend site
built on top of it (<http://woventheapp.com>) -- the more people we can get
knowledgeable about it, the better!

------
RegEx
Good read. I wrote a similar Flask introductory article[0] on how I moved my
Wordpress site/blog over to Flask. The source code of my site can be viewed on
github[1].

[0]: <http://vertstudios.com/blog/new-flask-site/> [1]:
<https://github.com/joequery/Vert-Flask>

------
mumphster
Great little tutorial but I'd also like to mention that if you're creating
websites using flask make sure to check out blueprints
(<http://flask.pocoo.org/docs/blueprints/>) it lets you modualize your website
and make it a lot more manageable once your code gets a little bit larger.

~~~
corford
Second that and would go as far as to say that if you're doing anything other
than a tiny tiny micro site, you should default to using them. They're easy to
use, come with no downside (that I'm aware of) and the modularity benefit is
huge.

------
welder
Great tutorial.

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...](https://github.com/alanhamlett/backbone/commit/91941afe693ae85bc5303b8e61982876cd5ae415)

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/>

------
jpadilla_
Flask is so damn awesome! For all those other projects Django seems too big, I
use Flask. The hardest thing for me was finding a project structure that
worked for me. I still haven't landed one that was "perfect". What project
structure/skeletons for Flask apps work for you?

~~~
davidjohnstone
Going big? Blueprints.

<http://flask.pocoo.org/docs/blueprints/>

Going small? Put everything in one folder.

app = Flask(__name__, static_folder='.', static_url_path='',
template_folder='')

------
corford
Flask is awesome and I'd strongly recommend it to anyone curious about doing
webdev in python.

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...](https://github.com/mitsuhiko/flask/tree/website/flask_website)

~~~
eric_bullington
Agreed. If you want to read some well-written Python, take a look at the Flask
source code. It is the embodiment of Pythonic.

------
lazydon
I just cannot thank you enough - you are a life saver. Coming from Java world
I was looking for exactly like this. Maybe I'm new to Python, I was just about
to give up. Setting Nginx/Apache for Flask was giving me nightmares -just
could not get it right to play with gunicorn, uwsgi and so on.

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.

~~~
lhnn
If you're experienced in Java, have you taken a look at the Play Framework?

<http://playframework.org>

~~~
lazydon
Thanks. I tried Scala but got a bit scared :-) I chose Clojure as the next
step from Java. But that doesn't exclude Scala by any means - it's just about
time that I play with it as I've heard it isn't that complex as people think
of it.

------
minikomi
Good stuff! Just a question, in the first session example, should the
url_for('say_hello') be url_for('message')? And in the message template,
'index' maybe should be 'home'.

~~~
emixam
Yeah I'm going to fix that. I published the article too early by mistake with
a few errors still left

~~~
minikomi
No problem! Thanks for writing this.

------
jrvarela56
I've always wondered why people who like using these python micro frameworks
don't use App Engine. Have any of you guys tried out GAE and decided it wasn't
worth it?

~~~
pjscott
I mostly use Flask to create handy status dashboards and debugging tools for
servers that do other things. It's a nice use pattern: you want to expose some
of the internals of a server over a firewalled-off port, so you whip up a few
HTTP endpoints in Flask, start an HTTP server in a background thread, and
declare victory. It's quick and easy, and can save huge amounts of time later
when you're trying to figure out why something isn't quite working.

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.

~~~
icebraining
Same here. I've used Flask for an internal "database cloner". It's
ridiculously easy to get it up and running.

------
d_luaz
For those working on GAE, try this handy template:
<https://github.com/kamalgill/flask-appengine-template>

------
codegeek
good read. I am working on something built in scratch using Flask and so far,
it has been a breeze in understanding the document, source code and samples
provided on the pocoo site.

------
cake
See also bottle <http://bottlepy.org/docs/dev/>

Both are great but I find them very slow.

~~~
wheaties
You can't just use Flask straight out of the box. You need to put it on
something. I've used Gunicorn with Gevent and it runs as fast as I need it to
run. Gevent with Nginx as a reverse proxy play very well together and the
responsiveness is more about how you're managing your DB queries rather than
rendering the site. (I shy away from ORMs for this reason but still use
sqlalchemy.)

------
bcambel
what an amazing tutorial!

------
countessa
Nicely done. I'm a Ruby/Sintra bod myself, but now I'm keen to play with Flask
a bit - thanks!

------
nyddle
Thanks for the tutorial. Can't wait for the next part about blueprints!

------
liamchzh
what's the difference between Flask and Django?

~~~
TazeTSchnitzel
Django's a huge, monolithic framework that you need to run a script for that
generates some files and a folder strucutre.

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.

~~~
lloeki
> Django's a huge, monolithic framework

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.

------
d0m
Good tutorial. You should check out Django. It comes with most of the script
you have written plus a lot more and a really active community.

~~~
chrisballinger
I learned on Django but have turned to Flask for my smaller projects after
wrestling too many times with Django's poor handling of static files during
development.

------
paulsutter
Any opinions about Flask vs Bottle?

~~~
mmariani
Watch this talk by Armin. <http://pyvideo.org/video/440/pycon-2011--opening-
the-flask>

------
chuppo
So you took flask documentation, some other sources and made it into a blog
post about it?

~~~
thmcmahon
Why is that a problem?

