From 0e8f8be1dbf89a6f689a5eea4a0534199a0028f6 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Thu, 14 Nov 2019 15:12:34 -0800 Subject: [PATCH] Better async template function example --- docs/plugins.rst | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/docs/plugins.rst b/docs/plugins.rst index 9704370c..e5a3d7dd 100644 --- a/docs/plugins.rst +++ b/docs/plugins.rst @@ -643,26 +643,19 @@ Here's an example plugin that returns an authentication object from the ASGI sco "auth": request.scope.get("auth") } -And here's an example which adds a ``query_database(sql)`` function which executes a SQL statement and returns the first column of the row: +And here's an example which adds a ``sql_first(sql_query)`` function which executes a SQL statement and returns the first column of the first row of results: .. code-block:: python @hookimpl - def extra_template_vars(datasette): - - async def query_database(sql): - first_db = list(datasette.databases.keys())[0] - return ( - await datasette.execute(first_db, sql) - ).rows[0][0] - - return { - "query_database": query_database, - } + def extra_template_vars(datasette, database): + async def sql_first(sql, dbname=None): + dbname = dbname or database or next(iter(datasette.databases.keys())) + return (await datasette.execute(dbname, sql)).rows[0][0] You can then use the new function in a template like so:: - {{ query_database("select sqlite_version()") }} + SQLite version: {{ sql_first("select sqlite_version()") }} .. _plugin_register_output_renderer: