Sometimes you don’t want to define all your routes in one place, you want to be modular right!? You can do that too with Flask-Via.
The most basic way of including other routes is to use the flask_via.routers.Include router. This is not a intended replacement or implementation of Flask blueprints, just a simple way of putting routes somewhere else in your application.
Assume the following application structure:
/path/to/foo
- bar/
- __init__.py
- routes.py
- views.py
- __int__.py
- routes.py
In the top level routes.py we would have:
from flask.ext.via.routers import Include
routes = [
Include('foo.bar.routes')
]
In the foo.routes we would have:
from flask.ext.via.routes import default
from foo.bar.views import some_view
routes = [
default.Basic('/bar', some_view)
]
You can see this in action with the Small Application Example.
Flask Blueprints are also supported allowing Flask-Via to automatically register blueprints on the application and routes on the blueprint, this is provided by the flask_via.routers.default.Blueprint router.
Let us assume we have the following application structure:
/path/to/foo
- bar/
- templates/
- foo.html
- __init__.py
- routes.py
- views.py
- __int__.py
- routes.py
In the above structure bar is a Flask blueprint which we wish to add to our flask application, so our top level routes would look like this:
from flask.ext.via.routers.default import Blueprint
routes = [
Blueprint('bar', 'foo.bar', template_folder='templates')
]
You will note we give the blueprint a name and pass the top level module path to the blueprint rather than a path to the routes file.
In our blueprints views we can define routes as normal:
from flask.ext.via.routes import default
from foo.bar.views import some_view
routes = [
default.Basic('/bar', some_view)
]
Note
All routes will be added to the blueprint rather than the flask application, this applies to any routes included using the Include router.