Bases: object
Handles the import of routes module and obtaining a list of routes from that module as well as loading each route onto the application
New in version 2014.05.06.
Imports a routes module and gets the routes from within that module and returns them.
Parameters: |
|
---|---|
Returns: | List of routes in the module |
Return type: | list |
Raises: |
|
Bases: flask_via.RoutesImporter
Flask-VIa integration into Flask applications. Flask-Via can be integrated in two different ways depending on how you have setup your Flask application.
New in version 2014.05.06.
You can bind to a specific flask application:
from flask import Flask
from flask.ext.via import Via
from flask.ext.via.routers.flask import Functional
app = Flask(__name__)
def foo(bar=None):
return 'Foo View!'
routes = [
Functional('/foo', foo),
Functional('/foo/<bar>', foo, endpoint='foo2'),
]
via = Via(app, routes_module='path.to.here')
if __name__ == "__main__":
app.run(debug=True)
Or if you use an application factory you can use flask_via.Via.init_app():
from flask import Flask
from flask.ext.via import Via
from flask.ext.via.routers.flask import Functional
via = Via()
def foo(bar=None):
return 'Foo View!'
routes = [
Functional('/foo', foo),
Functional('/foo/<bar>', foo, endpoint='foo2'),
]
def create_app():
app = Flask(__name__)
via.init_app(app)
return app
app = create_app()
if __name__ == "__main__":
app.run(debug=True)
Initialises Flask extension. Bootstraps the automatic route registration process.
Replace NotImplementedError with ImproperlyConfigured
routes_name keyword argument default value set to None
routes_name can now be configured using VIA_ROUTES_NAME app configuration variable. If routes_name keyword argument and VIA_ROUTES_NAME are not configured the default will be routes.
Improved init_app method
Parameters: | app (flask.app.Flask) – Flask application instance |
---|---|
Keyword Arguments: | |
|
|
Raises: | ImproperlyConfigured – If VIA_ROUTES_MODULE is not configured in appluication config and route_module keyword argument has not been provided. |
Custom exceptions which can be thrown by Flask-Via.
Base router classes and utilities.
Bases: object
Base router class all routers should inherit from providing common router functionality.
New in version 2014.05.06.
Example
from flask.ext.via.routers import BaseRouter
class MyRouter(BaseRouter):
def __init__(self, arg):
...
def add_to_app(self, app):
...
Bases: flask_via.routers.BaseRouter, flask_via.RoutesImporter
Adds the ability to include routes from other modules, this can be handy when you want to break out your routes into separate files for sanity.
New in version 2014.05.06.
Note
This is not a implementation of Flask blueprints
Constructor for Include router, taking the passed arguments and storing them on the instance.
url_prefix argument added
routes_name keyword argument default value set to None
endpoint keyword argument added
Parameters: | routes_module (str) – Python dotted path to the routes module |
---|---|
Keyword Arguments: | |
|
Instead of adding a route to the flask application this will include and load routes similar, same as in the flask_via.Via class.abs
url_prefix now injected into kwargs when loading in routes
endpoint now injects into kwargs when loading in routes
Parameters: |
|
---|
A set of flask specific router classes to be used when defining routes.
Example
from flask.ext.via.routes.flask import Basic, Pluggable
from yourapp.views import BarView, foo_view
routes = [
Basic('/foo', 'foo', foo_view),
Pluggable('/bar', BarView, 'bar'),
]
Bases: flask_via.routers.default.Functional
This is deprecated and will be removed in the next release. Please use Functional.
New in version 2014.05.06.
Deprecated since version 2014.05.19.
Bases: flask_via.routers.BaseRouter, flask_via.RoutesImporter
Registers a flask blueprint and registers routes to that blueprint, similar to flask_via.routers.Include.
New in version 2014.05.06.
Example
Auto creates Blueprint instance*
from flask.ext.via.routers import default
routes = [
default.Blueprint('foo', 'flask_via.examples.blueprints.foo')
]
Pass existing Blueprint instance*
from flask import Blueprint
from flask.ext.via.routers import default
blueprint = Blueprint('foo', __name__)
routes = [
default.Blueprint(blueprint)
]
Constructor for blueprint router.
Replaced name with name_or_instance argument which allows the router to take an already instantiated blueprint instance.
module argument optional when instance is passed as the first argument
routes_name keyword argument default value set to None
Parameters: | name (str, flask.blueprints.Blueprint) – Blueprint name or a Blueprint class instance |
---|---|
Keyword Arguments: | |
|
Creates a Flask blueprint and registers routes with that blueprint, this means any routes defined will be added to the blueprint rather than the application.
Parameters: |
|
---|
Returns a Flask Blueprint instance, either one provided or created here.
Renamed method from create_blueprint to blueprint
If instance attribute exists, use this is as the blueprint else create the blueprint.
Support for endpoint prefixing
Returns: | An instantiated Flask Blueprint instance |
---|---|
Return type: | flask.blueprints.Blueprint |
Bases: flask_via.routers.BaseRouter
A basic Flask router, used for the most basic form of flask routes, namely functionally based views which would normally use the @route decorator.
New in version 2014.05.19.
Example
from flask.ext.via.routes import default
from yourapp.views import foo_view, bar_view
routes = [
default.Functional('/foo', 'foo', foo_view),
default.Functional('/bar', 'bar', bar_view),
]
Basic router constructor, stores passed arguments on the instance.
Parameters: |
|
---|---|
Keyword Arguments: | |
endpoint (str, optional) – Optional endpoint string, by default flask will use the view function name as the endpoint name, use this argument to change the endpoint name. |
Adds the url route to the flask application object.mro
url_prefix can now be prefixed if present in kwargs
endpoint can now be prefixed if present in kwargs
Parameters: |
|
---|
Bases: flask_via.routers.BaseRouter
Pluggable View router class, allows Flask pluggable view routes to be added to the flask application.
New in version 2014.05.06.
Example
from flask.ext.via.routers import flask
from flask.views import MethodView
class FooView(MethodView):
def get(self):
return 'foo view'
class BarView(MethodView):
def get(self):
return 'bar view'
routes = [
flask.Pluggable('/', FooView, 'foo')
flask.Pluggable('/', BarView, 'bar')
]
Pluggable router constructor, stores passed arguments on instance.
Added view argument
Added endpoint argument
Parameters: |
|
---|
Adds the url route to the flask application object.
Changed in version 2014.05.19: Updated add_url_rule to support endpoint prefixing and support new way of defining Pluggable views
Parameters: |
|
---|
Routers for the Flask-Restful framework.
Bases: flask_via.routers.BaseRouter
The Resource router allows you to define Flask-Restful routes and have those API resources added to the application automatically. For this to work you must at init_app time pass a optional keyword argument restful_api to init_app with its value being the restful api extension instance.
New in version 2014.05.06.
Example
app = Flask(__name__)
api = restful.Api(app)
class FooResource(restful.Resource):
def get(self):
return {'hello': 'world'}
routes = [
Resource('/foo', FooResource)
]
via = Via()
via.init_app(
app,
routes_module='flask_via.examples.restful',
restful_api=api)
if __name__ == '__main__':
app.run(debug=True)
Constructor for flask restful resource router.
Parameters: |
|
---|---|
Keyword Arguments: | |
endpoint (str, optional) – Optional, override Flask-Restful automatic endpoint naming |
Routers for the Flask-Admin framework.
Bases: flask_via.routers.BaseRouter
The Admin router allows you to define Flask-Admin routes and have those views added to the application automatically. For this to work you must at init_app time pass a optional keyword argument flask_admin to init_app with its value being the Flask-Aadmin extension instance.
New in version 2014.05.08.
Note
Flask-Admin has its own way of handling defining urls so this router literally only requires the Flask-Admin view class.
Example
app = Flask(__name__)
admin = Admin(name='Admin')
admin.init_app(app)
class FooAdminView(BaseView):
@expose('/')
def index(self):
return 'foo'
routes = [
AdminRoute(FooAdminView(name='Foo'))
]
via = Via()
via.init_app(
app,
routes_module='flask_via.examples.admin',
flask_admin=admin)
if __name__ == '__main__':
app.run(debug=True)