The documentation here and here talks about it.
There are methods that can help with this:
add_url_rule(rule, endpoint=None, view_func=None, **options)
which works as a standard decorator:
@app.route('/') def index(): pass
which is equivalent to
def index(): pass app.add_url_rule('/', 'index', index)
If view_func not specified, then:
app.view_functions['index'] = index
With request.method , respectively:
def index(): if request.method == 'OPTIONS': # custom options handling here ... return 'Hello World!' index.provide_automatic_options = False index.methods = ['GET', 'OPTIONS'] app.add_url_rule('/', index)
If I understand you correctly, this is what you need. When generating a dictionary, you can automatically import exsons.
This is how I automatically import Blueprints :
def create_app(): ... register_routes(app) ... return app def register_routes(app): """Register routes.""" from . import controllers from flask.blueprints import Blueprint for module in _import_submodules_from_package(controllers): bp = getattr(module, 'bp') if bp and isinstance(bp, Blueprint): app.register_blueprint(bp) def _import_submodules_from_package(package): import pkgutil modules = [] for importer, modname, ispkg in pkgutil.iter_modules(package.__path__, prefix=package.__name__ + "."): modules.append(__import__(modname, fromlist="dummy")) return modules
And already in Blueprints themselves you can implement those same action games.
This helper may also be useful:
rules = {} for endpoint, _rules in iteritems(app.url_map._rules_by_endpoint): if any(item in endpoint for item in ['_debug_toolbar', 'debugtoolbar', 'static', 'admin']): continue rules[endpoint] = [{'rule': rule.rule} for rule in _rules]
Which makes it easy to get URLs for subsequent mapping, with a debugtoolbar , admin etc. bypass, for example, for the subsequent sitemap generation.
Or, here’s a command for manage.py , so that you can always quickly see which URLs are currently registered:
@manager.command def list_routes(): import urllib.request from flask import url_for output = [] for rule in app.url_map.iter_rules(): options = {} for arg in rule.arguments: options[arg] = "[{0}]".format(arg) methods = ','.join(rule.methods) url = url_for(rule.endpoint, **options) line = urllib.request.unquote("{:50s} {:20s} {}".format(rule.endpoint, methods, url)) for line in sorted(output): print (line)