Blueprints in Flask


Introduction

As Flask applications grow, managing a large codebase can become challenging. Blueprints provide a way to organize and modularize routes, templates, and static files into smaller, reusable components.

Step 1: Setting Up Flask

Start by installing Flask:

            pip install flask
        

Create the main Flask application file:

            from flask import Flask

            app = Flask(__name__)

            @app.route('/')
            def home():
                return 'Welcome to the Main App!'

            if __name__ == '__main__':
                app.run(debug=True)
        

Step 2: Creating a Blueprint

Create a new folder for your blueprint, for example, auth. Inside this folder, create a Python file named routes.py:

            from flask import Blueprint

            auth = Blueprint('auth', __name__)

            @auth.route('/login')
            def login():
                return 'Login Page'

            @auth.route('/logout')
            def logout():
                return 'Logout Page'

            @auth.route('/signup')
            def signup():
                return 'Signup Page'
        

Here, we define an auth blueprint to handle authentication-related routes.

Step 3: Registering the Blueprint

Register the blueprint in your main application:

            from flask import Flask
            from auth.routes import auth

            app = Flask(__name__)

            # Register the auth blueprint
            app.register_blueprint(auth, url_prefix='/auth')

            @app.route('/')
            def home():
                return 'Welcome to the Main App!'

            if __name__ == '__main__':
                app.run(debug=True)
        

The url_prefix='/auth' ensures that all routes in the auth blueprint are prefixed with /auth.

Step 4: Organizing Templates and Static Files

Within the auth folder, create subfolders for templates and static files:

            auth/
            ├── routes.py
            ├── templates/
            │   └── auth/
            │       └── login.html
            └── static/
                └── auth/
                    └── style.css
        

Update the routes.py to render templates:

            from flask import Blueprint, render_template

            auth = Blueprint('auth', __name__, template_folder='templates', static_folder='static')

            @auth.route('/login')
            def login():
                return render_template('auth/login.html')
        

Step 5: Adding Static Files

Create a CSS file in the static/auth folder. For example, style.css:

            body {
                font-family: Arial, sans-serif;
            }
        

Link the static file in your template:

            <!DOCTYPE html>
            <html>
            <head>
                <title>Login</title>
                <link rel="stylesheet" type="text/css" href="{{ url_for('auth.static', filename='auth/style.css') }}">
            </head>
            <body>
                <h1>Login Page</h1>
            </body>
            </html>
        

Step 6: Running the Application

Run your application and visit /auth/login to see the login page.

            python main.py
        

Conclusion

Using Blueprints in Flask helps modularize your application, making it easier to maintain and scale. By separating routes, templates, and static files into specific blueprints, you can manage your codebase more effectively.





Advertisement