15 Useful Flask Extensions and Libraries That I Use in Every Project

blog/cards/15-useful-flask-extensions-and-libraries-that-i-use-in-every-project.jpg

Part of the benefit of using a popular web framework is the thriving community around it. Here's my favorite Flask extensions.

Quick Jump: gunicorn | flask-debugtoolbar | pytest & pyest-cov | flake8 | Flask-SQLAlchemy & psycopg2 | alembic | celery & redis | Flask-WTF & WTForms-Components | Flask-Login | Flask-Limiter | Flask-Mail | What Are Your Favorite Flask Libraries?

When it comes to creating a new project, all of the extensions and libraries listed below end up in my requirements.txt file. They are awesome, currently well maintained and active.

I find lists like this really handy because when it comes to learning a new web framework, one of the most time consuming parts is figuring out the ecosystem, which really means investigating a bunch of packages to figure out which ones are worth using it.

I made this list to save you the time of having to do that. You can’t go wrong by using any of the libraries listed below.

gunicorn

This library is one of the most popular WSGI based servers for Python. It was a port from the popular Unicorn server that exists in Ruby.

What I really like about this app server is that it’s super easy to configure and get going, but it has enough config options to tweak things when needed.

If you’re wondering “but is it efficient?”, the answer is yes! It’s currently being used by Instagram. They could have picked uwsgi or Apache / mod_wsgi but they didn’t.

I personally use it for both personal and larger scale deployments. I’m really happy with it.

flask-debugtoolbar

This extension is near the top of my list.

All you do is add it to your Flask app and it gives you a really nice toolbar that shows you HTTP headers, request variables, config settings, a break down of template renderings, SQLAlchemy queries and more.

You can even add your own custom output too. Having these insights about your application can really help you optimize performance, and as the name suggests debug your code.

pytest & pyest-cov

Both pytest and pytest-cov have your back when it comes to testing your Flask applications.

Pytest gives you a great interface for running unit and integration tests. You can set up fixtures for your data too. By default, it gives you a lot of useful output from writing tests with very little boilerplate. It’s also easy to configure to take advantage of Flask’s app factory pattern.

Pytest-cov is an add-on that gives you a read-out of your test coverage. I don’t live by test coverage percents but it’s nice to see at a glance at what bits and pieces of your code base have or are missing test coverage.

flake8

Being able to analyze your code base for styling consistency and mistakes are very beneficial and that’s what flake8 does.

All you do is run the command against your code base and it will throw warnings and errors your way when things aren’t consistent or you have a syntax error.

I think it’s essential for open source projects because it keeps everyone on the same page for style guides and it’s something you can run as part of your test routines.

Flask-SQLAlchemy & psycopg2

When it comes to relational databases my weapon of choice is PostgreSQL. psycopg2 lets Python communicate with PostgreSQL and Flask-SQLALchemy uses SQLAlchemy under the hood to provide you an ORM.

I’m a big fan of using an ORM for relational databases because they make it a lot friendlier to interact with a database using your programming language’s constructs instead of raw SQL.

At the same time, SQLAlchemy is flexible enough to where you can write raw SQL when you need it, so it’s a win-win. SQLAlchemy is the most popular ORM for Python.

alembic

At some point you’ll likely want to migrate your database, and alembic is the recommended library for doing this if you use SQLAlchemy.

It was written by the same team who created SQLAlchemy. I personally don’t use things like Flask-Migrate because I find alembic very natural to work with and you often end up writing your own CLI tools for your apps anyways.

Setting up a simple DB migrate script with alembic and Click is only a few lines of code, and I much prefer writing a few lines of my own own than bring in an external dependency. Especially for something as important as database migrations.

celery & redis

When you need to execute background jobs, there’s very little reason to use anything besides celery backed by redis.

Celery has everything you need for running 1 off tasks or scheduled tasks. It’s also super battle hardened. It’s such a good library for managing jobs that it might be worth using Python on your next job-heavy app just for Celery.

Redis is just a key / value store that you can use to back your Celery jobs. I like to use Redis instead of RabbitMQ because I often use Redis for other things such as a cache, and 1 Redis instance can easily handle doing both things for most use cases.

Flask-WTF & WTForms-Components

I’m sure you’ve built at least one app that needs to process forms. A goto set of libraries for that are both Flask-WTF and WTForms-Component.

Flask-WTF (which uses WTForms internally) is the main one. I really dig their style of form creation because you are free to style up the forms in HTML however you want, but when it comes to defining the form, you do it in Python code.

This is where you can set up things like which fields belong to which form, set up validation rules and additional logic related to processing a form.

WTForms-Components is just an add-on that provides a few common field types, validations and widgets that don’t come with WTForms by default.

Some of these include TimeField, SelectMultipleField and being able to do a database backed Uniqueness check on a field. Imagine having a username field where you wanted to ensure no duplicates exist. WTForms-Components is perfect for that.

Flask-Login

95% of the applications I develop end up with a user system of some sort and I really really like Flask-Login. It is the perfect balance between doing 1 thing super well and being flexible.

You have free reign to create a user flow that works best for your application, but it takes care of the scary parts of authentication such as session management and dealing with cookies.

I much prefer this over something like Flask-Security because for most real world applications, the user registration and login flow doesn’t fit in a pre-defined mold and overriding its functionality is often more work than writing a few lines of your own code.

Flask-Limiter

Every once in a while you come across a little gem such as Flask-Limiter. This is the perfect library for controlling traffic rates. You can do rate limiting by IP address or other rules.

For example if you ever wanted to do something like “Only let users visit this page once per minute, but let admins always visit it”, then this is the library for you. This could be used to protect API endpoints as well.

One nice thing about this is you can use Redis as a back-end to store the rate limit information so lookups and writes are lightning fast. This comes built-in with the extension.

Flask-Mail

Sending email is important for a lot of application types, especially if you’re dealing with users. Flask-Mail handles sending SMTP based email.

All you have to do is configure your mail server’s login credentials and a few other things such as TLS and you’re good to go.

One nice thing about this extension is that it has a debug mode which lets you test sending emails without actually sending them out. It also deals with attachments too.

What Are Your Favorite Flask Libraries?

So that’s my list of frequently used Flask extensions and libraries.

Let me know your favorite ones in the comments below.

By the way, if you’re looking to learn Flask in great detail by building a real world application, I do have a self paced 10 hour video course that goes over using all of the these libraries (and more) to build a SAAS application that handles accepting recurring payments with Stripe.

Never Miss a Tip, Trick or Tutorial

Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per month (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.


Comments