Here are the examples of the python api aiohttp.web_reqrep.json_response taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
1 Examples
0
Example 1
async def message_data(request):
"""
As an example of aiohttp providing a non-html response, we load the actual messages for the "messages" view above
via ajax using this endpoint to get data. see static/message_display.js for details of rendering.
"""
messages = []
# {% if database.is_none %}
if request.app['message_file'].exists():
# read the message file, process it and populate the "messages" list
with request.app['message_file'].open() as msg_file:
for line in msg_file:
if not line:
# ignore blank lines eg. end of file
continue
# split the line into it constituent parts, see process_form above
username, ts, message = line.split('|', 2)
# parse the datetime string and render it in a more readable format.
ts = '{:%Y-%m-%d %H:%M:%S}'.format(datetime.strptime(ts, '%Y-%m-%dT%H:%M:%S.%f'))
messages.append({'username': username, 'timestamp': ts, 'message': message})
messages.reverse()
# {% elif database.is_postgres_sqlalchemy %}
async with request.app['pg_engine'].acquire() as conn:
async for row in conn.execute(sa_messages.select().order_by(sa_messages.c.timestamp.desc())):
ts = '{:%Y-%m-%d %H:%M:%S}'.format(row.timestamp)
messages.append({'username': row.username, 'timestamp': ts, 'message': row.message})
# {% else %}
# TODO
# {% endif %}
return json_response(messages)