为aio asyncio框架提供的网页模板
项目描述
为aio asyncio框架提供的网页模板
构建状态
安装
需要 python >= 3.4
安装方法
pip install aio.web.page
快速入门 - “Hello World”网页
将以下内容保存到文件“hello.conf”中
[aio]
modules = aio.web.server
[server/my_server]
factory = aio.web.server.factory
port = 8080
[web/my_server]
template_dirs = templates
[web/my_server/my_route]
match = /
route = my_example.route_handler
将以下内容保存到名为“my_example.py”的文件中
import aio.web.page
import aio.web.server
@aio.web.page.template('example_page.html')
def template_handler(request):
return {"message": "Hello template world"}
@aio.web.server.route
def route_handler(request, config):
return (yield from template_handler(request))
将以下内容保存到名为“templates/example_page.html”的文件中
<html>
<body>
{{ message }}
</body>
</html>
使用aio run命令运行
aio run -c hello.conf
aio.web.page 使用方法
aio.web.page 提供了构建网页的模板和片段
让我们设置一个测试来运行服务器并请求一个网页
>>> from aio.app.runner import runner >>> import aio.testing >>> import aiohttp
>>> @aio.testing.run_forever(sleep=1) ... def run_web_server(config, request_page="https://127.0.0.1:7070"): ... yield from runner(['run'], config_string=config) ... ... def call_web_server(): ... result = yield from ( ... yield from aiohttp.request( ... "GET", request_page)).read() ... aio.web.server.clear() ... ... print(result.decode()) ... ... return call_web_server
模板
@aio.web.server.route处理程序可以延迟到其他模板,例如根据匹配的路径。
>>> example_config = """ ... [aio] ... log_level = CRITICAL ... modules = aio.web.server ... aio.web.server.tests ... ... [server/server_name] ... factory: aio.web.server.factory ... port: 7070 ... ... [web/server_name/route_name] ... match = /{path:.*} ... route = aio.web.page.tests._example_route_handler ... """
让我们创建几个模板处理程序
>>> import aio.web.page
>>> @aio.web.page.template("test_template.html") ... def template_handler_1(request): ... return { ... 'message': "Hello, world from template handler 1"}
如果模板处理程序返回一个响应对象,则模板不会被渲染
>>> @aio.web.page.template("test_template.html") ... def template_handler_2(request): ... return aiohttp.web.Response( ... body=b"Hello, world from template handler 2")
并设置一个将延迟到相应模板的路由处理程序
>>> import aio.web.server
>>> @aio.web.server.route ... def route_handler(request, config): ... path = request.match_info['path'] ... ... if path == "path1": ... return (yield from template_handler_1(request)) ... ... elif path == "path2": ... return (yield from template_handler_2(request)) ... ... raise aiohttp.web.HTTPNotFound
并使其可导入
>>> import aio.web.page.tests >>> aio.web.page.tests._example_route_handler = route_handler
在/path1路径上调用服务器时,我们得到模板处理程序
>>> run_web_server( ... example_config, ... request_page="https://127.0.0.1:7070/path1") <html> <body> Hello, world from template handler 1 </body> </html>
在/path2路径上调用时,我们得到没有模板的响应
>>> run_web_server( ... example_config, ... request_page="https://127.0.0.1:7070/path2") Hello, world from template handler 2
模板必须始终指定一个模板,即使它们不使用它
>>> try: ... @aio.web.page.template ... def template_handler(request, test_list): ... return {'test_list': test_list} ... except Exception as e: ... print(repr(e)) TypeError('Template decorator must specify template: <function template_handler ...>',)
模板可以接受任意参数
>>> @aio.web.page.template("test_template.html") ... def template_handler(request, foo, bar): ... return { ... 'message': "Hello, world with %s and %s" % (foo, bar)}
>>> @aio.web.server.route ... def route_handler(request, config): ... return (yield from(template_handler(request, "spam", "tuesday"))) >>> aio.web.page.tests._example_route_handler = route_handler
>>> run_web_server(example_config) <html> <body> Hello, world with spam and tuesday </body> </html>
模板的第一个参数始终应该是请求对象
>>> @aio.web.page.template("test_template.html") ... def template_handler(foo, bar): ... return { ... 'message': "Hello, world with %s and %s" % (foo, bar)}
>>> @aio.web.server.route ... def route_handler(request, config): ... try: ... return (yield from(template_handler("spam", "tuesday"))) ... except TypeError as e: ... return aiohttp.web.Response(body=repr(e).encode()) >>> aio.web.page.tests._example_route_handler = route_handler
>>> run_web_server(example_config) TypeError("Template handler (<function template_handler at ...>) should be called with a request object, got: <class 'str'> spam",)
片段
片段渲染用于嵌入到其他模板中的HTML片段。
片段可以指定一个模板并返回一个用于渲染它的上下文对象
片段可以接受任意数量的参数
>>> @aio.web.page.fragment("fragments/test_fragment.html") ... def fragment_handler(request, foo, bar): ... return {"test_list": [foo, bar]}
>>> @aio.web.page.template("test_template.html") ... def template_handler(request): ... return {'message': (yield from fragment_handler(request, "eggs", "thursday"))}
>>> @aio.web.server.route ... def route_handler(request, config): ... return (yield from(template_handler(request))) >>> aio.web.page.tests._example_route_handler = route_handler
>>> run_web_server(example_config) <html> <body> <ul> <li>eggs</li><li>thursday</li> </ul> </body> </html>
片段的第一个参数始终应该是aiohttp.web.Request对象
>>> @aio.web.page.fragment("fragments/test_fragment.html") ... def fragment_handler(foo, bar): ... return {"test_list": [foo, bar]}
>>> @aio.web.page.template("test_template.html") ... def template_handler(request): ... try: ... message = (yield from(fragment_handler("eggs", "thursday"))) ... except Exception as e: ... message = repr(e) ... return {'message': message}
>>> @aio.web.server.route ... def route_handler(request, config): ... return (yield from(template_handler(request))) >>> aio.web.page.tests._example_route_handler = route_handler
>>> run_web_server(example_config) <html> <body> TypeError("Fragment handler (<function fragment_handler ...>) should be called with a request object, got: <class 'str'> eggs",) </body> </html>
片段不需要指定模板
>>> @aio.web.page.fragment ... def fragment_handler(request): ... return "Hello from fragment"
>>> @aio.web.page.template("test_template.html") ... def template_handler(request): ... return {'message': (yield from fragment_handler(request))}
>>> @aio.web.server.route ... def route_handler(request, config): ... return (yield from(template_handler(request))) >>> aio.web.page.tests._example_route_handler = route_handler
>>> run_web_server(example_config) <html> <body> Hello from fragment </body> </html>
如果片段没有指定模板,它必须返回一个字符串
>>> @aio.web.page.fragment ... def fragment_handler(request): ... return {"foo": "bar"}
>>> @aio.web.page.template("test_template.html") ... def template_handler(request): ... try: ... fragment = yield from fragment_handler(request) ... except Exception as e: ... fragment = repr(e) ... return {'message': fragment}
>>> @aio.web.server.route ... def route_handler(request, config): ... return (yield from(template_handler(request))) >>> aio.web.page.tests._example_route_handler = route_handler
>>> run_web_server(example_config) <html> <body> TypeError('Fragment handler (<function fragment_handler at ...>) should specify a template or return a string',) </body> </html>
片段应仅返回字符串或上下文字典,不应返回aiohttp.web.Response对象。
>>> @aio.web.page.fragment("fragments/test_fragment.html") ... def fragment_handler(request): ... return aiohttp.web.Response(body=b"Fragments should not return Response objects")
>>> @aio.web.page.template("test_template.html") ... def template_handler(request): ... try: ... fragment = yield from fragment_handler(request) ... except Exception as e: ... fragment = repr(e) ... return {'message': fragment}
>>> @aio.web.server.route ... def route_handler(request, config): ... return (yield from(template_handler(request))) >>> aio.web.page.tests._example_route_handler = route_handler
>>> run_web_server(example_config) <html> <body> TypeError("Fragment handler (<function fragment_handler ...>) should return a string or context dictionary, got: <class 'aiohttp.web_reqrep.Response'> <Response OK not started>",) </body> </html>
项目详情
关闭
aio.web.page-0.1.0.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | b44ea633143d6eccf30c446a0cb390cdc2af6b5d60ddf72ab061a63e2739899d |
|
MD5 | 2b498a3e3bae317198e0895bf9edc039 |
|
BLAKE2b-256 | 4de5c7caee4398b8d4dbb668a20ccf8476da5b607399138fb7fe36cfec5d4b42 |