Skip to main content

Datasette plugin for rendering HTML based on JSON values

Project description

datasette-json-html

PyPI Changelog Tests License

Datasette plugin for rendering HTML based on JSON values, using the render_cell plugin hook.

This plugin looks for cell values that match a very specific JSON format and converts them into HTML when they are rendered by the Datasette interface.

Links

{
    "href": "https://simonwillison.net/",
    "label": "Simon Willison"
}

Will be rendered as an <a href=""> link:

<a href="https://simonwillison.net/">Simon Willison</a>

You can set a tooltip on the link using a "title" key:

{
    "href": "https://simonwillison.net/",
    "label": "Simon Willison",
    "title": "My blog"
}

Produces:

<a href="https://simonwillison.net/" title="My blog">Simon Willison</a>

You can also include a description, which will be displayed below the link. If descriptions include newlines they will be converted to <br> elements:

select json_object(
    "href", "https://simonwillison.net/",
    "label", "Simon Willison",
    "description", "This can contain" || x'0a' || "newlines"
)

Produces:

<strong><a href="https://simonwillison.net/">Simon Willison</a></strong><br>This can contain<br>newlines

List of links

[
    {
        "href": "https://simonwillison.net/",
        "label": "Simon Willison"
    },
    {
        "href": "https://github.com/simonw/datasette",
        "label": "Datasette"
    }
]

Will be rendered as a comma-separated list of <a href=""> links:

<a href="https://simonwillison.net/">Simon Willison</a>,
<a href="https://github.com/simonw/datasette">Datasette</a>

The href property must begin with https:// or http:// or /, to avoid potential XSS injection attacks (for example URLs that begin with javascript:).

Lists of links cannot include "description" keys.

Images

The image tag is more complex. The most basic version looks like this:

{
    "img_src": "https://placekitten.com/200/300"
}

This will render as:

<img src="https://placekitten.com/200/300">

But you can also include one or more of alt, caption, width and href.

If you include width or alt, they will be added as attributes:

{
    "img_src": "https://placekitten.com/200/300",
    "alt": "Kitten",
    "width": 200
}

Produces:

<img src="https://placekitten.com/200/300"
    alt="Kitten" width="200">

The href key will cause the image to be wrapped in a link:

{
    "img_src": "https://placekitten.com/200/300",
    "href": "http://www.example.com"
}

Produces:

<a href="http://www.example.com">
    <img src="https://placekitten.com/200/300">
</a>

The caption key wraps everything in a fancy figure/figcaption block:

{
    "img_src": "https://placekitten.com/200/300",
    "caption": "Kitten caption"
}

Produces:

<figure>
    <img src="https://placekitten.com/200/300"></a>
    <figcaption>Kitten caption</figcaption>
</figure>

Preformatted text

You can use {"pre": "text"} to render text in a <pre> HTML tag:

{
    "pre": "This\nhas\nnewlines"
}

Produces:

<pre>This
has
newlines</pre>

If the value attached to the "pre" key is itself a JSON object, that JSON will be pretty-printed:

{
    "pre": {
        "this": {
            "object": ["is", "nested"]
        }
    }
}

Produces:

<pre>{
  &#34;this&#34;: {
    &#34;object&#34;: [
      &#34;is&#34;,
      &#34;nested&#34;
    ]
  }
}</pre>

Using these with SQLite JSON functions

The most powerful way to make use of this plugin is in conjunction with SQLite's JSON functions. For example:

select json_object(
    "href", "https://simonwillison.net/",
    "label", "Simon Willison"
);

You can use these functions to construct JSON objects that work with the plugin from data in a table:

select id, json_object(
    "href", url, "label", text
) from mytable;

The json_group_array() function is an aggregate function similar to group_concat() - it allows you to construct lists of JSON objects in conjunction with a GROUP BY clause.

This means you can use it to construct dynamic lists of links, for example:

select
    substr(package, 0, 12) as prefix,
    json_group_array(
        json_object(
            "href", url,
            "label", package
        )
    ) as package_links
from packages
group by prefix

The urllib_quote_plus() SQL function

Since this plugin is designed to be used with SQL that constructs the underlying JSON structure, it is likely you will need to construct dynamic URLs from results returned by a SQL query.

This plugin registers a custom SQLite function called urllib_quote_plus() to help you do that. It lets you use Python's urllib.parse.quote_plus() function from within a SQL query.

Here's an example of how you might use it:

select id, json_object(
    "href",
    "/mydatabase/other_table?_search=" || urllib_quote_plus(text),
    "label", text
) from mytable;

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

datasette-json-html-1.0.1.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

datasette_json_html-1.0.1-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file datasette-json-html-1.0.1.tar.gz.

File metadata

  • Download URL: datasette-json-html-1.0.1.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

File hashes

Hashes for datasette-json-html-1.0.1.tar.gz
Algorithm Hash digest
SHA256 9b23bd0eb685df0c5a72fabda8e3be6695e4f23019f340cc4f5ddf57ba837480
MD5 0ad60513e6f788c3ddd25a4c251d78f4
BLAKE2b-256 9545a7f5e828dd6b249e9d0214d11d789350fd8a24f12b08b878c05416c5e08b

See more details on using hashes here.

File details

Details for the file datasette_json_html-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: datasette_json_html-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

File hashes

Hashes for datasette_json_html-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a95291cfa4c6e80f363156408ecf87d8bfcf432f42c3f440f1128e6f55a4e066
MD5 13146bc70fa9085c3c4cb89501c56afc
BLAKE2b-256 4a677f502c77f3459946b08a1f856d220b81c5066a5dcdf4e7094741579ce463

See more details on using hashes here.

Supported by

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