Datasette plugin for executing SQL queries from templates
Project description
datasette-template-sql
Datasette plugin for executing SQL queries from templates.
This plugin makes a new function, sql(sql_query)
, available to your Datasette templates.
You can use it like this:
{% for row in sql("select 1 + 1 as two, 2 * 4 as eight") %}
{% for key in row.keys() %}
{{ key }}: {{ row[key] }}<br>
{% endfor %}
{% endfor %}
The plugin will execute SQL against the current database for the page in database.html
, table.html
and row.html
templates. If a template does not have a current database (index.html
for example) the query will execute against the first attached database.
You can pass an optional second argument to specify a named database to use for the query. For example, if you have attached a news.db
database you could use this:
{% for article in sql(
"select headline, date, summary from articles order by date desc limit 5",
"news"
) %}
<h3>{{ article.headline }}</h2>
<p class="date">{{ article.date }}</p>
<p>{{ article.summary }}</p>
{% endfor %}
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for datasette_template_sql-0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22394a1ba13d62b7db7b64b766ea57616f4431a6868cb2a32a5f9cfc5c07cb60 |
|
MD5 | cfc7c24d6a4f6e2ef9a246c0d4e717e8 |
|
BLAKE2b-256 | 237a607305faced4ec8b412449aaf97fbcf63ec4f345bdfa4f4259ae1ab464dc |