Building Scalable RESTful APIs with Flask: Custom Redprint Implementation

Custom redprints extend Flask's blueprint functionality by adding URL prefix capabilities. While blueprints provide a foundation, redprints introduce an additional url_prefix layer. For instance, if a blueprint has url_prefix='v1' and a redprint has url_prefix='book', the resulting URL becomes:

127.0.0.1:5000/v1/book

The project structure is organized as follows:

First, create the root directory 'ginger' and a 'libs' subdirectory for common utilities. The custom redprint implementation is stored in ginger/libs/redprint.py:

class CustomRedprint:
    def __init__(self, name):
        self.name = name
        self.route_collection = []

    def add_route(self, rule, **options):
        def decorator(func):
            self.route_collection.append((func, rule, options))
            return func
        return decorator

    def register_with_blueprint(self, blueprint, url_prefix=None):
        if url_prefix is None:
            url_prefix = '/' + self.name
        for func, rule, options in self.route_collection:
            endpoint_name = self.name + '+' + \
                          options.pop("endpoint", func.__name__)
            blueprint.add_url_rule(url_prefix + rule, endpoint_name, func, **options)

ginger/libs/redprint.py Since the redprint doesn't define add_url_rule directly, we store the necesary parameters during route registration and apply them when registering with a blueprint.

Next, create the 'app' package in the root directory, with a 'api' subpackage for interface definitions. The 'v1' subpackage will contain version 1 API endpoints:

"""
 Created by Developer on 2023/10/15.
"""
from app.libs.redprint import CustomRedprint
from flask import jsonify

__author__ = 'Developer'

book_api = CustomRedprint('book')


@book_api.add_route('/search')
def search_books():
    book_data = {'title': "Advanced Flask Patterns"}
    return jsonify(book_data)

app/api/v1/book.py``` """ Created by Developer on 2023/10/15. """ from flask import jsonify

from app.libs.redprint import CustomRedprint

user_api = CustomRedprint('user')

@user_api.add_route('', methods=['GET']) def retrieve_user(): user_info = {"username":"developer"}

return jsonify(user_info)


app/api/v1/user.py```
from flask import Blueprint
from app.api.v1 import user, book



def generate_v1_blueprint():
    v1_bp = Blueprint('api_v1', __name__)

    user_api.register_with_blueprint(v1_bp)
    book_api.register_with_blueprint(v1_bp)
    return v1_bp

app/api/v1/init.py``` """ Created by Developer on 2023/10/15. """

author = 'Developer'


app/api/__init__.py```
from flask import Flask

application = Flask(__name__)


def configure_blueprints(app):
    from app.api.v1 import generate_v1_blueprint
    app.register_blueprint(generate_v1_blueprint(), url_prefix='/v1')


def initialize_app():
    app.config.from_object('app.config.settings')
    app.config.from_object('app.config.security')
    configure_blueprints(app)

    return app

app/init_.pyCreate a 'config' directory in the 'app' package for configuration files:

"""
 Created by Developer on 2023/10/14.
"""

app/config/security.py``` """ Created by Developer on 2023/10/14. """


app/config/settings.pyFinally, create the startup file in the ginger directory:


from app import initialize_app

application = initialize_app()

if name == 'main': application.run(debug=True)



Tags: Flask RESTful API python web development Blueprint

Posted on Thu, 27 Aug 2026 16:11:04 +0000 by stephfox