Skip to content
Datasette plugin for executing SQL queries from templates
Python HTML
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci Install .[test] dependencies Nov 14, 2019
datasette_template_sql Tweaked database selection code Nov 14, 2019
tests First working version Nov 14, 2019
.gitignore First working version Nov 14, 2019
LICENSE First working version Nov 14, 2019
README.md First working version Nov 14, 2019
setup.py Needs at least Datasette 0.32 Nov 14, 2019

README.md

datasette-template-sql

PyPI CircleCI License

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 %}
You can’t perform that action at this time.