Flask Useful Extensions - DebugToolbar, Cache, Rate Limiter 
Tags: webdev, flask, opensource
Published: 2022-01-06 16:12:45

---
title: Flask Useful Extensions - DebugToolbar, Cache, Rate Limiter 
published: true
description: A curated list with Flask libraries and extensions we can use to debug or increase the quality of our projects (all free).
tags: webdev, flask, opensource
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ik6c0yg440i9eivl4w03.jpg
canonical_url: https://blog.appseed.us/flask-extensions-debug-toolbar-minify-limiter/
---
Hello coders!

This article presents a short-list with [Flask Libraries and Extensions](https://blog.appseed.us/flask-extensions-debug-toolbar-minify-limiter/) 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. 

---

> Thanks for reading! Content provided by **[App Generator](https://appseed.us/)**

- 👉 **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

--- 

## ✨ 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

```bash
$ pip install flask-debugtoolbar
```

---

> 👉 Step #2 - Usage 

```python
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)
```

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.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/62xsi80vhtngrwk7293i.gif)

--- 

## ✨ 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

```bash
$ pip install flask-marshmallow
```

---

> 👉 Step #2 - Usage in Code 

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

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

**Define a model**

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

class User(Model):
    email = Column(String)
    password = Column(String) 
```

Define your output format with marshmallow.

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

user_schema  = UserSchema()
users_schema = UserSchema(many=True)
```

**Define API nodes**

```python
@app.route("/api/users/")
def users():
    all_users = User.all()
    return users_schema.dump(all_users)


@app.route("/api/users/<id>")
def user_detail(id):
    user = User.get(id)
    return user_schema.dump(user)
```

--- 

## ✨ 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

```bash
$ pip install Flask-Cache
```

---

> 👉 Step #2 - Usage

```python
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'})
```

**Cache views** (via a decorator)

```python
@cache.cached(timeout=50)
def index():
    return render_template('index.html')
```

---


## ✨ 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

```bash
$ pip install Flask-Minify
```

---

> 👉 Step #2 - Usage in code

```python
from flask import Flask
from flask_minify import Minify

app = Flask(__name__)
Minify(app=app, html=True, js=True, cssless=True)
```

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

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

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

@app.route('/')
@minify_decorators.minify(html=True, js=True, cssless=True)
def example():
  return '<h1>Example...</h1>'
```

---

## ✨ 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

```bash
$ pip install Flask-Limiter
```

---

> 👉 Step #2 - Usage in code

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

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

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

@app.route("/fast")
def fast():
    return ":)"

@app.route("/ping")
@limiter.exempt
def ping():
    return "PONG"
```

---

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

---

- 👉 Download [Flask Apps](https://appseed.us/apps/flask-apps) generated by `AppSeed`
- 👉 Get [support](https://appseed.us/support) via email and [Discord](https://discord.gg/fZC6hup) (1k+ community)