Routes can live anywhere you want them too, as long as they are importable.
You can tell Flask-Via where to find routes in a couple of ways:
You can use which ever you prefer.
from flask import Flask
from flask.ext.via import Via
app = Flask(__name__)
app.config['VIA_ROUTES_MODULE'] = 'yourapp.routes'
via = Via()
via.init_app(app)
if __name__ == "__main__":
app.run(debug=True)
from flask import Flask
from flask.ext.via import Via
app = Flask(__name__)
via = Via()
via.init_app(app, routes_module='yourapp.routes')
if __name__ == "__main__":
app.run(debug=True)
The routes module should define a list of routes, by default this list is called routes:
routes = [
Functional('/', home),
Functional('/about', about),
]
You can configure Flask-Via to look for any variable name of your choosing, this is done by passing an argument named routes_name into init_app, for example:
via = Via()
via.init_app(app, routes_name='urls')
You can also make this setting permanent by using the VIA_ROUTES_NAME configuration variable:
app = Flask(__name__)
app.config['VIA_ROUTES_MODULE'] = 'yourapp.routes'
app.config['VIA_ROUTES_NAME'] = 'urls'
via = Via()
via.init_app(app)
Note
If you set VIA_ROUTES_NAME overriding this using routes_name is still possible however this does not propagate over any routes which are included.
Assume we have the following application structure:
/path/to/foo
- __init__.py
- routes.py
- views.py
- app.py
Within views.py we have:
def home():
return 'Hello world!'
def about():
return 'The world is big'
Within routes.py we have:
from flask.ext.via.routers import default
urls = [
default.Functional('/', home),
default.Functional('/about', about),
]
Within app.py we have:
from flask import Flask
from flask.ext.via import Via
app = Flask(__name__)
app.config['VIA_ROUTES_MODULE'] = 'foo.routes'
via = Via()
via.init_app(app, routes_name='urls')
if __name__ == "__main__":
app.run(debug=True)
You will see we used routes_name when calling via.init_app to tell Via what variable to look for within the routes module.