1.. _blueprints:23Modular Applications with Blueprints4====================================56.. versionadded:: 0.778Flask uses a concept of *blueprints* for making application components and9supporting common patterns within an application or across applications.10Blueprints can greatly simplify how large applications work and provide a11central means for Flask extensions to register operations on applications.12A :class:`Blueprint` object works similarly to a :class:`Flask`13application object, but it is not actually an application. Rather it is a14*blueprint* of how to construct or extend an application.1516Why Blueprints?17---------------1819Blueprints in Flask are intended for these cases:2021* Factor an application into a set of blueprints. This is ideal for22 larger applications; a project could instantiate an application object,23 initialize several extensions, and register a collection of blueprints.24* Register a blueprint on an application at a URL prefix and/or subdomain.25 Parameters in the URL prefix/subdomain become common view arguments26 (with defaults) across all view functions in the blueprint.27* Register a blueprint multiple times on an application with different URL28 rules.29* Provide template filters, static files, templates, and other utilities30 through blueprints. A blueprint does not have to implement applications31 or view functions.32* Register a blueprint on an application for any of these cases when33 initializing a Flask extension.3435A blueprint in Flask is not a pluggable app because it is not actually an36application -- it's a set of operations which can be registered on an37application, even multiple times. Why not have multiple application38objects? You can do that (see :ref:`app-dispatch`), but your applications39will have separate configs and will be managed at the WSGI layer.4041Blueprints instead provide separation at the Flask level, share42application config, and can change an application object as necessary with43being registered. The downside is that you cannot unregister a blueprint44once an application was created without having to destroy the whole45application object.4647The Concept of Blueprints48-------------------------4950The basic concept of blueprints is that they record operations to execute51when registered on an application. Flask associates view functions with52blueprints when dispatching requests and generating URLs from one endpoint53to another.5455My First Blueprint56------------------5758This is what a very basic blueprint looks like. In this case we want to59implement a blueprint that does simple rendering of static templates::6061 from flask import Blueprint, render_template, abort62 from jinja2 import TemplateNotFound6364 simple_page = Blueprint('simple_page', __name__,65 template_folder='templates')6667 @simple_page.route('/', defaults={'page': 'index'})68 @simple_page.route('/<page>')69 def show(page):70 try:71 return render_template('pages/%s.html' % page)72 except TemplateNotFound:73 abort(404)7475When you bind a function with the help of the ``@simple_page.route``76decorator the blueprint will record the intention of registering the77function `show` on the application when it's later registered.78Additionally it will prefix the endpoint of the function with the79name of the blueprint which was given to the :class:`Blueprint`80constructor (in this case also ``simple_page``).8182Registering Blueprints83----------------------8485So how do you register that blueprint? Like this::8687 from flask import Flask88 from yourapplication.simple_page import simple_page8990 app = Flask(__name__)91 app.register_blueprint(simple_page)9293If you check the rules registered on the application, you will find94these::9596 [<Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>,97 <Rule '/<page>' (HEAD, OPTIONS, GET) -> simple_page.show>,98 <Rule '/' (HEAD, OPTIONS, GET) -> simple_page.show>]99100The first one is obviously from the application ifself for the static101files. The other two are for the `show` function of the ``simple_page``102blueprint. As you can see, they are also prefixed with the name of the103blueprint and separated by a dot (``.``).104105Blueprints however can also be mounted at different locations::106107 app.register_blueprint(simple_page, url_prefix='/pages')108109And sure enough, these are the generated rules::110111 [<Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>,112 <Rule '/pages/<page>' (HEAD, OPTIONS, GET) -> simple_page.show>,113 <Rule '/pages/' (HEAD, OPTIONS, GET) -> simple_page.show>]114115On top of that you can register blueprints multiple times though not every116blueprint might respond properly to that. In fact it depends on how the117blueprint is implemented if it can be mounted more than once.118119Blueprint Resources120-------------------121122Blueprints can provide resources as well. Sometimes you might want to123introduce a blueprint only for the resources it provides.124125Blueprint Resource Folder126`````````````````````````127128Like for regular applications, blueprints are considered to be contained129in a folder. While multiple blueprints can originate from the same folder,130it does not have to be the case and it's usually not recommended.131132The folder is inferred from the second argument to :class:`Blueprint` which133is usually `__name__`. This argument specifies what logical Python134module or package corresponds to the blueprint. If it points to an actual135Python package that package (which is a folder on the filesystem) is the136resource folder. If it's a module, the package the module is contained in137will be the resource folder. You can access the138:attr:`Blueprint.root_path` property to see what the resource folder is::139140 >>> simple_page.root_path141 '/Users/username/TestProject/yourapplication'142143To quickly open sources from this folder you can use the144:meth:`~Blueprint.open_resource` function::145146 with simple_page.open_resource('static/style.css') as f:147 code = f.read()148149Static Files150````````````151152A blueprint can expose a folder with static files by providing a path to a153folder on the filesystem via the `static_folder` keyword argument. It can154either be an absolute path or one relative to the folder of the155blueprint::156157 admin = Blueprint('admin', __name__, static_folder='static')158159By default the rightmost part of the path is where it is exposed on the160web. Because the folder is called ``static`` here it will be available at161the location of the blueprint + ``/static``. Say the blueprint is162registered for ``/admin`` the static folder will be at ``/admin/static``.163164The endpoint is named `blueprint_name.static` so you can generate URLs to165it like you would do to the static folder of the application::166167 url_for('admin.static', filename='style.css')168169Templates170`````````171172If you want the blueprint to expose templates you can do that by providing173the `template_folder` parameter to the :class:`Blueprint` constructor::174175 admin = Blueprint('admin', __name__, template_folder='templates')176177As for static files, the path can be absolute or relative to the blueprint178resource folder. The template folder is added to the searchpath of179templates but with a lower priority than the actual application's template180folder. That way you can easily override templates that a blueprint181provides in the actual application.182183So if you have a blueprint in the folder ``yourapplication/admin`` and you184want to render the template ``'admin/index.html'`` and you have provided185``templates`` as a `template_folder` you will have to create a file like186this: ``yourapplication/admin/templates/admin/index.html``.187188Building URLs189-------------190191If you want to link from one page to another you can use the192:func:`url_for` function just like you normally would do just that you193prefix the URL endpoint with the name of the blueprint and a dot (``.``)::194195 url_for('admin.index')196197Additionally if you are in a view function of a blueprint or a rendered198template and you want to link to another endpoint of the same blueprint,199you can use relative redirects by prefixing the endpoint with a dot only::200201 url_for('.index')202203This will link to ``admin.index`` for instance in case the current request204was dispatched to any other admin blueprint endpoint.