This is where the major problem with individual template folders comes in. jinja_loader if loader is not None : yield blueprint, loader jinja_loader if loader is not None : yield self. If the template is not found there, it then continues to yield the rest of the loaders created for each blueprint that has a template_folder=”something” option set: def _iter_loaders ( self, template ): loader = self. Upon a call to render_template this loader first yields the familiar app.jinja_loader to be used to load a template. In reality, something called a DispatchingJinjaLoader is created in the main Flask application. The FileSystemLoader stored at jinja_loader is not actually the top Jinja loader for a Flask application. On instantiation of a Flask application with a call to Flask(_name_), Flask creates a Jinja FileSystemLoader that points to the root templates folder in the _PackageBoundObject class (which both a Flask application and a Blueprint inherit from) called jinja_loader, which you would usually access as app.jinja_loader. Unfortunately the reality is not so simple. It seems intuitive that from within the “users” blueprint a call to render_template(“index.html”) would render the template located in its templates folder which perhaps would extend the base.html template. You might hope that this would allow you to create an application structured as shown: app/ Luckily (at least I thought at the time), Flask allows you to specify a templates folder when instantiating a Blueprint, as shown: mod = Blueprint ( 'users', _name_, url_prefix = '/users', template_folder = "templates" ) This would make things more modular, as a single blueprint could be deleted from or added to an application without needing to mess around with another set of templates being copied into the main templates folder. On a recent project, I decided I wanted to start storing an individual templates folder inside ofĮach blueprint’s Python module instead of jumbling them all together into a single folder. A side effect of this is that for large applications with many blueprints you end up with a templates folder that is a jumble of subdirectories, each relating to a specific blueprint. Example: ```python bp = APIBlueprint('foo', _name_, tag='Foo') ``` ```python bp = APIBlueprint('foo', _name_, tag=`) for `json` location.A lot of the work I do involves large Flask applications structured in the manner described by the excellent Digital Ocean guide How To Structure Large Flask Applications. Accepts a tag name string or an OpenAPI tag dict. If not set, the `.title()` will be used (`'foo'` -> `'Foo'`). This helps locate the `root_path` for the blueprint. import_name: The name of the blueprint package, usually `_name_`. Arguments: name: The name of the blueprint. Union ] = _sentinel # type: ignore ) -> None : """Make a blueprint instance. Optional ] = None, enable_openapi : bool = True, static_folder : t. *Version added: 0.2.0* """ def _init_ ( self, name : str, import_name : str, tag : t. Examples: ```python from apiflask import APIBlueprint bp = APIBlueprint('foo', _name_) ``` *Version changed: 0.5.0* - Add `enable_openapi` parameter. Class APIBlueprint ( APIScaffold, Blueprint ): """Flask's `Blueprint` object with some web API support.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |