Datasette plugin for rendering HTML based on JSON values
Project description
datasette-json-html
Datasette plugin for rendering HTML based on JSON values, using the new render_cell
plugin hook only available in Datasette master - see issue 352 for details.
This plugin looks for Database values that match a very specific JSON format and converts them into HTML when they are rendered by the Datasette interface.
See russian-ira-facebook-ads-datasette for an example of this plugin in action.
It currently supports three formats.
Links
{
"href": "https://simonwillison.net/",
"label": "Simon Willison"
}
Will be rendered as an <a href="">
link:
<a href="https://simonwillison.net/">Simon Willison</a>
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:
).
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>
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 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file datasette_json_html-0.3.1-py3-none-any.whl
.
File metadata
- Download URL: datasette_json_html-0.3.1-py3-none-any.whl
- Upload date:
- Size: 3.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2bef3c6cdefd89abc2de5cc07e3276c88897ab691b695e67ebfc9c2dfba9e506 |
|
MD5 | 050b6623807ea067468425522a8a32d3 |
|
BLAKE2b-256 | f8e7d11d7980e8220a1603880a3b5b11fc438bf8f0a2dbd384c8d4169f561d4d |