Flask is a Python web framework built with a small core and easy-to-extend philosophy.
Flask is considered more Pythonic than Django because Flask web application code is in most cases more explicit. Flask is easy to get started with as a beginner because there is little boilerplate code for getting a simple app up and running.
For example, here's a valid "hello world" web application with Flask (the equivalent in Django would be significantly more code):
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
Flask was also written several years after Django and therefore learned from the Python community's reactions as the framework evolved. Jökull Sólberg wrote a great piece articulating to this effect in his experience switching between Flask and Django.
The Flask mega tutorial by Miguel Grinberg is a perfect starting resource for using this web framework. Each post focuses on a single topic and builds on previous posts. The series includes 18 parts: #1 Hello World, #2 Templates, #3 Web Forms, #4 Database, #5 User Logins, #6 Profile Page and Avatars, #7 Unit Testing, #8 Followers, Contacts, and Friends, #9 Pagination, #10 Full Text Search, #11 Email Support, #12 Facelift, #13 Dates and Times, #14 I18n and L10n, #15 Ajax, #16 Debugging, Testing and Profiling, #17 Deployment on Linux and #18 Deployment on the Heroku Cloud. Miguel also wrote the O'Reilly Flask Web Development book which is also an excellent learning resource.
If you're looking for a fun tutorial with Flask and WebSockets, check out my blog post on creating Choose Your Own Adventure Presentations with Reveal.js, Python and WebSockets. Follow up that tutorial by building an admin interface in part 1, part 2 and part 3 that'll show you how to use forms and SQLAlchemy. There is also a companion open source GitHub repository for the app with tags for each step in the blog posts.
This simple Flask app uses Twilio Voice to do voice calling with three participants. It's a fun introduction to Python and Flask I wrote for the Twilio blog.
The Flask Extensions Registry is a curated list of the best packages that extend Flask. It's the first location to look through when you're wondering how to do something that's not in the core framework.
Explore Flask is a public domain book that was previously backed on Kickstarter and cost money for about a year before being open sourced. The book explains best practices and patterns for building Flask apps.
How I Structure My Flask Application walks through how this developer organizes the components and architecture for his Flask applications.
Adding phone calling to your web application is a killer Flask tutorial with all the code needed to create a web app that can dial phones and receive inbound calls.
Nice post by Jeff Knupp on Productionizing a Flask App.
The Plank & Whittle blog has two posts, one on Packaging a Flask web app and another on Packaging a Flask app in a Debian package once you've built an app and want to deploy it.
The Tuts+ Flask tutorial is another great walkthrough for getting started with the framework.
Create Your Own Obnoxiously Simple Messaging App Just Like Yo is a silly walkthrough of very basic Flask web application that uses Nitrous.io to get started and Twilio for SMS.
The blog post series "Things which aren't magic" covers how Flask's ubiquitous @app.route decorator works under the covers. There are two parts in the series, part 1 and part 2.
Flask by Example: Part 1 shows the basic first steps for setting up a Flask project. Part 2 explains how to use PostgreSQL, SQLAlchemy and Alembic. Part 3 describes text processing with BeautifulSoup and NLTK. Part 4 shows how to build a task queue with Flask and Redis.
Branded MMS Coupon Generation with Python and Twilio is a Flask tutorial I wrote for building a web application that can send branded barcode coupons via MMS. The post goes through every step from a blank directory until you have a working app that you can deploy to Heroku.
How to Structure Large Flask Applications covers a subject that comes up quickly once you begin adding significant functionality to your Flask application.
Flask Blueprint templates
shows a way of structuring your __init__.py
file with
blueprints for large
projects.
Video streaming with Flask is another fantastic tutorial by Miguel Grinberg that covers video streaming.
One line of code cut our Flask page load times by 60% is an important note about optimizing Flask template cache size to dramatically increase performance in some cases.
Unit Testing Your Twilio App Using Python’s Flask and Nose covers integrating the Twilio API into a Flask application and how to test that functionality with nose.
The Flask documentation has some quick examples for how to deploy Flask with standalone WSGI containers.
Handling Email Confirmation in Flask is a great walkthrough for a common use case of ensuring an email address matches with the user's login information.
If you're not sure why DEBUG
should be set to False
in a production
deployment, be sure to read this article on
how Patreon got hacked.
Choose Your Own Adventure Presentations combines Flask with Reveal.js and text messages to create presentations where the audience can vote on how the story should proceed. The code is all open source under an MIT license and also uses the Flask-SocketIO and Flask-WTF projects to support voting and form input.
Skylines is an open source flight tracking web application built with Flask. You can check out a running version of the application.
Microblog is the companion open source project that goes along with Miguel Grinberg's O'Reilly Flask book.
Flaskr TDD takes the official Flask tutorial and adds test driven development and JQuery to the project.
Here is a note-taking app along with the source code in Gists.
Bean Counter is an open source Flask app for tracking coffee.
FlaskBB is a Flask app for a discussion forum.
psdash is an app built with Flask and psutils to display information about the computer it is running on.
Use the Flask App Engine Template for getting set up on Google App Engine with Flask.
Flask Foundation is a starting point for new Flask projects. There's also a companion website for the project that explains what extensions the base project includes.
Cookiecutter Flask is a project template for use with Cookiecutter.
Flask-Boilerplate provides another starting project with sign up, log in and password reset.
Install Flask on your local development machine.
Work through the 18-part Flask tutorial listed first under "Flask resources" above.
Read through Flask Extensions Registry to find out what extensions you'll need to build your project.
Start coding your Flask app based on what you learned from the 18 part Flask tutorial plus open source example applications found below.
Move on to the deployment section to get your initial Flask project on the web.
If you're searching for step-by-step Flask tutorials then I highly recommend checking out Real Python.