13 ms·
Building websites in Python with Flask
- liamchzh 14y agowhat's the difference between Flask and Django?
- TazeTSchnitzel 14y agoDjango'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 14y ago> 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.
- mercurial 14y agoDjango follows Python's philosphy of "batteries included". In particular, it uses its own ORM. It also has a large ecosystem of pluggable apps, whether contrib modules or outside the Django tree. 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.
- codegeek 14y agogood 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.
- bcambel 14y agowhat an amazing tutorial!
- jrvarela56 14y agoI'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?
- 3amOpsGuy 14y agoGAE's working very well for me, i use flask on GAE. 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%.
- djtidau 14y agoI was a big user of appengine in the last couple of years but have slowly moved off for cost reasons. It is a great service getting started, and scaled amazingly, but as the userbase grew, the cost became too much. There are certainly ways to mitigate it but for me it was not worth the extra time investment when it was mostly my side projects causing the problems.
- X-Istence 14y agoOne thing I personally realised is that App Engine is absolutely fantastic for my needs, but as soon as my sites are outside of the free quota the costs rise extremely fast almost putting them out of price range before they even got started. 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.
- hoka 14y agoI'm definitely toward the novice end of the spectrum, but GAE scares me with how tightly coupled you can get to it, especially if you use libraries they provide. 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.
- lhnn 14y ago
- 3amOpsGuy 14y agoGreat 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/ http://lucumr.pocoo.org/talks/ Bucket list 2013, attend a talk by Armin Ronacher.
- davidjohnstone 14y agoYes, 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/ http://packages.python.org/Flask-Login/
- eric_bullington 14y agoStick 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!"
- taw9 14y agoArmin is da bomb.
- mumphster 14y agoGreat 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/ 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 14y agoSecond 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 14y agoGreat tutorial. Since you're already using backbone.js why not plug in CSRF protection with Flask-SeaSurf? (http://flask.pocoo.org/extensions/ http://flask.pocoo.org/extensions/) If you use this backbone.js change then CSRF is practically invisible: https://github.com/alanhamlett/backbone/commit/91941afe693ae85bc5303b8e61982876cd5ae415 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/
- minikomi 14y agoGood 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'.
- RegEx 14y agoGood 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/ http://vertstudios.com/blog/new-flask-site/ [1]: https://github.com/joequery/Vert-Flask https://github.com/joequery/Vert-Flask
- jpadilla_ 14y agoFlask 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?
- emixam 14y agoI'm using blueprints too which are great to organize your app as modules. I plan to write another tutorial on that
- davidjohnstone 14y agoGoing big? Blueprints. 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='')
- d0m 14y agoGood 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 14y agoI 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.
- joeshaw 14y agoA 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/ http://flask.pocoo.org/docs/blueprints/
- emixam 14y agoI'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 14y agoGreat, looking forward to it. I love Flask, and am working on a backend site built on top of it (http://woventheapp.com http://woventheapp.com) -- the more people we can get knowledgeable about it, the better!
- lazydon 14y agoI 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 14y agoIf you're experienced in Java, have you taken a look at the Play Framework? http://playframework.org http://playframework.org
- lazydon 14y agoThanks. 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.
- taw9 14y agoI absolutely loved Flask... then I met Node.js. Python is great, don't get me wrong (7 years, 5 of which as a paid Python/C++ developer) but production tasks are better served by faster/more resource efficient frameworks. The biggest mistake I made was getting too attached to a single language. Now I use Node, Go, Lua... I used to argue that Python was what everyone should learn with, but I'm not sure that is true anymore.
- countessa 14y agoNicely done. I'm a Ruby/Sintra bod myself, but now I'm keen to play with Flask a bit - thanks!
- corford 14y agoFlask 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_website https://github.com/mitsuhiko/flask/tree/website/flask_websit...
- eric_bullington 14y agoAgreed. If you want to read some well-written Python, take a look at the Flask source code. It is the embodiment of Pythonic.
- chuppo 14y agoSo you took flask documentation, some other sources and made it into a blog post about it?
- thmcmahon 14y agoWhy is that a problem?
- cake 14y agoSee also bottle http://bottlepy.org/docs/dev/ http://bottlepy.org/docs/dev/ Both are great but I find them very slow.
- kfk 14y agoCould you explain?
- dignan 14y agoSlow relative to what? Based on what?
- wheaties 14y agoYou 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.)
- d_luaz 14y agoFor those working on GAE, try this handy template: https://github.com/kamalgill/flask-appengine-template https://github.com/kamalgill/flask-appengine-template
- paulsutter 14y agoAny opinions about Flask vs Bottle?
- mmariani 14y agoWatch this talk by Armin. http://pyvideo.org/video/440/pycon-2011--opening-the-flask http://pyvideo.org/video/440/pycon-2011--opening-the-flask
- nyddle 14y agoThanks for the tutorial. Can't wait for the next part about blueprints!
- whalesalad 14y agoI 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 14y agoHaving 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
- ghc 14y agoThe trouble is, larger apps are logical extensions of smaller apps in Flask. I find it rather pleasant to use Flask to build large RESTful APIs, but I don't find there to be any interesting challenges or insights that would make it worthwhile to produce an article relating the experience. I'm afraid it's the same way for many people I know who are using Flask to build large APIs at their companies - the process is so predictably boring that I doubt any of them have even considered writing about it.
- kenneth_reitz 14y agohttps://speakerdeck.com/u/kennethreitz/p/flasky-goodness https://speakerdeck.com/u/kennethreitz/p/flasky-goodness