home / content / repos

repos: 221802296

This data as json

id node_id name full_name private owner html_url description fork created_at updated_at pushed_at homepage size stargazers_count watchers_count language has_issues has_projects has_downloads has_wiki has_pages forks_count archived disabled open_issues_count license topics forks open_issues watchers default_branch permissions temp_clone_token organization network_count subscribers_count readme readme_html allow_forking visibility is_template template_repository web_commit_signoff_required has_discussions
221802296 MDEwOlJlcG9zaXRvcnkyMjE4MDIyOTY= datasette-template-sql simonw/datasette-template-sql 0 9599 https://github.com/simonw/datasette-template-sql Datasette plugin for executing SQL queries from templates 0 2019-11-14T23:05:34Z 2021-05-18T17:58:47Z 2021-05-18T17:58:44Z https://datasette.io/plugins/datasette-template-sql 23 6 6 Python 1 1 1 1 0 0 0 0 1 apache-2.0 ["datasette", "datasette-plugin", "datasette-io"] 0 1 6 main {"admin": false, "push": false, "pull": false}     0 1 # datasette-template-sql [![PyPI](https://img.shields.io/pypi/v/datasette-template-sql.svg)](https://pypi.org/project/datasette-template-sql/) [![Changelog](https://img.shields.io/github/v/release/simonw/datasette-template-sql?include_prereleases&label=changelog)](https://github.com/simonw/datasette-template-sql/releases) [![Tests](https://github.com/simonw/datasette-template-sql/workflows/Test/badge.svg)](https://github.com/simonw/datasette-template-sql/actions?query=workflow%3ATest) [![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/simonw/datasette-template-sql/blob/main/LICENSE) Datasette plugin for executing SQL queries from templates. ## Examples [datasette.io](https://datasette.io/) uses this plugin extensively with [custom page templates](https://docs.datasette.io/en/stable/custom_templates.html#custom-pages), check out [simonw/datasette.io](https://github.com/simonw/datasette.io) to see how it works. [www.niche-museums.com](https://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](https://github.com/simonw/museums) - see also [niche-museums.com, powered by Datasette](https://simonwillison.net/2019/Nov/25/niche-museums/). [simonw/til](https://github.com/simonw/til) is another simple example, described in [Using a self-rewriting README powered by GitHub Actions to track TILs](https://simonwillison.net/2020/Apr/20/self-rewriting-readme/). ## 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: ```html+jinja {% 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: ```html+jinja {% 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: ```html+jinja {% 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: ```html+jinja {% 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 %} ``` <div id="readme" class="md" data-path="README.md"><article class="markdown-body entry-content container-lg" itemprop="text"><h1><a id="user-content-datasette-template-sql" class="anchor" aria-hidden="true" href="#user-content-datasette-template-sql"><svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a>datasette-template-sql</h1> <p><a href="https://pypi.org/project/datasette-template-sql/" rel="nofollow"><img src="https://camo.githubusercontent.com/4de02f94922198f24958d43afbaa62802bf36bc44849bdf7bfb630d8bd7d5dde/68747470733a2f2f696d672e736869656c64732e696f2f707970692f762f6461746173657474652d74656d706c6174652d73716c2e737667" alt="PyPI" data-canonical-src="https://img.shields.io/pypi/v/datasette-template-sql.svg" style="max-width:100%;"></a> <a href="https://github.com/simonw/datasette-template-sql/releases"><img src="https://camo.githubusercontent.com/564e7d6e3e049c80e9c446272b50dd5ce9c6d4b573ad51a7f1e9e51756affbcb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f73696d6f6e772f6461746173657474652d74656d706c6174652d73716c3f696e636c7564655f70726572656c6561736573266c6162656c3d6368616e67656c6f67" alt="Changelog" data-canonical-src="https://img.shields.io/github/v/release/simonw/datasette-template-sql?include_prereleases&amp;label=changelog" style="max-width:100%;"></a> <a href="https://github.com/simonw/datasette-template-sql/actions?query=workflow%3ATest"><img src="https://github.com/simonw/datasette-template-sql/workflows/Test/badge.svg" alt="Tests" style="max-width:100%;"></a> <a href="https://github.com/simonw/datasette-template-sql/blob/main/LICENSE"><img src="https://camo.githubusercontent.com/1698104e976c681143eb0841f9675c6f802bb7aa832afc0c7a4e719b1f3cf955/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d417061636865253230322e302d626c75652e737667" alt="License" data-canonical-src="https://img.shields.io/badge/license-Apache%202.0-blue.svg" style="max-width:100%;"></a></p> <p>Datasette plugin for executing SQL queries from templates.</p> <h2><a id="user-content-examples" class="anchor" aria-hidden="true" href="#user-content-examples"><svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a>Examples</h2> <p><a href="https://datasette.io/" rel="nofollow">datasette.io</a> uses this plugin extensively with <a href="https://docs.datasette.io/en/stable/custom_templates.html#custom-pages" rel="nofollow">custom page templates</a>, check out <a href="https://github.com/simonw/datasette.io">simonw/datasette.io</a> to see how it works.</p> <p><a href="https://www.niche-museums.com/" rel="nofollow">www.niche-museums.com</a> uses this plugin to run a custom themed website on top of Datasette. The full source code for the site <a href="https://github.com/simonw/museums">is here</a> - see also <a href="https://simonwillison.net/2019/Nov/25/niche-museums/" rel="nofollow">niche-museums.com, powered by Datasette</a>.</p> <p><a href="https://github.com/simonw/til">simonw/til</a> is another simple example, described in <a href="https://simonwillison.net/2020/Apr/20/self-rewriting-readme/" rel="nofollow">Using a self-rewriting README powered by GitHub Actions to track TILs</a>.</p> <h2><a id="user-content-installation" class="anchor" aria-hidden="true" href="#user-content-installation"><svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a>Installation</h2> <p>Run this command to install the plugin in the same environment as Datasette:</p> <div class="snippet-clipboard-content position-relative" data-snippet-clipboard-copy-content="$ pip install datasette-template-sql "><pre><code>$ pip install datasette-template-sql </code></pre></div> <h2><a id="user-content-usage" class="anchor" aria-hidden="true" href="#user-content-usage"><svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a>Usage</h2> <p>This plugin makes a new function, <code>sql(sql_query)</code>, available to your Datasette templates.</p> <p>You can use it like this:</p> <div class="highlight highlight-text-html-django position-relative" data-snippet-clipboard-copy-content="{% for row in sql(&quot;select 1 + 1 as two, 2 * 4 as eight&quot;) %} {% for key in row.keys() %} {{ key }}: {{ row[key] }}&lt;br&gt; {% endfor %} {% endfor %} "><pre><span class="pl-e">{%</span> <span class="pl-k">for</span> <span class="pl-s">row</span> <span class="pl-k">in</span> <span class="pl-s">sql</span>(<span class="pl-s">"select 1 + 1 as two, 2 * 4 as eight"</span>) <span class="pl-e">%}</span> <span class="pl-e">{%</span> <span class="pl-k">for</span> <span class="pl-s">key</span> <span class="pl-k">in</span> <span class="pl-s">row</span>.<span class="pl-s">keys</span>() <span class="pl-e">%}</span> {{ key }}: {{ row[ke<span class="pl-smi">y]</span> }}&lt;<span class="pl-ent">br</span>&gt; <span class="pl-e">{%</span> <span class="pl-k">endfor</span> <span class="pl-e">%}</span> <span class="pl-e">{%</span> <span class="pl-k">endfor</span> <span class="pl-e">%}</span></pre></div> <p>The plugin will execute SQL against the current database for the page in <code>database.html</code>, <code>table.html</code> and <code>row.html</code> templates. If a template does not have a current database (<code>index.html</code> for example) the query will execute against the first attached database.</p> <h3><a id="user-content-queries-with-arguments" class="anchor" aria-hidden="true" href="#user-content-queries-with-arguments"><svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a>Queries with arguments</h3> <p>You can construct a SQL query using <code>?</code> or <code>:name</code> parameter syntax by passing a list or dictionary as a second argument:</p> <div class="highlight highlight-text-html-django position-relative" data-snippet-clipboard-copy-content="{% for row in sql(&quot;select distinct topic from til order by topic&quot;) %} &lt;h2&gt;{{ row.topic }}&lt;/h2&gt; &lt;ul&gt; {% for til in sql(&quot;select * from til where topic = ?&quot;, [row.topic]) %} &lt;li&gt;&lt;a href=&quot;{{ til.url }}&quot;&gt;{{ til.title }}&lt;/a&gt; - {{ til.created[:10] }}&lt;/li&gt; {% endfor %} &lt;/ul&gt; {% endfor %} "><pre><span class="pl-e">{%</span> <span class="pl-k">for</span> <span class="pl-s">row</span> <span class="pl-k">in</span> <span class="pl-s">sql</span>(<span class="pl-s">"select distinct topic from til order by topic"</span>) <span class="pl-e">%}</span> &lt;<span class="pl-ent">h2</span>&gt;{{ row.topic }}&lt;/<span class="pl-ent">h2</span>&gt; &lt;<span class="pl-ent">ul</span>&gt; <span class="pl-e">{%</span> <span class="pl-k">for</span> <span class="pl-s">til</span> <span class="pl-k">in</span> <span class="pl-s">sql</span>(<span class="pl-s">"select * from til where topic = ?"</span>, [<span class="pl-s">row</span>.<span class="pl-s">topic</span>]) <span class="pl-e">%}</span> &lt;<span class="pl-ent">li</span>&gt;&lt;<span class="pl-ent">a</span> <span class="pl-e">href</span>=<span class="pl-s"><span class="pl-pds">"</span>{{ til.url }}<span class="pl-pds">"</span></span>&gt;{{ til.title }}&lt;/<span class="pl-ent">a</span>&gt; - {{ til.created[:1<span class="pl-smi">0]</span> }}&lt;/<span class="pl-ent">li</span>&gt; <span class="pl-e">{%</span> <span class="pl-k">endfor</span> <span class="pl-e">%}</span> &lt;/<span class="pl-ent">ul</span>&gt; <span class="pl-e">{%</span> <span class="pl-k">endfor</span> <span class="pl-e">%}</span></pre></div> <p>Here's the same example using the <code>:topic</code> style of parameters:</p> <div class="highlight highlight-text-html-django position-relative" data-snippet-clipboard-copy-content="{% for row in sql(&quot;select distinct topic from til order by topic&quot;) %} &lt;h2&gt;{{ row.topic }}&lt;/h2&gt; &lt;ul&gt; {% for til in sql(&quot;select * from til where topic = :topic&quot;, {&quot;topic&quot;: row.topic}) %} &lt;li&gt;&lt;a href=&quot;{{ til.url }}&quot;&gt;{{ til.title }}&lt;/a&gt; - {{ til.created[:10] }}&lt;/li&gt; {% endfor %} &lt;/ul&gt; {% endfor %} "><pre><span class="pl-e">{%</span> <span class="pl-k">for</span> <span class="pl-s">row</span> <span class="pl-k">in</span> <span class="pl-s">sql</span>(<span class="pl-s">"select distinct topic from til order by topic"</span>) <span class="pl-e">%}</span> &lt;<span class="pl-ent">h2</span>&gt;{{ row.topic }}&lt;/<span class="pl-ent">h2</span>&gt; &lt;<span class="pl-ent">ul</span>&gt; <span class="pl-e">{%</span> <span class="pl-k">for</span> <span class="pl-s">til</span> <span class="pl-k">in</span> <span class="pl-s">sql</span>(<span class="pl-s">"select * from til where topic = :topic"</span>, {<span class="pl-s">"topic"</span>: <span class="pl-s">row</span>.<span class="pl-s">topic</span>}) <span class="pl-e">%}</span> &lt;<span class="pl-ent">li</span>&gt;&lt;<span class="pl-ent">a</span> <span class="pl-e">href</span>=<span class="pl-s"><span class="pl-pds">"</span>{{ til.url }}<span class="pl-pds">"</span></span>&gt;{{ til.title }}&lt;/<span class="pl-ent">a</span>&gt; - {{ til.created[:1<span class="pl-smi">0]</span> }}&lt;/<span class="pl-ent">li</span>&gt; <span class="pl-e">{%</span> <span class="pl-k">endfor</span> <span class="pl-e">%}</span> &lt;/<span class="pl-ent">ul</span>&gt; <span class="pl-e">{%</span> <span class="pl-k">endfor</span> <span class="pl-e">%}</span></pre></div> <h3><a id="user-content-querying-a-different-database" class="anchor" aria-hidden="true" href="#user-content-querying-a-different-database"><svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a>Querying a different database</h3> <p>You can pass an optional <code>database=</code> argument to specify a named database to use for the query. For example, if you have attached a <code>news.db</code> database you could use this:</p> <div class="highlight highlight-text-html-django position-relative" data-snippet-clipboard-copy-content="{% for article in sql( &quot;select headline, date, summary from articles order by date desc limit 5&quot;, database=&quot;news&quot; ) %} &lt;h3&gt;{{ article.headline }}&lt;/h2&gt; &lt;p class=&quot;date&quot;&gt;{{ article.date }}&lt;/p&gt; &lt;p&gt;{{ article.summary }}&lt;/p&gt; {% endfor %} "><pre><span class="pl-e">{%</span> <span class="pl-k">for</span> <span class="pl-s">article</span> <span class="pl-k">in</span> <span class="pl-s">sql</span>( <span class="pl-s">"select headline, date, summary from articles order by date desc limit 5"</span>, <span class="pl-s">database</span>=<span class="pl-s">"news"</span> ) <span class="pl-e">%}</span> &lt;<span class="pl-ent">h3</span>&gt;{{ article.headline }}&lt;/<span class="pl-ent">h2</span>&gt; &lt;<span class="pl-ent">p</span> <span class="pl-e">class</span>=<span class="pl-s"><span class="pl-pds">"</span>date<span class="pl-pds">"</span></span>&gt;{{ article.date }}&lt;/<span class="pl-ent">p</span>&gt; &lt;<span class="pl-ent">p</span>&gt;{{ article.summary }}&lt;/<span class="pl-ent">p</span>&gt; <span class="pl-e">{%</span> <span class="pl-k">endfor</span> <span class="pl-e">%}</span></pre></div> </article></div>            

Links from other tables

  • 4 rows from repo in releases
Powered by Datasette · Queries took 1.483ms