Code Monkey home page Code Monkey logo

datasette-json-html's Introduction

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;

datasette-json-html's People

Contributors

simonw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

jagarinart

datasette-json-html's Issues

Skip nulls or empty href values in arrays?

Consider this query, with this SQL:

select
  json_array(
    json_object(
      'href',
      'https://simonwillison.net/',
      'label',
      'Simon Willison'
    ),
    case
      when 'hi' = 'hello' then json_object(
        'href',
        'https://www.python.org/',
        'label',
        'Python'
      )
    end,
    json_object(
      'href',
      '',
      'label',
      'Empty href'
    )
  ) as links

The links don't render due to the null as the second item and the empty href value in the third.

I was trying to put together a query that rendered links for column values I had at hand but some may be missing. There's no doubt a way to extend the query to turn the columns into a subquery so I could use json_group_array but that would be a bit more complicated.

Would skipping null items and/or items with empty href values be worthwhile?

Add a workaround for "JSON cannot hold BLOB values" error

If you attempt to do this:

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

But the text column contains certain characters, SQLite throws this error:

JSON cannot hold BLOB values

Maybe provide a utility SQL function to work around this, something like json_string(value)?

Ability to include a multiline description with a link

It would be handy if I could produce something like this:

This is a link
This is a multiline description associated with that link.
It can contain line breaks.

Easiest way to do this would be to add an optional "description" key to the link form, which knows that newlines should be replaced with <br>.

So...

select json_object(
  'href', 'https://simonwillison.net/',
  'label', 'This is a link',
  'description', 'This is a multiline description associated with that link.
It can contain line breaks.')

Fix Jinja deprecation warnings

/home/runner/work/datasette-json-html/datasette-json-html/datasette_json_html/__init__.py:67: DeprecationWarning: 'jinja2.escape' is deprecated and will be removed in Jinja 3.1. Import 'markupsafe.escape' instead.

Also:

/Users/simon/Dropbox/Development/datasette-json-html/datasette_json_html/__init__.py:61: DeprecationWarning: 'jinja2.Markup' is deprecated and will be removed in Jinja 3.1. Import 'markupsafe.Markup' instead.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.