Skip to main content

Datasette plugin for executing SQL queries from templates

Project description

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 %}

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

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

datasette_template_sql-0.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file datasette_template_sql-0.1-py3-none-any.whl.

File metadata

  • Download URL: datasette_template_sql-0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.6.9

File hashes

Hashes for datasette_template_sql-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 22394a1ba13d62b7db7b64b766ea57616f4431a6868cb2a32a5f9cfc5c07cb60
MD5 cfc7c24d6a4f6e2ef9a246c0d4e717e8
BLAKE2b-256 237a607305faced4ec8b412449aaf97fbcf63ec4f345bdfa4f4259ae1ab464dc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page