sections: plugin_hooks:plugin-register-column-types
This data as json
| id | page | ref | title | content | breadcrumbs | references |
|---|---|---|---|---|---|---|
| plugin_hooks:plugin-register-column-types | plugin_hooks | plugin-register-column-types | register_column_types(datasette) | Return a list of ColumnType subclasses (not instances) to register custom column types. Column types define how values in specific columns are rendered, validated, and transformed. from datasette import hookimpl from datasette.column_types import ColumnType, SQLiteType import markupsafe class ColorColumnType(ColumnType): name = "color" description = "CSS color value" sqlite_types = (SQLiteType.TEXT,) async def render_cell( self, value, column, table, database, datasette, request, ): if value: return markupsafe.Markup( '<span style="background-color: {color}">' "{color}</span>" ).format(color=markupsafe.escape(value)) return None async def validate(self, value, datasette): if value and not value.startswith("#"): return "Color must start with #" return None async def transform_value(self, value, datasette): # Normalize to uppercase if isinstance(value, str): return value.upper() return value @hookimpl def register_column_types(datasette): return [ColorColumnType] Each ColumnType subclass must define the following class 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" . sqlite_types - tuple of SQLiteType values, optional Restrict assignments of this column type to columns with matching SQLite types, e.g. (SQLiteType.TEXT,) . If omitted, the column type can be assigned to any column. And the following methods, all optional: render_cell(self, value, column, table, database, datasette, request) 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, 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, datasette) Transform a value before it appears in JSON API output. Return the transformed value. The default implementation returns the value unchanged. Per-column configuration is available via self.config in all methods. When a column type is looked up for a specific column (via get_column_type or get_column_types ), the returned instance has config set to the parsed JSON config dict for that column assignment, or None if no config was provided. Column types are assigned to columns via the column_types table configuration option: 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 hooks"] | [] |