Render templates using Jinja async mode

Closes #628
This commit is contained in:
Simon Willison 2019-11-14 15:14:22 -08:00 committed by GitHub
commit 8c642f04e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 15 deletions

View file

@ -446,13 +446,19 @@ def render_cell(value, database):
@hookimpl
def extra_template_vars(template, database, table, view_name, request, datasette):
async def query_database(sql):
first_db = list(datasette.databases.keys())[0]
return (
await datasette.execute(first_db, sql)
).rows[0][0]
async def inner():
return {
"extra_template_vars_from_awaitable": json.dumps({
"template": template,
"scope_path": request.scope["path"],
"awaitable": True,
}, default=lambda b: b.decode("utf8"))
}, default=lambda b: b.decode("utf8")),
"query_database": query_database,
}
return inner