Python aiohttp_jinja2.setup() Examples
The following are 30
code examples of aiohttp_jinja2.setup().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
aiohttp_jinja2
, or try the search function
.
Example #1
Source File: main.py From aiohttp-devtools with MIT License | 6 votes |
def create_app(): app = web.Application() settings = Settings() app.update( settings=settings, static_root_url='/static/', ) jinja2_loader = jinja2.FileSystemLoader(str(THIS_DIR / 'templates')) aiohttp_jinja2.setup(app, loader=jinja2_loader) app.on_startup.append(startup) app.on_cleanup.append(cleanup) aiohttp_session.setup(app, EncryptedCookieStorage(settings.auth_key, cookie_name=settings.cookie_name)) app.router.add_get('/', index, name='index') app.router.add_route('*', '/messages', messages, name='messages') app.router.add_get('/messages/data', message_data, name='message-data') return app
Example #2
Source File: test_jinja_globals.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_static_var_missing(aiohttp_client, caplog): async def index(request): with pytest.raises(RuntimeError, match='static_root_url'): aiohttp_jinja2.render_template('tmpl.jinja2', request, {}) return web.Response() app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader( {'tmpl.jinja2': "{{ static('whatever.js') }}"})) app.router.add_route('GET', '/', index) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status # static_root_url is not set
Example #3
Source File: test_jinja_globals.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_url_param_forbidden_type(aiohttp_client): async def index(request): with pytest.raises(TypeError, match=(r"argument value should be str or int, " r"got arg -> \[<class 'bool'>\] True")): aiohttp_jinja2.render_template('tmpl.jinja2', request, {}) return web.Response() async def other(request): return app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader( {'tmpl.jinja2': "{{ url('other', arg=True)}}"})) app.router.add_route('GET', '/', index) app.router.add_route('GET', '/uid/{arg}', other, name='other') client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status
Example #4
Source File: test_simple_renderer.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_func(aiohttp_client): @aiohttp_jinja2.template('tmpl.jinja2') async def func(request): return {'head': 'HEAD', 'text': 'text'} template = '<html><body><h1>{{head}}</h1>{{text}}</body></html>' app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({ 'tmpl.jinja2': template })) app.router.add_route('*', '/', func) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert '<html><body><h1>HEAD</h1>text</body></html>' == txt
Example #5
Source File: test_jinja_globals.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_helpers_disabled(aiohttp_client): async def index(request): with pytest.raises(jinja2.UndefinedError, match="'url' is undefined"): aiohttp_jinja2.render_template('tmpl.jinja2', request, {}) return web.Response() app = web.Application() aiohttp_jinja2.setup( app, default_helpers=False, loader=jinja2.DictLoader( {'tmpl.jinja2': "{{ url('index')}}"}) ) app.router.add_route('GET', '/', index) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status
Example #6
Source File: test_jinja_globals.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_url_int_param(aiohttp_client): @aiohttp_jinja2.template('tmpl.jinja2') async def index(request): return {} async def other(request): return app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader( {'tmpl.jinja2': "{{ url('other', arg=1)}}"})) app.router.add_route('GET', '/', index) app.router.add_route('GET', '/uid/{arg}', other, name='other') client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert '/uid/1' == txt
Example #7
Source File: test_jinja_globals.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_url_with_query(aiohttp_client): @aiohttp_jinja2.template('tmpl.jinja2') async def index(request): return {} app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader( {'tmpl.jinja2': "{{ url('index', query_={'foo': 'bar'})}}"})) app.router.add_get('/', index, name='index') client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert '/?foo=bar' == txt
Example #8
Source File: test_jinja_globals.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_url(aiohttp_client): @aiohttp_jinja2.template('tmpl.jinja2') async def index(request): return {} async def other(request): return app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader( {'tmpl.jinja2': "{{ url('other', name='John_Doe')}}"})) app.router.add_route('GET', '/', index) app.router.add_route('GET', '/user/{name}', other, name='other') client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert '/user/John_Doe' == txt
Example #9
Source File: app_svc.py From caldera with Apache License 2.0 | 6 votes |
def load_plugins(self, plugins): def trim(p): if p.startswith('.'): return False return True async def load(p): plugin = Plugin(name=p) if plugin.load_plugin(): await self.get_service('data_svc').store(plugin) if plugin.name in self.get_config('plugins'): await plugin.enable(self.get_services()) self.log.debug('Enabled plugin: %s' % plugin.name) if not plugin.version: self._errors.append(Error(plugin.name, 'plugin code is not a release version')) for plug in filter(trim, plugins): if not os.path.isdir('plugins/%s' % plug) or not os.path.isfile('plugins/%s/hook.py' % plug): self.log.error('Problem locating the "%s" plugin. Ensure code base was cloned recursively.' % plug) exit(0) asyncio.get_event_loop().create_task(load(plug)) templates = ['plugins/%s/templates' % p.lower() for p in self.get_config('plugins')] templates.append('templates') aiohttp_jinja2.setup(self.application, loader=jinja2.FileSystemLoader(templates))
Example #10
Source File: test_simple_renderer.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_render_class_based_view(aiohttp_client): class MyView(web.View): @aiohttp_jinja2.template('tmpl.jinja2') async def get(self): return {'head': 'HEAD', 'text': 'text'} template = '<html><body><h1>{{head}}</h1>{{text}}</body></html>' app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({ 'tmpl.jinja2': template })) app.router.add_route('*', '/', MyView) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert '<html><body><h1>HEAD</h1>text</body></html>' == txt
Example #11
Source File: test_simple_renderer.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_convert_func_to_coroutine(aiohttp_client): @aiohttp_jinja2.template('tmpl.jinja2') async def func(request): return {'head': 'HEAD', 'text': 'text'} template = '<html><body><h1>{{head}}</h1>{{text}}</body></html>' app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({ 'tmpl.jinja2': template })) app.router.add_route('*', '/', func) client = await aiohttp_client(app) resp = await client.get('/') txt = await resp.text() assert '<html><body><h1>HEAD</h1>text</body></html>' == txt
Example #12
Source File: test_simple_renderer.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_set_status(aiohttp_client): @aiohttp_jinja2.template('tmpl.jinja2', status=201) async def func(request): return {'head': 'HEAD', 'text': 'text'} template = '<html><body><h1>{{head}}</h1>{{text}}</body></html>' app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({ 'tmpl.jinja2': template })) app.router.add_route('*', '/', func) client = await aiohttp_client(app) resp = await client.get('/') assert 201 == resp.status txt = await resp.text() assert '<html><body><h1>HEAD</h1>text</body></html>' == txt
Example #13
Source File: test_simple_renderer.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_render_template(aiohttp_client): async def func(request): return aiohttp_jinja2.render_template( 'tmpl.jinja2', request, {'head': 'HEAD', 'text': 'text'}) template = '<html><body><h1>{{head}}</h1>{{text}}</body></html>' app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({ 'tmpl.jinja2': template })) app.router.add_route('*', '/', func) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert '<html><body><h1>HEAD</h1>text</body></html>' == txt
Example #14
Source File: test_simple_renderer.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_template_not_found(): async def func(request): return aiohttp_jinja2.render_template('template', request, {}) app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({})) app.router.add_route('GET', '/', func) req = make_mocked_request('GET', '/', app=app) with pytest.raises(web.HTTPInternalServerError) as ctx: await func(req) t = "Template 'template' not found" assert t == ctx.value.text assert t == ctx.value.reason
Example #15
Source File: main.py From aiohttp_admin with Apache License 2.0 | 6 votes |
def init(loop): # load config from yaml file conf = load_config(str(PROJ_ROOT / 'config' / 'dev.yml')) # setup application and extensions app = web.Application(loop=loop) pg = await setup_pg(app, conf, loop) # init modules aiohttp_jinja2.setup( app, loader=jinja2.FileSystemLoader(str(TEMPLATES_ROOT)) ) setup_admin(app, pg) # setup views and routes handler = SiteHandler(pg) add_route = app.router.add_route add_route('GET', '/', handler.index) app.router.add_static('/static', path=str(PROJ_ROOT / 'static')) host, port = conf['host'], conf['port'] return app, host, port
Example #16
Source File: server.py From snare with GNU General Public License v3.0 | 6 votes |
def start(self): app = web.Application() app.add_routes([web.route('*', '/{tail:.*}', self.handle_request)]) aiohttp_jinja2.setup( app, loader=jinja2.FileSystemLoader(self.dir) ) middleware = SnareMiddleware( error_404=self.meta['/status_404'].get('hash'), headers=self.meta['/status_404'].get('headers', []), server_header=self.run_args.server_header ) middleware.setup_middlewares(app) self.runner = web.AppRunner(app) await self.runner.setup() site = web.TCPSite( self.runner, self.run_args.host_ip, self.run_args.port) await site.start() names = sorted(str(s.name) for s in self.runner.sites) print("======== Running on {} ========\n" "(Press CTRL+C to quit)".format(', '.join(names)))
Example #17
Source File: confserver.py From bumper with GNU General Public License v3.0 | 6 votes |
def start_site(self, app, address='localhost', port=8080, usessl=False): runner = web.AppRunner(app) self.runners.append(runner) await runner.setup() if usessl: ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) ssl_ctx.load_cert_chain(bumper.server_cert, bumper.server_key) site = web.TCPSite( runner, host=address, port=port, ssl_context=ssl_ctx, ) else: site = web.TCPSite( runner, host=address, port=port ) await site.start()
Example #18
Source File: __init__.py From aiohttp_admin with Apache License 2.0 | 6 votes |
def setup(app, admin_conf_path, *, resources, static_folder=None, template_folder=None, template_name=None, name=None, app_key=APP_KEY): admin = web.Application(loop=app.loop) app[app_key] = admin tf = gather_template_folders(template_folder) loader = jinja2.FileSystemLoader(tf) aiohttp_jinja2.setup(admin, loader=loader, app_key=TEMPLATE_APP_KEY) template_name = template_name or 'admin.html' admin_handler = AdminHandler(admin, resources=resources, name=name, template=template_name, loop=app.loop) admin['admin_handler'] = admin_handler admin['layout_path'] = admin_conf_path static_folder = static_folder or str(PROJ_ROOT / 'static') setup_admin_handlers(admin, admin_handler, static_folder, admin_conf_path) return admin
Example #19
Source File: main.py From aiohttp_admin with Apache License 2.0 | 6 votes |
def init(loop): conf = load_config(str(PROJ_ROOT / 'config' / 'config.yml')) app = web.Application(loop=loop) cookie_storage = SimpleCookieStorage() app = web.Application(middlewares=[session_middleware(cookie_storage)]) mongo = await setup_mongo(app, conf, loop) setup_jinja(app) admin = setup_admin(app, mongo) app.add_subapp('/admin', admin) app.router.add_static('/static', path=str(PROJ_ROOT / 'static')) # setup views and routes handler = SiteHandler(mongo) setup_routes(app, handler, PROJ_ROOT) host, port = conf['host'], conf['port'] return app, host, port
Example #20
Source File: main.py From aiohttp_admin with Apache License 2.0 | 6 votes |
def init(loop): # load config from yaml file conf = load_config(str(PROJ_ROOT / 'config' / 'dev.yml')) # setup application and extensions app = web.Application(loop=loop) pg = await setup_pg(app, conf, loop) # init modules aiohttp_jinja2.setup( app, loader=jinja2.FileSystemLoader(str(TEMPLATES_ROOT))) admin = setup_admin(app, pg) app.add_subapp('/admin/', admin) # setup views and routes handler = SiteHandler(pg) add_route = app.router.add_route add_route('GET', '/', handler.index) app.router.add_static('/static', path=str(PROJ_ROOT / 'static')) host, port = conf['host'], conf['port'] return app, host, port
Example #21
Source File: test_jinja_filters.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_jinja_filters(aiohttp_client): @aiohttp_jinja2.template('tmpl.jinja2') async def index(request): return {} def add_2(value): return value + 2 app = web.Application() aiohttp_jinja2.setup( app, loader=jinja2.DictLoader({'tmpl.jinja2': "{{ 5|add_2 }}"}), filters={'add_2': add_2} ) app.router.add_route('GET', '/', index) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert '7' == txt
Example #22
Source File: test_simple_renderer.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_skip_render_for_response_from_handler(aiohttp_client): @aiohttp_jinja2.template('tmpl.jinja2') async def func(request): return web.Response(text='OK') app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader( {'tmpl.jinja2': '{{text}}'})) app.router.add_route('GET', '/', func) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert 'OK' == txt
Example #23
Source File: test_simple_renderer.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_render_can_disable_autoescape(aiohttp_client): @aiohttp_jinja2.template('tmpl.jinja2') async def func(request): return {'text': '<script>alert(1)</script>'} app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader( {'tmpl.jinja2': '<html>{{text}}</html>'}), autoescape=False) app.router.add_route('GET', '/', func) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert '<html><script>alert(1)</script></html>' == txt
Example #24
Source File: test_simple_renderer.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_render_default_is_autoescaped(aiohttp_client): @aiohttp_jinja2.template('tmpl.jinja2') async def func(request): return {'text': '<script>alert(1)</script>'} app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader( {'tmpl.jinja2': '<html>{{text}}</html>'})) app.router.add_route('GET', '/', func) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert '<html><script>alert(1)</script></html>' == txt
Example #25
Source File: test_simple_renderer.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_render_not_mapping(): @aiohttp_jinja2.template('tmpl.jinja2') async def func(request): return 123 app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader( {'tmpl.jinja2': 'tmpl'})) app.router.add_route('GET', '/', func) req = make_mocked_request('GET', '/', app=app) msg = "context should be mapping, not <class 'int'>" with pytest.raises(web.HTTPInternalServerError) as ctx: await func(req) assert msg == ctx.value.text
Example #26
Source File: test_simple_renderer.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_render_template_custom_status(aiohttp_client): async def func(request): return aiohttp_jinja2.render_template( 'tmpl.jinja2', request, {'head': 'HEAD', 'text': 'text'}, status=404) template = '<html><body><h1>{{head}}</h1>{{text}}</body></html>' app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({ 'tmpl.jinja2': template })) app.router.add_route('*', '/', func) client = await aiohttp_client(app) resp = await client.get('/') assert 404 == resp.status txt = await resp.text() assert '<html><body><h1>HEAD</h1>text</body></html>' == txt
Example #27
Source File: app.py From aiohttp-login with ISC License | 5 votes |
def create_app(loop): app = web.Application(loop=loop, debug=settings.DEBUG) setup_jinja(app, settings.DEBUG) aiohttp_session.setup(app, EncryptedCookieStorage( settings.SESSION_SECRET.encode('utf-8'), max_age=settings.SESSION_MAX_AGE)) app.middlewares.append(aiohttp_login.flash.middleware) app.router.add_get('/', handlers.index) app.router.add_get('/users/', handlers.users, name='users') app['db'] = await asyncpg.create_pool(dsn=settings.DATABASE, loop=loop) aiohttp_login.setup(app, AsyncpgStorage(app['db']), settings.AUTH) return app
Example #28
Source File: base_app.py From proxy_py with GNU General Public License v3.0 | 5 votes |
def __init__(self, logger=None): self.logger = logger self._app = web.Application() aiohttp_jinja2.setup(self.app, loader=jinja2.FileSystemLoader( settings.TEMPLATES_PATH ))
Example #29
Source File: http.py From LedFx with MIT License | 5 votes |
def __init__(self, ledfx, host, port): """Initialize the HTTP server""" self.app = web.Application(loop=ledfx.loop) self.api = RestApi(ledfx) aiohttp_jinja2.setup( self.app, loader=jinja2.PackageLoader('ledfx_frontend', '.')) self.register_routes() self._ledfx = ledfx self.host = host self.port = port
Example #30
Source File: core.py From friendly-telegram with GNU Affero General Public License v3.0 | 5 votes |
def start(self): self.runner = web.AppRunner(self.app) await self.runner.setup() self.port = os.environ.get("PORT", 8080) site = web.TCPSite(self.runner, None, self.port) await site.start() self.running.set()