top | item 18271948

(no title)

ziikutv | 7 years ago

Indeed. Additionally, not take anything away from the Ruby counter part, the Python project is amazing and we use it in production. I really prefer the endpoint and viewmethod declaration methods in Falcon over Flask

    # In this example, I use blueprint plugin
    @bp.route('/', methods=['PUT'])
    def create_resource():
        return
vs

    class Resource(...):
        def on_put(self, request, response):
            return
Mirror: https://pastebin.com/50W8TJV2

Flask's hooking logic [1] is nice but I much prefer Falcon's middleware logic [2]. This then ties into the fact that Flask's design requires working with `g` [3] (essentially a global object) where as the middleware design leads us to bake in data in the request object. The example blow demonstrates how middleware definition works and how "adding data" per request works in both of these frameworks.

    # Flask; assuume `app` exists
    from flask import g
    @app.before_request
    def before_request():
        g.some_variable = 'foobar'
vs

    class ExampleComponent(object):
        def process_request(self, req, resp):
            req.some_variable = 'foobar'
Mirror: https://pastebin.com/Z3yGCfzF

Additionally, Falcon middleware API also exposes `process_resource` which is similar to above but also allows us to _pass_ arguments to the routed resource instance.

[1] http://flask.pocoo.org/docs/1.0/api/#flask.Flask.before_requ...

[2] https://falcon.readthedocs.io/en/stable/api/middleware.html

[3] http://flask.pocoo.org/docs/1.0/appcontext/#storing-data

Edit: Formatting

discuss

order

No comments yet.