Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # my_blueprint.py
- from flask import Blueprint, render_template
- class MyBlueprint(object):
- def __init__(self, app, *args, **kwargs):
- if app is not None:
- self.init_app(app, **kwargs)
- def init_app(self, app, **kwargs):
- url_prefix = kwargs['url_prefix'] if 'url_prefix' in kwargs else '/DEFAULT_URL_PREFIX'
- my_bp = Blueprint('my_bp', __name__, template_folder='mybp_templates')
- my_bp.add_url_rule('/', 'home', self.my_home_route)
- app.register_blueprint(my_bp, url_prefix=url_prefix)
- def my_home_route(self):
- return render_template('home.html')
- # mybp_templates/home.html
- <h1>My Blueprint Home!</h1>
- # app.py
- from flask import Flask
- from my_blueprint import MyBlueprint
- app = Flask(__name__)
- MyBlueprint(app, url_prefix='/my-blueprint')
- app.run()
- # run `python app.py`
- # visit url: http://127.0.0.1:5000/my-blueprint
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement