Flask Useful Extensions

A curated list with Flask libraries and extensions we can use to debug or increase the quality of our projects (all free).

Flask - Official Banner
Flask - Official Banner

Hello! This article presents a short-list with Flask Libraries and Extensions we can use to make our projects faster or stable. For newcomers, Flask is a leading framework powered by Python that we can use to code simple sites, APIs, or complex eCommerce solutions.

  • 👉 Flask-DebugToolbar - useful in the development phase
  • 👉 Flask-Marshmallow - for API development
  • 👉 Flask-Cache - for blazing-fast websites
  • 👉 Flask-Minify - HTML & assets compression
  • 👉 Flask-Limiter - rate limiter for Flask

All mentioned libraries are open-source and actively supported. Thanks for reading!

✨ Flask-DebugToolbar

This extension adds a toolbar overlay to Flask applications containing useful information for debugging. Once this module is installed we should be able to see in the browser things like HTTP headers, configuration, or even profiling information.

👉 Step #1 - Installation

$ pip install flask-debugtoolbar
Flask Debug Toolbar - Installation

👉 Step #2 - Usage

from flask import Flask
from flask_debugtoolbar import DebugToolbarExtension

app = Flask(__name__)

# the toolbar is only enabled in debug mode:
app.debug = True

# Inject the toolbar 
toolbar = DebugToolbarExtension(app)
Flask Debug Toolbar - Usage in Code

The toolbar will automatically be injected into HTML responses when debug mode is on. In production, setting app.debug = False will disable the toolbar.

Flask Debug Toolbar.
Flask Debug Toolbar

✨ Flask Marshmallow

Flask-Marshmallow is a thin integration layer for Flask (a Python web framework) and marshmallow (an object serialization/deserialization library) that optionally integrates with Flask-SQLAlchemy.

👉 Step #1 - Installation

$ pip install flask-marshmallow
Flask-Marshmallow - Installation

👉 Step #2 - Usage in Code

from flask import Flask
from flask_marshmallow import Marshmallow  # <-- NEW

app = Flask(__name__)
ma = Marshmallow(app)                      # <-- NEW  
Flask-Marshmallow - App Injection

Define a model

from Flask-SqlAchemy import Model, Column, Integer, String, DateTime

class User(Model):
    email = Column(String)
    password = Column(String) 
Flask-Marshmallow - Define Model

Define your output format with marshmallow.

class UserSchema(ma.Schema):
    class Meta:
        # Fields to expose
        fields = ("email")

user_schema  = UserSchema()
users_schema = UserSchema(many=True)
Flask-Marshmallow - Define Output Format

Define API nodes

def users():
    all_users = User.all()
    return users_schema.dump(all_users)

def user_detail(id):
    user = User.get(id)
    return user_schema.dump(user)
Flask-Marshmallow - API Nodes

✨ Flask Cache

This extension might help us to reduce the performance issues that sometimes occur in our projects and also speed up the response for static pages.  

👉 Step #1 - Installation

$ pip install Flask-Cache
Flask Cache - Installation

👉 Step #2 - Usage

from flask import Flask
from flask.ext.cache import Cache

app = Flask(__name__)
# Check Configuring Flask-Cache section for more details
cache = Cache(app,config={'CACHE_TYPE': 'simple'})
Flask-Cache - Library Injection

Cache views (via a decorator)

def index():
    return render_template('index.html')
Flask-Cache - View Implementation

✨ Flask Minify

This extension provides a runtime compression of served pages and optionally to assets (JSS, CSS). This optimization might improve the SEO score of our projects and also the overall experience for our users.

👉 Step #1 - Installation

$ pip install Flask-Minify
Flask-Minify - Installation

👉 Step #2 - Usage in code

from flask import Flask
from flask_minify import Minify

app = Flask(__name__)
Minify(app=app, html=True, js=True, cssless=True)
Flask-Minify - Applied to all pages

The extension effect can be isolated to a single view using the passive flag and decorators:

from flask import Flask
from flask_minify import Minify, decorators as minify_decorators

app = Flask(__name__)
Minify(app=app, passive=True)   # <-- Passive mode enabled

@minify_decorators.minify(html=True, js=True, cssless=True)
def example():
  return '<h1>Example...</h1>'
Flask-Minify - Passive Mode

✨ Flask Limiter

Flask-Limiter provides rate limiting features to Flask applications. Flask-Limiter can be configured to persist the rate limit state to many commonly used storage backends via the limits library.

👉 Step #1 - Installation

$ pip install Flask-Limiter
Flask Limiter - Installation

👉 Step #2 - Usage in code

from flask import Flask
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address

app = Flask(__name__)
limiter = Limiter(
    default_limits=["200 per day", "50 per hour"]
@limiter.limit("1 per day")
def slow():
    return ":("

@limiter.limit("1/second", override_defaults=False)
def medium():
    return ":|"

def fast():
    return ":)"

def ping():
    return "PONG"
Flask Limiter - Usage in Code

Thanks for reading! For more resources and support, feel free to access: