基于json值呈现html的dataset插件

datasette-json-html的Python项目详细描述


数据集json html

PyPITravis CILicense

用于基于json值呈现html的datasette插件,使用render_cell plugin hook

此插件查找与非常特定的json格式匹配的单元格值,并在由dataset接口呈现时将其转换为html。

链接

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

将呈现为<a href="">链接:

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

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

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

产生:

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

链接列表

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

将呈现为<a href="">链接的逗号分隔列表:

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

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

图像

图像标记更复杂。最基本的版本如下:

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

这将呈现为:

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

但也可以包含altcaptionwidthhref中的一个或多个。

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

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

产生:

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

href键将使图像包装在链接中:

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

产生:

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

caption键将所有内容包装成一个奇特的图形/figcaption块:

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

产生:

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

预格式化文本

您可以使用{"pre": "text"}<pre>html标记中呈现文本:

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

产生:

<pre>This
has
newlines</pre>

如果附加到"pre"键的值本身是一个json对象,那么该json将被很好地打印出来:

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

产生:

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

将它们与sqlite json函数一起使用

使用这个插件的最强大的方法是与sqlite的JSON functions结合使用。例如:

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

您可以使用这些函数从表中的数据构造与插件一起工作的json对象:

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

json_group_array()函数是一个类似于group_concat()的聚合函数,它允许您结合GROUP BY子句构造json对象列表。

这意味着您可以使用它来构造链接的动态列表,例如:

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

sql函数urllib_quote_plus()

由于此插件设计用于构造底层json结构的sql,因此可能需要根据sql查询返回的结果构造动态url。

这个插件注册了一个名为urllib_quote_plus()的自定义sqlite函数来帮助您实现这一点。它允许您在sql查询中使用python的urllib.parse.quote_plus() function

下面是一个如何使用它的示例:

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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
由于java的原因,maven无法运行代码。lang.NoClassDefFoundError:com/fasterxml/jackson/annotation/JsonMerge   Android项目中的java Creative SDK图像编辑器UI   java如何在Android Studio中使用DataOutputStream上传文件并将其他参数传递到web服务器   java倒计时服务打开时崩溃   java将RubyonRails项目转换为JRubyonRails项目   java我的图库意图是不显示图像?为什么?   java如何在春季启动时跳过mongodb/   java@Autowired在Spring中是如何实现的   甲骨文Akka java。util。同时发生的timeoutexception线程池频繁超时   java maven依赖项对spring启动应用程序有何影响?   java Firestore执行复合查询,未截获事件“已修改”   java ItemStreamException:未能初始化读取器,原因是:IllegalStateException:流已初始化。重新开放前关闭   java将空标记解组到集合的新实例中   使用AspectJ的java新手:无法调用aspect   java查找棋类游戏的所有组合   你为什么要这样做and==与Java中的equals方法不一样吗?   如何对使用JavaUUID的代码进行单元测试?