datasette-json-html 作者 simonw

Star

Screenshot of simonw/datasette-json-html

README 源代码

datasette-json-html

PyPI Changelog Tests License

Datasette 插件,用于根据 JSON 值渲染 HTML,使用 render_cell 插件钩子

这个插件查找符合特定 JSON 格式的单元格值,并在 Datasette 界面渲染时将其转换为 HTML。

链接

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

将被渲染为一个 链接:

您可以使用 "title" 键为链接设置工具提示

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

生成

Simon Willison">
Simon Willison

您还可以包含描述,它将显示在链接下方。如果描述包含换行符,它们将被转换为
元素

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

生成

Simon Willison
这可以包含
换行符">
Simon Willison
This can contain
newlines

链接列表

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

将被渲染为一个逗号分隔的 链接列表:

href 属性必须以 https://http:/// 开头,以避免潜在的 XSS 注入攻击(例如以 javascript: 开头的 URL)。

链接列表不能包含 "description" 键。

图片

图片标签更复杂。最基本的版本如下所示

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

这将渲染为

">

但您也可以包含一个或多个 altcaptionwidthhref

如果您包含 width 或 alt,它们将作为属性添加

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

生成

">
Kitten

href 键将导致图片被包裹在一个链接中

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

生成

">

    

caption 键将所有内容包裹在一个美观的 figure/figcaption 块中

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

生成

小猫标题
">
Kitten caption

预格式化文本

您可以使用 {"pre": "text"}

 标签中渲染文本

 HTML tag:

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

Produces:

This
has
newlines

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:

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

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;