Hello everyone, I love the way Flask let you declare routes using the @app.route decorator. I looked at the PR https://github.com/KeepSafe/aiohttp/pull/195 which implements this, but it was rejected.
Are there any plans to have this feature? This is the only thing that prevents me to fully appreciate aiohttp and use it in my web apps.
Thank you for the great work!
@fafhrd91 mentioned two reasons for avoiding route decorator:
They are still present, so we don't want add the issue.
But if you _really_ want to use decorators just derive from web.Application and add desired method.
-10, I'm in with @asvetlov, you'll have more issues than solutions, especially if you use threads.
Look the source code of Flask to see the full-monty.
Since this ticket probably leads common public from Google, I'll point them to the small helper library that I've created:
https://github.com/IlyaSemenov/aiohttp_route_decorator
This is something intermediate between pure evil (global app) and tedious repetitive manual app.router setup.
@IlyaSemenov thanks for this library, especially the non-decorator version. I'll take a look.
Can this library of decorators be used for nested applications routes? I tried but couldn't get it to work.
https://github.com/IlyaSemenov/aiohttp_route_decorator is obsolete, aiohttp has native support for router decorators: https://docs.aiohttp.org/en/stable/web.html#alternative-ways-for-registering-routes
In fact it uses the same technique as aiohttp_route_decorator (at least at first glance).
Yes, RouteTableDef supports nested applications.
@asvetlov Can you give me an example with aiohttp decorators for nested applications? Following is my nested app without decorators.
from aiohttp import web
async def index_view(request):
return web.Response(text='index\n')
async def subapp_view(request):
name = request.match_info.get('name', "Anonymous")
txt = "Hello {}\n".format(name)
return web.Response(text=txt)
app = web.Application()
app.router.add_get('/', index_view)
greet = web.Application()
greet.router.add_get('/{name}', subapp_view)
app.add_subapp('/greet/', greet)
if __name__ == '__main__':
web.run_app(app, host='127.0.0.1', port=8080)
from aiohttp import web
app_routes = web.RouteTableDef()
@app_routes.get('/')
async def index_view(request):
return web.Response(text='index\n')
subapp_routes = web.RouteTableDef()
@subapp_routes.get('/{name}')
async def subapp_view(request):
name = request.match_info.get('name', "Anonymous")
txt = "Hello {}\n".format(name)
return web.Response(text=txt)
greet = web.Application()
greet.router.add_routes(subapp_routes)
app = web.Application()
app.router.add_routes(app_routes)
app.add_subapp('/greet/', greet)
if __name__ == '__main__':
web.run_app(app, host='127.0.0.1', port=8080)
Thanks @asvetlov!
Also, in the documentation, its mentioned that we should avoid using decorators in the FAQs [https://docs.aiohttp.org/en/stable/faq.html#id1]. Is it for the old version or I should be good to work with these decorators?
Latest unreleased docs are different: https://docs.aiohttp.org/en/latest/faq.html
@asvetlov I've updated aiohttp_route_decorator's README with a deprecation warning pointing to the new built-in RouteTableDef.
@IlyaSemenov cool!
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs.
If you feel like there's important points made in this discussion, please include those exceprts into that [new issue].
Most helpful comment