From 32e4a319133182e5f5dfd7a787e901ebfab4077f Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Mar 2026 04:03:52 +0000 Subject: [PATCH] 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 --- docs/plugin_hooks.rst | 99 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 2 deletions(-) diff --git a/docs/plugin_hooks.rst b/docs/plugin_hooks.rst index b9701f7c..b25403b3 100644 --- a/docs/plugin_hooks.rst +++ b/docs/plugin_hooks.rst @@ -474,8 +474,8 @@ Examples: `datasette-publish-fly ` 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 ` 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 ` 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( + '' + "{color}" + ).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 `: + +.. 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)