return web.Response(text=text, content_type=request['selected_media_type']) ##### Configuration ##### CONFIG = { 'AIOHTTP_UTILS': { 'RENDERERS': OrderedDict([ ('application/json', negotiation.render_json), ('text/html', render_mako), ]) } } ##### Application ##### app = web.Application(router=RouterWithTemplating(), debug=True) app['mako_lookup'] = lookup app.update(CONFIG) negotiation.setup(app) app.router.add_route('GET', '/', index, template='index.html') if __name__ == "__main__": run( app, app_uri='examples.mako_example:app', reload=True, port=8000, )
template = request.app['mako_lookup'].get_template(template_name) text = template.render_unicode(**data) return web.Response(text=text, content_type=request['selected_media_type']) ##### Configuration ##### CONFIG = { 'AIOHTTP_UTILS': { 'RENDERERS': OrderedDict([ ('application/json', negotiation.render_json), ('text/html', render_mako), ]) } } ##### Application ##### app = web.Application(router=RouterWithTemplating(), debug=True) app['mako_lookup'] = lookup app.update(CONFIG) negotiation.setup(app) app.router.add_route('GET', '/', index, template='index.html') if __name__ == "__main__": run( app, app_uri='examples.mako_example:app', reload=True, port=8000, )
return web.json_response(data={"error": "No url founded in the request."}, status=400) morph = pymorphy2.MorphAnalyzer() negative_words = await get_words(constants.NEGATIVE_WORDS) urls = query_urls.split(",") if len(urls) > constants.URLS_LIMIT: return web.json_response(data={"error": "too many urls in request, should be 10 or less"}, status=400) articles_results = await handle_urls(urls=urls, morph=morph, charged=negative_words) return web.json_response(articles_results) async def handle_urls(urls: list, morph: pymorphy2.MorphAnalyzer, charged: list): results = [] async with ClientSession() as session: for url in urls: async with create_task_group() as tg: await tg.spawn(process_article, session, url, charged, morph, results) return results app = web.Application() app.add_routes( [web.get('/', index)] ) if __name__ == '__main__': run(app, reload=True, app_uri="server:app")
sockjs.add_endpoint(self, self.sockjs_handler, name="game", prefix="/sockjs/") @aiohttp_jinja2.template('index.html') def index(self, request): return { 'title': 'Whats up ' + request.GET.get('name', 'World'), 'app': self, } def sockjs_handler(self, msg, session): if msg.tp == sockjs.MSG_OPEN: session.manager.broadcast("Someone Joined") self.game.player_joined(session) if msg.tp == sockjs.MSG_MESSAGE: session.manager.broadcast(msg.data) if msg.tp == sockjs.MSG_CLOSED: session.manager.broadcast("Someone Left") logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s') app = GameApplication(router=web.UrlDispatcher(), debug=True) if __name__ == "__main__": run(app, app_uri='game.main:app', reload=True, port=8000, host='0.0.0.0')
class HelloResource(): async def get(self, request): name = request.GET.get('name', 'World') return response({'message': 'Hello' + name}) with routing.add_route_context(app) as route: route('GET', '/', index) # app.router.add_resource_object('/', HelloResource()) # Content negotiation # negotiation.setup( # app, renderers={ # 'application/json': negotiation.render_json # } # ) path_norm.setup(app) if __name__ == '__main__': # Development server run( app, # app_uri='main:app', # reload=True, port=8000, host='0.0.0.0')
@coroutine def index(request): return Response('Welcome!') class HelloResource: @coroutine def get(self, request): return Response({ 'message': 'Welcome to the API!' }) with routing.add_route_context(app) as route: route('GET', '/', index) with routing.add_resource_context(app, url_prefix='/api/') as route: route('/', HelloResource()) negotiation.setup(app) path_norm.setup(app) if __name__ == "__main__": run( app, app_uri="examples.kitchen_sink:app", reload=True, port=8000 )
from asyncio import coroutine from aiohttp import web from aiohttp_utils import Response, routing, negotiation, run, path_norm app = web.Application(router=routing.ResourceRouter()) @coroutine def index(request): return Response('Welcome!') class HelloResource: @coroutine def get(self, request): return Response({'message': 'Welcome to the API!'}) with routing.add_route_context(app) as route: route('GET', '/', index) with routing.add_resource_context(app, url_prefix='/api/') as route: route('/', HelloResource()) negotiation.setup(app) path_norm.setup(app) if __name__ == "__main__": run(app, app_uri="examples.kitchen_sink:app", reload=True, port=8000)