0
0
Flaskframework~30 mins

Blueprint creation and registration in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
Blueprint creation and registration
📖 Scenario: You are building a simple Flask web app that has two parts: a main page and a user section. To keep the code organized, you will use Flask Blueprints.
🎯 Goal: Create a Flask Blueprint for the user section and register it with the main Flask app. This will help you separate routes and keep your app clean.
📋 What You'll Learn
Create a Blueprint named user_bp with the import name __name__ and URL prefix /user
Define a route /profile inside the user_bp Blueprint that returns the text 'User Profile'
Create the main Flask app instance named app
Register the user_bp Blueprint with the main app app
💡 Why This Matters
🌍 Real World
Blueprints help organize large Flask apps by grouping related routes and code. This makes apps easier to maintain and scale.
💼 Career
Many companies use Flask Blueprints to build modular web apps. Knowing how to create and register Blueprints is a key skill for Flask developers.
Progress0 / 4 steps
1
Create the user Blueprint
Import Blueprint from flask and create a Blueprint called user_bp with the import name __name__ and URL prefix /user.
Flask
Need a hint?

Think of Blueprint as a mini app inside your main app. You give it a name, the current file's name, and a URL prefix.

2
Add a route to the user Blueprint
Inside the user_bp Blueprint, define a route for /profile that returns the string 'User Profile'.
Flask
Need a hint?

Use the @user_bp.route('/profile') decorator to add the route, then define a function that returns the text.

3
Create the main Flask app
Import Flask from flask and create the main app instance called app.
Flask
Need a hint?

The main app is created by calling Flask(__name__). Don't forget to import Flask.

4
Register the Blueprint with the app
Register the user_bp Blueprint with the main Flask app app using app.register_blueprint(user_bp).
Flask
Need a hint?

Use app.register_blueprint(user_bp) to connect the Blueprint to your main app.