mirror of
https://github.com/simonw/datasette.git
synced 2026-05-31 22:27:00 +02:00
Document register_column_types hook and updated render_cell signature
- Add register_column_types(datasette) hook documentation with example - Update render_cell signature to include column_type and column_type_config parameters - Fixes test_plugin_hooks_are_documented https://claude.ai/code/session_01SvPEPqHgURTWESRp28pTC3
This commit is contained in:
parent
ad6a020e6d
commit
32e4a31913
1 changed files with 97 additions and 2 deletions
|
|
@ -474,8 +474,8 @@ Examples: `datasette-publish-fly <https://datasette.io/plugins/datasette-publish
|
|||
|
||||
.. _plugin_hook_render_cell:
|
||||
|
||||
render_cell(row, value, column, table, pks, database, datasette, request)
|
||||
-------------------------------------------------------------------------
|
||||
render_cell(row, value, column, table, pks, database, datasette, request, column_type, column_type_config)
|
||||
----------------------------------------------------------------------------------------------------------
|
||||
|
||||
Lets you customize the display of values within table cells in the HTML table view.
|
||||
|
||||
|
|
@ -503,6 +503,14 @@ Lets you customize the display of values within table cells in the HTML table vi
|
|||
``request`` - :ref:`internals_request`
|
||||
The current request object
|
||||
|
||||
``column_type`` - string or None
|
||||
The name of the :ref:`column type <column_types>` assigned to this column, or ``None`` if no column type is assigned.
|
||||
|
||||
``column_type_config`` - dict or None
|
||||
The configuration dict for the assigned column type, or ``None``.
|
||||
|
||||
If a column has a :ref:`column type <column_types>` assigned and that column type's ``render_cell`` method returns a non-``None`` value, it will take priority over this plugin hook.
|
||||
|
||||
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.
|
||||
|
|
@ -989,6 +997,93 @@ This tells Datasette "here's how to find all documents in the system - look in t
|
|||
|
||||
The permission system then uses this query along with rules from plugins to determine which documents each user can access, all efficiently in SQL rather than loading everything into Python.
|
||||
|
||||
.. _plugin_register_column_types:
|
||||
|
||||
register_column_types(datasette)
|
||||
--------------------------------
|
||||
|
||||
Return a list of :ref:`ColumnType <column_types>` instances to register custom column types. Column types define how values in specific columns are rendered, validated, and transformed.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from datasette import hookimpl
|
||||
from datasette.column_types import ColumnType
|
||||
import markupsafe
|
||||
|
||||
|
||||
class ColorColumnType(ColumnType):
|
||||
async def render_cell(
|
||||
self, value, column, table, database,
|
||||
datasette, request, config
|
||||
):
|
||||
if value:
|
||||
return markupsafe.Markup(
|
||||
'<span style="background-color: {color}">'
|
||||
"{color}</span>"
|
||||
).format(color=markupsafe.escape(value))
|
||||
return None
|
||||
|
||||
async def validate(self, value, config, datasette):
|
||||
if value and not value.startswith("#"):
|
||||
return "Color must start with #"
|
||||
return None
|
||||
|
||||
async def transform_value(
|
||||
self, value, config, datasette
|
||||
):
|
||||
# Normalize to uppercase
|
||||
if isinstance(value, str):
|
||||
return value.upper()
|
||||
return value
|
||||
|
||||
|
||||
@hookimpl
|
||||
def register_column_types(datasette):
|
||||
return [
|
||||
ColorColumnType(
|
||||
name="color",
|
||||
description="CSS color value",
|
||||
)
|
||||
]
|
||||
|
||||
Each ``ColumnType`` instance has the following attributes:
|
||||
|
||||
``name`` - string
|
||||
Unique identifier for the column type, e.g. ``"color"``. Must be unique across all plugins.
|
||||
|
||||
``description`` - string
|
||||
Human-readable label, e.g. ``"CSS color value"``.
|
||||
|
||||
And the following methods, all optional:
|
||||
|
||||
``render_cell(self, value, column, table, database, datasette, request, config)``
|
||||
Return an HTML string to render this cell value, or ``None`` to fall through to the default ``render_cell`` plugin hook chain. When a column type provides rendering, it takes priority over the ``render_cell`` plugin hook.
|
||||
|
||||
``validate(self, value, config, datasette)``
|
||||
Validate a value before it is written via the insert, update, or upsert API endpoints. Return ``None`` if valid, or a string error message if invalid. Null values and empty strings skip validation.
|
||||
|
||||
``transform_value(self, value, config, datasette)``
|
||||
Transform a value before it appears in JSON API output. Return the transformed value. The default implementation returns the value unchanged.
|
||||
|
||||
The ``config`` argument passed to these methods is the parsed JSON config dict for the specific column assignment, or ``None`` if no config was provided.
|
||||
|
||||
Column types are assigned to columns via the ``column_types`` key in :ref:`table configuration <metadata_tables>`:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
databases:
|
||||
mydb:
|
||||
tables:
|
||||
mytable:
|
||||
column_types:
|
||||
bg_color: color
|
||||
highlight:
|
||||
type: color
|
||||
config:
|
||||
format: rgb
|
||||
|
||||
Datasette includes three built-in column types: ``url``, ``email``, and ``json``.
|
||||
|
||||
.. _plugin_asgi_wrapper:
|
||||
|
||||
asgi_wrapper(datasette)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue