Flask Blueprints Sinhala: විශාල Flask Apps හදන හැටි | Python Web Development

කොහොමද යාලුවනේ! ✋ අද අපි කතා කරන්න යන්නේ Python Web Development වලදී Flask Framework එකත් එක්ක වැඩ කරන ඔයාලට ගොඩක් වැදගත් වෙන concept එකක් ගැන – ඒ තමයි Flask Blueprints.
ඔයාලා Flask වලින් පොඩි පොඩි Web Apps හදලා ඇතිනේ? සාමාන්යයෙන් මුලින්ම Flask ඉගෙන ගන්නකොට අපි හැමෝම වගේ කරන්නේ මුළු App එකම එකම Python file එකක ලියන එක. පොඩි Project එකකට ඒක අවුලක් නැහැ, වැඩේ ලේසියි. හැබැයි, ඔයාගේ App එක ටිකෙන් ටික ලොකු වෙන්න ගත්තම, ඒ කියන්නේ Pages වැඩි වෙනකොට, User Authentication, Admin Panels, API endpoints වගේ එක එක features එකතු වෙනකොට, ඒ එකම Python file එක බැලූ බැල්මටම අවුල් ජාලාවක් වගේ වෙන්න පුළුවන්. ඒක Manage කරන්න අමාරුයි, අලුත් feature එකක් එකතු කරන්න ගියාම පැරණි ඒවා කැඩෙන්න පුළුවන්, Team එකක් එක්ක වැඩ කරනකොට නම් ඒක තනිකරම nightmare එකක්!
ඔන්න ඔය වගේ වෙලාවට තමයි Flask Blueprints අපේ පිහිටට එන්නේ. මේවා හරියට අපේ App එකේ තියෙන ලොකු ලොකු Features වලට වෙන වෙනම කොටස් වෙන් කරනවා වගේ වැඩක්. මේ Tutorial එකෙන් අපි මේ Blueprints කියන්නේ මොනවද, ඇයි මේවා පාවිච්චි කරන්න ඕනේ, කොහොමද ඒවා Implement කරන්නේ, ඒ වගේම මේවා පාවිච්චි කරනකොට එන පොදු ගැටළු වලට විසඳුම් මොනවද කියලා පැහැදිලිව ඉගෙන ගමු. එහෙනම්, අපි පටන් ගමු!
Blueprints කියන්නේ මොනවද? (What are Blueprints?)
සරලවම කිව්වොත්, Blueprint එකක් කියන්නේ Flask App එකක තියෙන functionality set එකක්, නැත්නම් features සමූහයක්, වෙනමම මොඩියුලයක් විදියට organize කරගන්න පුළුවන් ක්රමයක්. හරියට ලොකු කම්පැණියක එක එක Departments තියෙනවා වගේ. උදාහරණයක් විදියට, කම්පැණියක Sales Department එකක්, Marketing Department එකක්, HR Department එකක් තියෙනවා වගේ, අපේ Flask App එකක Authentication (Login, Register), User Profiles, Blog Posts, Admin Panel වගේ වෙන වෙනම Features වලට වෙන වෙනම Blueprints හදන්න පුළුවන්.
මෙහෙම වෙනමම කොටස් වලට කඩන එකෙන් අපිට ලැබෙන වාසි ගොඩක් තියෙනවා:
- Modularity (මොඩියුලර් බව): අපේ App එකේ Code එක එක එක කොටස් වලට කඩලා තියෙන නිසා, එක කොටසක වෙනසක් කරනකොට අනිත් කොටස් වලට බලපාන එක අඩු වෙනවා.
- Maintainability (නඩත්තු කිරීමේ පහසුව): Code එක ගොඩක් පැහැදිලියි, ඕනෑම කෙනෙක්ට තේරුම් ගන්න ලේසියි. ඒ නිසා App එකේ දෝෂ හොයාගන්න එක සහ ඒවා නිවැරදි කරන එක පහසු වෙනවා.
- Scalability (පුළුල් කිරීමේ හැකියාව): අනාගතයේදී අලුත් Features එකතු කරන එක හෝ පවතින ඒවා වෙනස් කරන එක ගොඩක් ලේසියි. App එක විශාල වෙන තරමට මේකේ වටිනාකම තේරෙයි.
- Reusability (නැවත භාවිතය): ඔයා හදන Blueprint එකක් වෙනත් Flask Projects වලත් නැවත පාවිච්චි කරන්න පුළුවන්.
- Team Collaboration (කණ්ඩායම් වැඩ): Team එකේ කට්ටියට එකම file එකක ගැටෙන්නේ නැතුව, වෙන වෙනම Blueprint files වල වැඩ කරන්න පුළුවන්.
සරලවම කිව්වොත්, Blueprint එකක් කියන්නේ, අපිට Routes, Templates, Static files, Forms වගේ දේවල් ඒ අදාල Feature එකටම අයිති වෙන විදියට organize කරගන්න පුළුවන් "miniature application" එකක් වගේ දෙයක්. මේවා අපි අපේ main Flask application instance එකට "register" කරනවා.
Blueprints භාවිතා කරලා App එකක් හදමු (Building an App with Blueprints)
හරි, දැන් අපි බලමු මේ Blueprints practical විදියට කොහොමද භාවිතා කරන්නේ කියලා. අපි පොඩි Flask Project එකක් හදමු, ඒකේ Authentication (Login, Register) සහ Posts (බ්ලොග් පොස්ට් වගේ) කියලා ප්රධාන කොටස් දෙකක් තියෙනවා කියලා හිතමු. මේවාට අපි වෙන වෙනම Blueprints භාවිතා කරනවා.
Project Structure
මුලින්ම අපි අපේ Project Structure එක බලමු. මේ වගේ File Structure එකක් පාවිච්චි කරන එක ගොඩක් හොඳයි:
my_flask_app/
├── config.py
├── run.py
├── my_flask_app/
│ ├── __init__.py
│ ├── auth/
│ │ ├── __init__.py
│ │ ├── views.py
│ │ └── templates/
│ │ └── auth/
│ │ ├── login.html
│ │ └── register.html
│ ├── posts/
│ │ ├── __init__.py
│ │ └── views.py
│ │ └── templates/
│ │ └── posts/
│ │ └── index.html
│ ├── templates/
│ │ └── base.html
│ └── static/
│ └── css/
│ └── style.css
└── venv/
මෙහිදී,
my_flask_app/__init__.py
: අපේ main Flask application instance එක හදන "application factory" එක මෙහි අඩංගු වෙනවා.auth/
: Authentication (Login, Register, Logout) සම්බන්ධ සියලුම දේවල් අඩංගු Blueprint එක.posts/
: Blog Posts (View, Add, Edit) සම්බන්ධ සියලුම දේවල් අඩංගු Blueprint එක.templates/
: Shared Templates (base.html
වගේ) අඩංගු වෙනවා.auth/templates/auth/
සහposts/templates/posts/
: ඒ ඒ Blueprints වලටම අදාල Templates.
Blueprint එකක් හදන හැටි (How to create a Blueprint)
දැන් අපි බලමු auth
Blueprint එක කොහොමද හදන්නේ කියලා. මේක අපි my_flask_app/auth/__init__.py
file එකේ ලියනවා.
# my_flask_app/auth/__init__.py
from flask import Blueprint
# Blueprint instance එකක් හදනවා.
# 'auth' කියන්නේ Blueprint එකේ නම.
# __name__ කියන්නේ Blueprint එක අදාල Module එකටම assign කරනවා කියන එක.
# template_folder සහ static_folder කියන්නේ මේ Blueprint එකටම අදාල වෙන Templates සහ Static files තියෙන තැන්.
# url_prefix කියන්නේ මේ Blueprint එකේ තියෙන හැම URL එකකටම මුලට එකතු වෙන කොටස.
auth_bp = Blueprint(
'auth',
__name__,
template_folder='templates',
static_folder='static',
url_prefix='/auth' # මේකෙන් හැම Auth URL එකක්ම /auth/login වගේ වෙනවා.
)
# මේක ගොඩක් වැදගත්! Circular Imports වළක්වා ගන්න views import කරන්නේ පස්සේ.
from . import views
ඒ වගේම posts
Blueprint එකත් අපි my_flask_app/posts/__init__.py
file එකේ ලියනවා:
# my_flask_app/posts/__init__.py
from flask import Blueprint
posts_bp = Blueprint(
'posts',
__name__,
template_folder='templates',
static_folder='static',
url_prefix='/posts'
)
from . import views
Views ලියන හැටි (Writing Views)
දැන් අපි අපේ Views හදමු. auth
Blueprint එකේ Views අපි my_flask_app/auth/views.py
file එකේ ලියනවා.
# my_flask_app/auth/views.py
from flask import render_template, redirect, url_for, flash, request
from . import auth_bp # Blueprint instance එක import කරගන්නවා
@auth_bp.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
# මෙතනදී Database එකෙන් username, password check කරනවා
if username == 'admin' and password == 'password':
flash('Login Successful!', 'success')
return redirect(url_for('posts.index')) # වෙනත් Blueprint එකක view එකකට යනවා
else:
flash('Invalid credentials. Please try again.', 'danger')
return render_template('auth/login.html')
@auth_bp.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
# User registration logic
flash('Registration Successful! Please login.', 'success')
return redirect(url_for('auth.login'))
return render_template('auth/register.html')
@auth_bp.route('/logout')
def logout():
# User logout logic
flash('You have been logged out.', 'info')
return redirect(url_for('auth.login'))
ඒ වගේම posts
Blueprint එකේ Views අපි my_flask_app/posts/views.py
file එකේ ලියනවා:
# my_flask_app/posts/views.py
from flask import render_template, redirect, url_for, flash
from . import posts_bp # Blueprint instance එක import කරගන්නවා
@posts_bp.route('/')
@posts_bp.route('/index')
def index():
# Get all blog posts from a database
sample_posts = [
{'id': 1, 'title': 'First Post', 'content': 'This is my first blog post!'},
{'id': 2, 'title': 'Second Post', 'content': 'Another exciting post.'}
]
return render_template('posts/index.html', posts=sample_posts)
@posts_bp.route('/view/<int:post_id>')
def view_post(post_id):
# Get a specific post from the database
# For simplicity, we'll just show the ID
return render_template('posts/view_post.html', post={'id': post_id, 'title': f'Post {post_id}', 'content': f'Content for post {post_id}.'})
Main Application එකට Register කරන හැටි (Registering with Main App)
දැන් අපි මේ Blueprints දෙකම අපේ main Flask application instance එකට Register කරන්න ඕනේ. මේ සඳහා අපි "application factory pattern" එක භාවිතා කරන එක තමයි හොඳම Best Practice එක. අපි my_flask_app/__init__.py
file එකේ create_app()
කියලා function එකක් හදමු.
# my_flask_app/__init__.py
from flask import Flask, render_template
def create_app():
app = Flask(__name__)
# Configuration load කරගන්නවා
# අපිට config.py එකක් හදලා secrets, database URIs වගේ දේවල් manage කරන්න පුළුවන්.
app.config.from_object('config.DevelopmentConfig') # config.py එකේ DevelopmentConfig Class එක load කරනවා
# Blueprints Register කරනවා
# මෙතනදී තමයි අපි හදපු Blueprints, main app එකට සම්බන්ධ කරන්නේ.
from .auth import auth_bp
from .posts import posts_bp
app.register_blueprint(auth_bp)
app.register_blueprint(posts_bp)
# Error Handlers
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
# Optional: A simple homepage for the root URL
@app.route('/')
def index():
return "<h1>Welcome to My Flask App!</h1><p>Visit <a href='/auth/login'>Login</a> or <a href='/posts/index'>Posts</a></p>"
return app
අපි config.py
file එකත් හදාගමු:
# config.py
import os
class Config:
SECRET_KEY = os.environ.get('SECRET_KEY') or 'super_secret_key_for_dev'
# වෙනත් පොදු configurations මෙතනට
class DevelopmentConfig(Config):
DEBUG = True
# Development වලට අදාල configurations මෙතනට
class ProductionConfig(Config):
DEBUG = False
# Production වලට අදාල configurations මෙතනට
අවසාන වශයෙන්, අපේ App එක run කරන්න run.py
file එක හදමු:
# run.py
from my_flask_app import create_app # අපේ factory function එක import කරගන්නවා
app = create_app()
if __name__ == '__main__':
app.run(debug=True) # Development environment එකේදී debug=True වැදගත්
URL Generation (url_for
) with Blueprints
Blueprints එක්ක වැඩ කරනකොට url_for()
function එක භාවිතා කරන විදිය පොඩ්ඩක් වෙනස් වෙනවා. සාමාන්යයෙන් අපි url_for('view_function_name')
කියලා දුන්නට, Blueprints එක්ක වැඩ කරනකොට url_for('blueprint_name.view_function_name')
කියලා දෙන්න ඕනේ.
උදාහරණයක් විදියට, auth
Blueprint එකේ තියෙන login
view එකට යන්න ඕන නම්:
# Python Code එක ඇතුලේ
url_for('auth.login')
posts
Blueprint එකේ තියෙන index
view එකට යන්න ඕන නම්:
# Python Code එක ඇතුලේ
url_for('posts.index')
Templates ඇතුලෙත් මේ විදියටම පාවිච්චි කරන්න පුළුවන්:
<!-- base.html එකේදී -->
<a href="{{ url_for('auth.login') }}">Login</a>
<a href="{{ url_for('posts.index') }}">View Posts</a>
පොදු ගැටළු සහ විසඳුම් (Common Issues & Solutions)
Circular Imports (චක්රීය ආයාත)
Blueprints භාවිතා කරනකොට ගොඩක් වෙලාවට එන ප්රධාන ගැටළුවක් තමයි Circular Imports. මේක වෙන්නේ Python modules එකිනෙක මත රඳා පවතින විදියට import කරනකොට.
උදාහරණයක් විදියට, my_flask_app/auth/__init__.py
එකේදී views.py
import කරනවා, ඒ වගේම my_flask_app/auth/views.py
එකේදී __init__.py
එකේ තියෙන auth_bp
instance එක import කරනවා නම්, මේක Circular Import එකක්. Python module එකක් load කරනකොට, ඒක සම්පූර්ණයෙන්ම load වෙන්න කලින්, වෙනත් module එකක් load කරන්න හැදුවොත් මේ error එක එනවා.
# වැරදි ක්රමය - my_flask_app/auth/__init__.py
from flask import Blueprint
from . import views # views.py එක මුලින්ම import කළොත්
auth_bp = Blueprint('auth', __name__)
# ...
# වැරදි ක්රමය - my_flask_app/auth/views.py
from . import auth_bp # මේක auth_bp හදන්න කලින් import කරන්න හැදුවොත්
from flask import render_template
@auth_bp.route('/')
def index():
return "Auth Index"
මේ ගැටළුවට විසඳුම තමයි views.py
file එක __init__.py
එකේදී Blueprint instance එක හදලා ඉවර වුණාට පස්සේ import කරන එක. අපි කලින් උදාහරණ වලදීත් ඒ විදියටම කරා, මතකයි නේද?
# නිවැරදි ක්රමය - my_flask_app/auth/__init__.py
from flask import Blueprint
auth_bp = Blueprint(
'auth',
__name__,
template_folder='templates',
static_folder='static',
url_prefix='/auth'
)
# Views.py එක import කරන්නේ Blueprint instance එක හදලා ඉවර වුණාට පස්සේ.
# මේකෙන් Python වලට මුලින්ම Blueprint එක register කරන්න පුළුවන්,
# ඊට පස්සේ ඒ Blueprint එකට views ටික assign කරන්න පුළුවන්.
from . import views
මේ from . import views
කියන විදිය Python වලදි module එකක් load කරන process එකට උදව් වෙනවා. ඒකෙන් Python වලට __init__.py
එක සම්පූර්ණයෙන්ම Execute කරන්න ඉඩ දෙනවා, ඊට පස්සේ views.py
එක load කරන්න පුළුවන්, views.py
එකට auth_bp
instance එක තියෙනවා. ඒ නිසා මේක මතක තියාගන්න! 💡
Best Practices: Modularity for Scalability
Flask Blueprints පාවිච්චි කරන එක තව තවත් effective කරගන්න පුළුවන් Best Practices ටිකක් බලමු.
- Blueprint එකක තනි Feature එකකට අවධානය යොමු කරන්න (Focus on Single Feature): සෑම Blueprint එකක්ම එකම Feature එකක් හෝ ඒ හා සම්බන්ධ Feature සමූහයක් පමණක් Manage කරන්න ඕනේ. උදා: User management වලට එකක්, Product management වලට තව එකක්, Analytics වලට තව එකක් වගේ.
url_prefix
බුද්ධිමත්ව භාවිතා කරන්න:url_prefix
එකෙන් ඔබේ URL Structure එක පැහැදිලිව තියාගන්න පුළුවන්. උදා:/users/login
,/products/list
,/api/v1/data
වගේ.- Templates සහ Static files Blueprint එක තුළම තබන්න: කලින් උදාහරණ වල වගේම, Blueprint එකට අදාල Templates සහ Static files ඒ Blueprint එකේ folder එක ඇතුලෙම තියන්න පුරුදු වෙන්න. මේකෙන් Code එකේ පැහැදිලි බව වැඩි වෙනවා.
- Application Context සහ Configuration භාවිතය: Blueprints අතර කෙලින්ම Data Share කරනවා වෙනුවට, Flask Application Context එක හෝ App configuration එක හරහා Data Share කරන්න. මේකෙන් Blueprints අතර Dependency අඩු වෙනවා.
- Application Factory Pattern එක භාවිතා කරන්න: අපි කලින් දැක්කා වගේ,
create_app()
වගේ function එකකින් Flask App instance එක හදන එක ගොඩක් හොඳ Best Practice එකක්. මේකෙන් Unit Testing කරන්න ලේසියි, ඒ වගේම විවිධ environment වලට App එක configure කරන්නත් පුළුවන්.
නිගමනය (Conclusion)
ඉතින් යාලුවනේ, Flask Blueprints කියන්නේ Flask Framework එකත් එක්ක ලොකු Web Applications හදනකොට අත්යවශ්ය මෙවලමක්. ඔබේ App එකේ Code එක Organize කරගන්න, Maintainability වැඩි කරගන්න, Scalability ලබාගන්න, ඒ වගේම Team එකක් විදියට වැඩ කරනකොටත් මේවා ගොඩක් ප්රයෝජනවත් වෙනවා. අද අපි Blueprints කියන්නේ මොනවද, ඒක කොහොමද Implement කරන්නේ, ඒකෙන් එන Common Issues සහ ඒවාට විසඳුම්, ඒ වගේම Best Practices ටිකක් ගැනත් ඉගෙන ගත්තා.
ඔබේ ඊළඟ Flask Project එකේදී මේ Blueprint concept එක භාවිතා කරන්න උත්සාහ කරන්න. මුලදී පොඩ්ඩක් සංකීර්ණ වගේ දැනුනත්, පුරුදු වෙනකොට මේක ගොඩක් පහසු වේවි. Code එක clean විදියට තියාගන්න එක Software Engineering වලදී ගොඩක් වැදගත්. ඒ වගේම මතක තියාගන්න, හොඳින් organize කරපු Code එකක් කියන්නේ Team එකකට වැඩ කරන්න පහසු කරනවා වගේම, App එකේ අනාගත වර්ධනයටත් හොඳ පදනමක් දානවා කියන එකයි.
ඔයාගේ අත්දැකීම් මොනවද? මේ Blueprint concept එක ගැන ඔයාට මොනවා හරි ප්රශ්න තියෙනවා නම්, නැත්නම් ඔයා දන්නා අලුත් Tips තියෙනවා නම්, පහළින් comment එකක් දාලා අපිත් එක්ක බෙදාගන්න! Happy Coding! 🚀