Skip to content
Myr

Updated blueprint documentation

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 2647bec · Browse files · 1 file+89 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+89 added−1 removed

  • other:+89 added−1 removed

1 file+89 added−1 removed

modified:docs/blueprints.rst+89 added-1 removed
55 unmodified lines
56 @simple_page.route('/', defaults={'page': 'index'})57 @simple_page.route('/<page>')58 def show(page):59 try:60 return render_template('simple_pages/%s.html' % page)60 return render_template('pages/%s.html' % page)61 except TemplateNotFound:62 abort(404)6364When you bind a function with the help of the ``@simple_page.route``65decorator the blueprint will record the intention of registering the66function `show` on the application when it's later registered.67Additionally it will prefix the endpoint of the function with the68name of the blueprint which was given to the :class:`Blueprint`69constructor (in this case also ``simple_page``).7071So how do you register that blueprint? Like this::7273 from flask import Flask74 from yourapplication.simple_page import simple_page7576 app = Flask(__name__)77 app.register_blueprint(simple_page)7879If you check the rules registered on the application, you will find80these::8182 [<Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>,83 <Rule '/<page>' (HEAD, OPTIONS, GET) -> simple_page.show>,84 <Rule '/' (HEAD, OPTIONS, GET) -> simple_page.show>]8586The first one is obviously from the application ifself for the static87files. The other two are for the `show` function of the ``simple_page``88blueprint. As you can see, they are also prefixed with the name of the89blueprint and separated by a dot (``.``).9091Blueprint Resources92-------------------9394Blueprints can provide resources as well. Sometimes you might want to95introduce a blueprint only for the resources it provides.9697Blueprint Resource Folder98`````````````````````````99100Like for regular applications, blueprints are considered to be contained101in a folder. While multiple blueprints can origin from the same folder,102it does not have to be the case and it's usually not recommended.103104The folder is infered from the second argument to :class:`Blueprint` which105is ususally `__name__`. This argument specifies what logical Python106module or package corresponds to the blueprint. If it points to an actual107Python package that package (which is a folder on the filesystem) is the108resource folder. If it's a module, the package the module is contained in109will be the resource folder. You can access the110:attr:`Blueprint.root_path` property to see what's the resource folder::111112 >>> simple_page.root_path113 '/Users/username/TestProject/yourapplication'114115To quickly open sources from this folder you can use the116:meth:`~Blueprint.open_resource` function::117118 with simple_page.open_resource('static/style.css') as f:119 code = f.read()120121Static Files122````````````123124A blueprint can expose a folder with static files by providing a path to a125folder on the filesystem via the `static_folder` keyword argument. It can126either be an absolute path or one relative to the folder of the127blueprint::128129 admin = Blueprint('admin', __name__, static_folder='static')130131By default the rightmost part of the path is where it is exposed on the132web. Because the folder is called ``static`` here it will be available at133the location of the blueprint + ``/static``. Say the blueprint is134registered for ``/admin`` the static folder will be at ``/admin/static``.135136The endpoint is named `blueprint_name.static` so you can generate URLs to137it like you would do to the static folder of the application::138139 url_for('admin.static', filename='style.css')140141Templates142`````````143144If you want the blueprint to expose templates you can do that by providing145the `template_folder` parameter to the :class:`Blueprint` constructor::146147 admin = Blueprint('admin', __name__, template_folder='templates')148149As for static files, the path can be absolute or relative to the blueprint150resource folder.