Advertisement
YellowShark

Flask Blueprint Example

Oct 21st, 2015
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.86 KB | None | 0 0
  1. # my_blueprint.py
  2. from flask import Blueprint, render_template
  3.  
  4. class MyBlueprint(object):
  5.   def __init__(self, app, *args, **kwargs):
  6.     if app is not None:
  7.       self.init_app(app, **kwargs)
  8.  
  9.   def init_app(self, app, **kwargs):
  10.     url_prefix = kwargs['url_prefix'] if 'url_prefix' in kwargs else '/DEFAULT_URL_PREFIX'
  11.     my_bp = Blueprint('my_bp', __name__, template_folder='mybp_templates')
  12.     my_bp.add_url_rule('/', 'home', self.my_home_route)
  13.     app.register_blueprint(my_bp, url_prefix=url_prefix)
  14.  
  15.   def my_home_route(self):
  16.     return render_template('home.html')
  17.  
  18. # mybp_templates/home.html
  19. <h1>My Blueprint Home!</h1>
  20.  
  21. # app.py
  22. from flask import Flask
  23. from my_blueprint import MyBlueprint
  24.  
  25. app = Flask(__name__)
  26. MyBlueprint(app, url_prefix='/my-blueprint')
  27. app.run()
  28.  
  29. # run `python app.py`
  30. # visit url: http://127.0.0.1:5000/my-blueprint
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement