Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Are there any plans for @app.route decorator? #428

Closed
youtux opened this issue Jun 29, 2015 · 13 comments
Closed

Are there any plans for @app.route decorator? #428

youtux opened this issue Jun 29, 2015 · 13 comments
Labels

Comments

@youtux
Copy link

youtux commented Jun 29, 2015

Hello everyone, I love the way Flask let you declare routes using the @app.route decorator. I looked at the PR #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!

@asvetlov
Copy link
Member

asvetlov commented Jun 30, 2015

@fafhrd91 mentioned two reasons for avoiding route decorator:

  • this adds huge problem named "configuration as side effect of importing"
  • route matching is order specific, it is very hard to maintain import order

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.

@ludovic-gasc
Copy link
Contributor

-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.

@IlyaSemenov
Copy link
Contributor

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.

@ludovic-gasc
Copy link
Contributor

@IlyaSemenov thanks for this library, especially the non-decorator version. I'll take a look.

@Akshita6
Copy link

Can this library of decorators be used for nested applications routes? I tried but couldn't get it to work.

@asvetlov
Copy link
Member

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.

@Akshita6
Copy link

@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)

@asvetlov
Copy link
Member

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)

@Akshita6
Copy link

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?

@asvetlov
Copy link
Member

Latest unreleased docs are different: https://docs.aiohttp.org/en/latest/faq.html

@IlyaSemenov
Copy link
Contributor

@asvetlov I've updated aiohttp_route_decorator's README with a deprecation warning pointing to the new built-in RouteTableDef.

@asvetlov
Copy link
Member

@IlyaSemenov cool!

@lock
Copy link

lock bot commented Oct 28, 2019

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].
[new issue]: https://github.com/aio-libs/aiohttp/issues/new

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants