Python: Request handler in Flask
Question: I'm learning Flask, and the request handling seems to be like:

    
    
    @app.route("/")
    def hello():
        return "Hello World!"
    

So I end up defining the functions for all my routes in a single file. I'd
much rather have functions for a model in its own file, e.g. get_user,
create_user in user.py. I've used Express (node.js) in the past, and I can do:

    
    
    user = require('./models/user')
    app.get('/user', user.list)
    

where user.coffee (or .js) has a list function.

How do I do the same in Flask?


Answer: From the [docs](http://flask.pocoo.org/docs/api/#flask.Flask.route):

> A decorator that is used to register a view function for a given URL rule.
> This does the same thing as
> [add_url_rule()](http://flask.pocoo.org/docs/api/#flask.Flask.add_url_rule)
> but is intended for decorator usage

The `add_url_rule` docs elaborate:

    
    
    @app.route('/')
    def index():
        pass
    

Is equivalent to the following:

    
    
    def index():
        pass
    app.add_url_rule('/', 'index', index)
    

You can just as easily import your view functions into a `urls.py` file and
call `add_url_rule` once for each view function there instead of defining the
rules along side the functions or use the [lazy loading
pattern](http://flask.pocoo.org/docs/patterns/lazyloading/).

