Datasette plugin for executing SQL queries from templates
Project description
datasette-template-sql
Datasette plugin for executing SQL queries from templates.
Examples
datasette.io uses this plugin extensively with custom page templates, check out simonw/datasette.io to see how it works.
www.niche-museums.com uses this plugin to run a custom themed website on top of Datasette. The full source code for the site is here - see also niche-museums.com, powered by Datasette.
simonw/til is another simple example, described in Using a self-rewriting README powered by GitHub Actions to track TILs.
Installation
Run this command to install the plugin in the same environment as Datasette:
$ pip install datasette-template-sql
Usage
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.
Queries with arguments
You can construct a SQL query using ? or :name parameter syntax by passing a list or dictionary as a second argument:
{% for row in sql("select distinct topic from til order by topic") %}
<h2>{{ row.topic }}</h2>
<ul>
{% for til in sql("select * from til where topic = ?", [row.topic]) %}
<li><a href="{{ til.url }}">{{ til.title }}</a> - {{ til.created[:10] }}</li>
{% endfor %}
</ul>
{% endfor %}
Here's the same example using the :topic style of parameters:
{% for row in sql("select distinct topic from til order by topic") %}
<h2>{{ row.topic }}</h2>
<ul>
{% for til in sql("select * from til where topic = :topic", {"topic": row.topic}) %}
<li><a href="{{ til.url }}">{{ til.title }}</a> - {{ til.created[:10] }}</li>
{% endfor %}
</ul>
{% endfor %}
Querying a different database
You can pass an optional database= 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",
database="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 Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file datasette_template_sql-1.0.3.tar.gz.
File metadata
- Download URL: datasette_template_sql-1.0.3.tar.gz
- Upload date:
- Size: 2.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c31c96be9cbf10423a55089d3180cf2379debf1c4ac155ba2d95e55be7c02910
|
|
| MD5 |
fe0ca89b39bd2ea2b09eb65749f17f08
|
|
| BLAKE2b-256 |
9a1d2100b87eced750f4ce0643d84b90cfd3c77ceae6a7d5902b4d820062e448
|
Provenance
The following attestation bundles were made for datasette_template_sql-1.0.3.tar.gz:
Publisher:
publish.yml on simonw/datasette-template-sql
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
datasette_template_sql-1.0.3.tar.gz -
Subject digest:
c31c96be9cbf10423a55089d3180cf2379debf1c4ac155ba2d95e55be7c02910 - Sigstore transparency entry: 1256486948
- Sigstore integration time:
-
Permalink:
simonw/datasette-template-sql@70395fb245051df5499e10fba596c255b50c8b74 -
Branch / Tag:
refs/tags/1.0.3 - Owner: https://github.com/simonw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@70395fb245051df5499e10fba596c255b50c8b74 -
Trigger Event:
release
-
Statement type:
File details
Details for the file datasette_template_sql-1.0.3-py3-none-any.whl.
File metadata
- Download URL: datasette_template_sql-1.0.3-py3-none-any.whl
- Upload date:
- Size: 3.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1d3cefb64daec376a0bca9c3baa71f865e61eda380eec26faabeeffd5c180849
|
|
| MD5 |
53fe6e37a715d6bdffd1ef8960423909
|
|
| BLAKE2b-256 |
f20f2ee8a980036a7e2c43677182e064fa7dca1f5c2d08ad8dddd21fea00c781
|
Provenance
The following attestation bundles were made for datasette_template_sql-1.0.3-py3-none-any.whl:
Publisher:
publish.yml on simonw/datasette-template-sql
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
datasette_template_sql-1.0.3-py3-none-any.whl -
Subject digest:
1d3cefb64daec376a0bca9c3baa71f865e61eda380eec26faabeeffd5c180849 - Sigstore transparency entry: 1256487087
- Sigstore integration time:
-
Permalink:
simonw/datasette-template-sql@70395fb245051df5499e10fba596c255b50c8b74 -
Branch / Tag:
refs/tags/1.0.3 - Owner: https://github.com/simonw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@70395fb245051df5499e10fba596c255b50c8b74 -
Trigger Event:
release
-
Statement type: