{ "info": { "author": "Ryan Northey", "author_email": "ryan@3ca.org.uk", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Intended Audience :: Developers", "Programming Language :: Python :: 3.4", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "Detailed documentation\n**********************\n\naio.web.page\n============\n\nWeb page templates for the aio_ asyncio framework\n\n.. _aio: https://github.com/phlax/aio\n\n\n\nBuild status\n------------\n\n.. image:: https://travis-ci.org/phlax/aio.web.page.svg?branch=master\n\t :target: https://travis-ci.org/phlax/aio.web.page\n\n\nInstallation\n------------\n\nRequires python >= 3.4\n\nInstall with:\n\n.. code:: bash\n\n\t pip install aio.web.page\n\n\nQuick start - hello world web page\n----------------------------------\n\nSave the following into a file \"hello.conf\"\n\n.. code:: ini\n\n\t [aio]\n\t modules = aio.web.server\n\n\t [server/my_server]\n\t factory = aio.web.server.factory\n\t port = 8080\n\n\t [web/my_server]\n\t template_dirs = templates\n\t \n\t [web/my_server/my_route]\n\t match = /\n\t route = my_example.route_handler\n\n\nAnd save the following into a file named \"my_example.py\"\n\n.. code:: python\n\n\t import aio.web.page\t \n\t import aio.web.server\n\n\t @aio.web.page.template('example_page.html')\n\t def template_handler(request):\n\t return {\"message\": \"Hello template world\"}\t \n\t \n\t @aio.web.server.route\n\t def route_handler(request, config):\n\t return (yield from template_handler(request))\n\n\nAnd the following into a file named \"templates/example_page.html\"\n\n.. code:: html\n\t \n\t \n\t
\n\t {{ message }}\n\t \n\t \n\t \nRun with the aio run command\n\n.. code:: bash\n\n\t aio run -c hello.conf\n\n\n\naio.web.page usage\n------------------\n\naio.web.page provides templates and fragments for building web pages\n\nLets set up a test to run a server and request a web page\n\n>>> from aio.app.runner import runner \n>>> import aio.testing\n>>> import aiohttp \n\n>>> @aio.testing.run_forever(sleep=1)\n... def run_web_server(config, request_page=\"http://localhost:7070\"):\n... yield from runner(['run'], config_string=config)\n... \n... def call_web_server():\n... result = yield from (\n... yield from aiohttp.request(\n... \"GET\", request_page)).read()\n... aio.web.server.clear()\n... \n... print(result.decode())\n... \n... return call_web_server\n\n\nTemplates\n---------\n \nAn @aio.web.server.route handler can defer to other templates, for example according to the matched path.\n\n>>> example_config = \"\"\"\n... [aio]\n... log_level = CRITICAL\n... modules = aio.web.server\n... aio.web.server.tests \n... \n... [server/server_name]\n... factory: aio.web.server.factory\n... port: 7070\n... \n... [web/server_name/route_name]\n... match = /{path:.*}\n... route = aio.web.page.tests._example_route_handler\n... \"\"\"\n\nLets create a couple of template handlers\n\n>>> import aio.web.page\n\n>>> @aio.web.page.template(\"test_template.html\") \n... def template_handler_1(request): \n... return {\n... 'message': \"Hello, world from template handler 1\"}\n\nTemplate handlers can return a response object, in which case the template is not rendered\n \n>>> @aio.web.page.template(\"test_template.html\")\n... def template_handler_2(request):\n... return aiohttp.web.Response(\n... body=b\"Hello, world from template handler 2\")\n\n\nAnd lets set up a route handler which will defer to a template accordingly\n\n>>> import aio.web.server\n\n>>> @aio.web.server.route\n... def route_handler(request, config):\n... path = request.match_info['path']\n... \n... if path == \"path1\":\n... return (yield from template_handler_1(request))\n... \n... elif path == \"path2\":\n... return (yield from template_handler_2(request))\n... \n... raise aiohttp.web.HTTPNotFound\n\nAnd make it importable\n \n>>> import aio.web.page.tests\n>>> aio.web.page.tests._example_route_handler = route_handler\n\nCalling the server at /path1 we get the templated handler\n \n>>> run_web_server(\n... example_config,\n... request_page=\"http://localhost:7070/path1\") \n\n \n Hello, world from template handler 1\n \n\n\nAnd calling on /path2 we get the response without the template\n \n>>> run_web_server(\n... example_config,\n... request_page=\"http://localhost:7070/path2\") \nHello, world from template handler 2\n\n\nTemplates must always specify a template, even if they dont use it\n\n>>> try:\n... @aio.web.page.template\n... def template_handler(request, test_list): \n... return {'test_list': test_list}\n... except Exception as e:\n... print(repr(e))\nTypeError('Template decorator must specify template: