mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Use Jinja async mode, refs #628
This commit is contained in:
parent
b51f258d00
commit
7eb2be41df
5 changed files with 31 additions and 4 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
from bs4 import BeautifulSoup as Soup
|
||||
from .fixtures import app_client, make_app_client, TEMP_PLUGIN_SECRET_FILE # noqa
|
||||
from datasette.utils import sqlite3
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
|
|
@ -214,3 +215,20 @@ def test_plugins_extra_template_vars(restore_working_directory):
|
|||
"awaitable": True,
|
||||
"scope_path": "/-/metadata",
|
||||
} == extra_template_vars_from_awaitable
|
||||
|
||||
|
||||
def test_plugins_async_template_function(restore_working_directory):
|
||||
for client in make_app_client(
|
||||
template_dir=str(pathlib.Path(__file__).parent / "test_templates")
|
||||
):
|
||||
response = client.get("/-/metadata")
|
||||
assert response.status == 200
|
||||
extra_from_awaitable_function = (
|
||||
Soup(response.body, "html.parser")
|
||||
.select("pre.extra_from_awaitable_function")[0]
|
||||
.text
|
||||
)
|
||||
expected = (
|
||||
sqlite3.connect(":memory:").execute("select sqlite_version()").fetchone()[0]
|
||||
)
|
||||
assert expected == extra_from_awaitable_function
|
||||
|
|
|
|||
|
|
@ -5,4 +5,5 @@
|
|||
Test data for extra_template_vars:
|
||||
<pre class="extra_template_vars">{{ extra_template_vars|safe }}</pre>
|
||||
<pre class="extra_template_vars_from_awaitable">{{ extra_template_vars_from_awaitable|safe }}</pre>
|
||||
<pre class="extra_from_awaitable_function">{{ query_database("select sqlite_version();") }}</pre>
|
||||
{% endblock %}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue