sections_fts: 10
This data as json
rowid | title | content |
---|---|---|
10 | render_cell(row, value, column, table, database, datasette, request) | Lets you customize the display of values within table cells in the HTML table view. row - sqlite.Row The SQLite row object that the value being rendered is part of value - string, integer, float, bytes or None The value that was loaded from the database column - string The name of the column being rendered table - string or None The name of the table - or None if this is a custom SQL query database - string The name of the database datasette - Datasette class You can use this to access plugin configuration options via datasette.plugin_config(your_plugin_name) , or to execute SQL queries. request - Request object The current request object If your hook returns None , it will be ignored. Use this to indicate that your hook is not able to custom render this particular value. If the hook returns a string, that string will be rendered in the table cell. If you want to return HTML markup you can do so by returning a jinja2.Markup object. You can also return an awaitable function which returns a value. Datasette will loop through all available render_cell hooks and display the value returned by the first one that does not return None . Here is an example of a custom render_cell() plugin which looks for values that are a JSON string matching the following format: {"href": "https://www.example.com/", "label": "Name"} If the value matches that pattern, the plugin returns an HTML link element: from datasette import hookimpl import markupsafe import json @hookimpl def render_cell(value): # Render {"href": "...", "label": "..."} as link if not isinstance(value, str): return None stripped = value.strip() if not ( stripped.startswith("{") and stripped.endswith("}") ): return None try: data = json.loads(value) except ValueError: return None if not isinstance(data, dict): return None if set(data.keys()) != {"href", "label"}: return None href = data["href"] if not ( href.startswith("/") or href.startswith("http://") or href.startswith("https://") ): return None return markupsafe.Markup( '<a href="{href}">{label}</a>'.format( href=markupsafe.escape(data["href"]), label=markupsafe.escape(data["label"] or "") or " ", ) ) Examples: datasette-render-binary , datasette-render-markdown , datasette-json-html |