diff --git a/datasette/app.py b/datasette/app.py
index 793bc931..d3a8168e 100644
--- a/datasette/app.py
+++ b/datasette/app.py
@@ -24,6 +24,7 @@ from .views.database import DatabaseDownload, DatabaseView
 from .views.index import IndexView
 from .views.special import JsonDataView
 from .views.table import RowView, TableView
+from .renderer import json_renderer
 
 from .utils import (
     InterruptedError,
@@ -207,6 +208,7 @@ class Datasette:
         self.plugins_dir = plugins_dir
         self.static_mounts = static_mounts or []
         self._config = dict(DEFAULT_CONFIG, **(config or {}))
+        self.renderers = {}  # File extension -> renderer function
         self.version_note = version_note
         self.executor = futures.ThreadPoolExecutor(
             max_workers=self.config("num_sql_threads")
@@ -630,6 +632,22 @@ class Datasette:
             )
         return results
 
+    def register_renderers(self):
+        """ Register output renderers which output data in custom formats. """
+        # Built-in renderers
+        self.renderers['json'] = json_renderer
+
+        # Hooks
+        hook_renderers = []
+        for hook in pm.hook.register_output_renderer(datasette=self):
+            if type(hook) == list:
+                hook_renderers += hook
+            else:
+                hook_renderers.append(hook)
+
+        for renderer in hook_renderers:
+            self.renderers[renderer['extension']] = renderer['callback']
+
     def app(self):
 
         class TracingSanic(Sanic):
@@ -671,6 +689,11 @@ class Datasette:
         self.jinja_env.filters["to_css_class"] = to_css_class
         # pylint: disable=no-member
         pm.hook.prepare_jinja2_environment(env=self.jinja_env)
+
+        self.register_renderers()
+        # Generate a regex snippet to match all registered renderer file extensions
+        renderer_regex = "|".join(r"\." + key for key in self.renderers.keys())
+
         app.add_route(IndexView.as_view(self), r"/<as_format:(\.jsono?)?$>")
         # TODO: /favicon.ico and /-/static/ deserve far-future cache expires
         app.add_route(favicon, "/favicon.ico")
@@ -706,7 +729,8 @@ class Datasette:
             DatabaseDownload.as_view(self), r"/<db_name:[^/]+?><as_db:(\.db)$>"
         )
         app.add_route(
-            DatabaseView.as_view(self), r"/<db_name:[^/]+?><as_format:(\.jsono?|\.csv)?$>"
+            DatabaseView.as_view(self),
+            r"/<db_name:[^/]+?><as_format:(" + renderer_regex + r"|.jsono|\.csv)?$>"
         )
         app.add_route(
             TableView.as_view(self),
@@ -714,7 +738,7 @@ class Datasette:
         )
         app.add_route(
             RowView.as_view(self),
-            r"/<db_name:[^/]+>/<table:[^/]+?>/<pk_path:[^/]+?><as_format:(\.jsono?)?$>",
+            r"/<db_name:[^/]+>/<table:[^/]+?>/<pk_path:[^/]+?><as_format:(" + renderer_regex + r")?$>",
         )
         self.register_custom_units()
 
diff --git a/datasette/hookspecs.py b/datasette/hookspecs.py
index 6db95344..576e05b9 100644
--- a/datasette/hookspecs.py
+++ b/datasette/hookspecs.py
@@ -38,3 +38,8 @@ def publish_subcommand(publish):
 @hookspec(firstresult=True)
 def render_cell(value, column, table, database, datasette):
     "Customize rendering of HTML table cell values"
+
+
+@hookspec
+def register_output_renderer(datasette):
+    "Register a renderer to output data in a different format"
diff --git a/datasette/renderer.py b/datasette/renderer.py
new file mode 100644
index 00000000..dc9011ce
--- /dev/null
+++ b/datasette/renderer.py
@@ -0,0 +1,96 @@
+import json
+from datasette.utils import (
+    value_as_boolean,
+    remove_infinites,
+    CustomJSONEncoder,
+    path_from_row_pks,
+)
+
+
+def convert_specific_columns_to_json(rows, columns, json_cols):
+    json_cols = set(json_cols)
+    if not json_cols.intersection(columns):
+        return rows
+    new_rows = []
+    for row in rows:
+        new_row = []
+        for value, column in zip(row, columns):
+            if column in json_cols:
+                try:
+                    value = json.loads(value)
+                except (TypeError, ValueError) as e:
+                    print(e)
+                    pass
+            new_row.append(value)
+        new_rows.append(new_row)
+    return new_rows
+
+
+def json_renderer(args, data, view_name):
+    """ Render a response as JSON """
+    status_code = 200
+    # Handle the _json= parameter which may modify data["rows"]
+    json_cols = []
+    if "_json" in args:
+        json_cols = args["_json"]
+    if json_cols and "rows" in data and "columns" in data:
+        data["rows"] = convert_specific_columns_to_json(
+            data["rows"], data["columns"], json_cols
+        )
+
+    # unless _json_infinity=1 requested, replace infinity with None
+    if "rows" in data and not value_as_boolean(args.get("_json_infinity", "0")):
+        data["rows"] = [remove_infinites(row) for row in data["rows"]]
+
+    # Deal with the _shape option
+    shape = args.get("_shape", "arrays")
+    if shape == "arrayfirst":
+        data = [row[0] for row in data["rows"]]
+    elif shape in ("objects", "object", "array"):
+        columns = data.get("columns")
+        rows = data.get("rows")
+        if rows and columns:
+            data["rows"] = [dict(zip(columns, row)) for row in rows]
+        if shape == "object":
+            error = None
+            if "primary_keys" not in data:
+                error = "_shape=object is only available on tables"
+            else:
+                pks = data["primary_keys"]
+                if not pks:
+                    error = (
+                        "_shape=object not available for tables with no primary keys"
+                    )
+                else:
+                    object_rows = {}
+                    for row in data["rows"]:
+                        pk_string = path_from_row_pks(row, pks, not pks)
+                        object_rows[pk_string] = row
+                    data = object_rows
+            if error:
+                data = {"ok": False, "error": error}
+        elif shape == "array":
+            data = data["rows"]
+    elif shape == "arrays":
+        pass
+    else:
+        status_code = 400
+        data = {
+            "ok": False,
+            "error": "Invalid _shape: {}".format(shape),
+            "status": 400,
+            "title": None,
+        }
+    # Handle _nl option for _shape=array
+    nl = args.get("_nl", "")
+    if nl and shape == "array":
+        body = "\n".join(json.dumps(item) for item in data)
+        content_type = "text/plain"
+    else:
+        body = json.dumps(data, cls=CustomJSONEncoder)
+        content_type = "application/json"
+    return {
+        "body": body,
+        "status_code": status_code,
+        "content_type": content_type
+    }
diff --git a/datasette/templates/query.html b/datasette/templates/query.html
index 81901660..b4b759a5 100644
--- a/datasette/templates/query.html
+++ b/datasette/templates/query.html
@@ -47,7 +47,7 @@
 </form>
 
 {% if display_rows %}
-<p class="export-links">This data as <a href="{{ url_json }}">JSON</a>, <a href="{{ url_csv }}">CSV</a></p>
+  <p class="export-links">This data as {% for name, url in renderers.items() %}<a href="{{ url }}">{{ name }}</a>{{ ", " if not loop.last }}{% endfor %}, <a href="{{ url_csv }}">CSV</a></p>
 <table class="rows-and-columns">
     <thead>
         <tr>
diff --git a/datasette/templates/row.html b/datasette/templates/row.html
index 389b16b2..baffaf96 100644
--- a/datasette/templates/row.html
+++ b/datasette/templates/row.html
@@ -22,7 +22,7 @@
 
 {% block description_source_license %}{% include "_description_source_license.html" %}{% endblock %}
 
-<p>This data as <a href="{{ url_json }}">.json</a></p>
+<p>This data as {% for name, url in renderers.items() %}<a href="{{ url }}">{{ name }}</a>{{ ", " if not loop.last }}{% endfor %}</p>
 
 {% include custom_rows_and_columns_templates %}
 
diff --git a/datasette/templates/table.html b/datasette/templates/table.html
index 1c65aa10..d28f41d6 100644
--- a/datasette/templates/table.html
+++ b/datasette/templates/table.html
@@ -106,7 +106,7 @@
     <p><a class="not-underlined" title="{{ query.sql }}" href="{{ database_url(database) }}?{{ {'sql': query.sql}|urlencode|safe }}{% if query.params %}&amp;{{ query.params|urlencode|safe }}{% endif %}">&#x270e; <span class="underlined">View and edit SQL</span></a></p>
 {% endif %}
 
-<p class="export-links">This data as <a href="{{ url_json }}">JSON</a>{% if display_rows %}, <a href="{{ url_csv }}">CSV</a> (<a href="#export">advanced</a>){% endif %}</p>
+<p class="export-links">This data as {% for name, url in renderers.items() %}<a href="{{ url }}">{{ name }}</a>{{ ", " if not loop.last }}{% endfor %}{% if display_rows %}, <a href="{{ url_csv }}">CSV</a> (<a href="#export">advanced</a>){% endif %}</p>
 
 {% if suggested_facets %}
     <p class="suggested-facets">
@@ -155,10 +155,10 @@
     <div id="export" class="advanced-export">
         <h3>Advanced export</h3>
         <p>JSON shape:
-            <a href="{{ url_json }}">default</a>,
-            <a href="{{ append_querystring(url_json, '_shape=array') }}">array</a>,
-            <a href="{{ append_querystring(url_json, '_shape=array&_nl=on') }}">newline-delimited</a>{% if primary_keys %},
-                <a href="{{ append_querystring(url_json, '_shape=object') }}">object</a>
+            <a href="{{ renderers['json'] }}">default</a>,
+            <a href="{{ append_querystring(renderers['json'], '_shape=array') }}">array</a>,
+            <a href="{{ append_querystring(renderers['json'], '_shape=array&_nl=on') }}">newline-delimited</a>{% if primary_keys %},
+                <a href="{{ append_querystring(renderers['json'], '_shape=object') }}">object</a>
             {% endif %}
         </p>
         <form action="{{ url_csv_path }}" method="get">
diff --git a/datasette/utils.py b/datasette/utils.py
index 0c161ac6..2f5c633e 100644
--- a/datasette/utils.py
+++ b/datasette/utils.py
@@ -152,8 +152,10 @@ def sqlite_timelimit(conn, ms):
             return 1
 
     conn.set_progress_handler(handler, n)
-    yield
-    conn.set_progress_handler(None, n)
+    try:
+        yield
+    finally:
+        conn.set_progress_handler(None, n)
 
 
 class InvalidSql(Exception):
@@ -716,17 +718,16 @@ def get_plugins(pm):
     return plugins
 
 
-FORMATS = ('csv', 'json', 'jsono')
-
-
-async def resolve_table_and_format(table_and_format, table_exists):
+async def resolve_table_and_format(table_and_format, table_exists, allowed_formats=[]):
     if '.' in table_and_format:
         # Check if a table exists with this exact name
         it_exists = await table_exists(table_and_format)
         if it_exists:
             return table_and_format, None
+
     # Check if table ends with a known format
-    for _format in FORMATS:
+    formats = list(allowed_formats) + ['csv', 'jsono']
+    for _format in formats:
         if table_and_format.endswith(".{}".format(_format)):
             table = table_and_format[:-(len(_format) + 1)]
             return table, _format
diff --git a/datasette/views/base.py b/datasette/views/base.py
index 764ad7dd..ae6aac71 100644
--- a/datasette/views/base.py
+++ b/datasette/views/base.py
@@ -1,7 +1,6 @@
 import asyncio
 import csv
 import itertools
-import json
 import re
 import time
 import urllib
@@ -15,21 +14,17 @@ from sanic.views import HTTPMethodView
 from datasette import __version__
 from datasette.plugins import pm
 from datasette.utils import (
-    CustomJSONEncoder,
     InterruptedError,
     InvalidSql,
     LimitedWriter,
     format_bytes,
     is_url,
-    path_from_row_pks,
     path_with_added_args,
     path_with_removed_args,
     path_with_format,
-    remove_infinites,
     resolve_table_and_format,
     sqlite3,
     to_css_class,
-    value_as_boolean,
 )
 
 ureg = pint.UnitRegistry()
@@ -127,6 +122,7 @@ class RenderMixin(HTTPMethodView):
 
 
 class BaseView(RenderMixin):
+    name = ''
     re_named_parameter = re.compile(":([a-zA-Z0-9_]+)")
 
     def __init__(self, datasette):
@@ -184,7 +180,8 @@ class BaseView(RenderMixin):
                     table_and_format=urllib.parse.unquote_plus(
                         kwargs["table_and_format"]
                     ),
-                    table_exists=async_table_exists
+                    table_exists=async_table_exists,
+                    allowed_formats=self.ds.renderers.keys()
                 )
                 kwargs["table"] = table
                 if _format:
@@ -316,33 +313,43 @@ class BaseView(RenderMixin):
             content_type=content_type
         )
 
-    async def view_get(self, request, database, hash, correct_hash_provided, **kwargs):
+    async def get_format(self, request, database, args):
+        """ Determine the format of the response from the request, from URL
+            parameters or from a file extension.
+
+            `args` is a dict of the path components parsed from the URL by the router.
+        """
         # If ?_format= is provided, use that as the format
         _format = request.args.get("_format", None)
         if not _format:
-            _format = (kwargs.pop("as_format", None) or "").lstrip(".")
-        if "table_and_format" in kwargs:
+            _format = (args.pop("as_format", None) or "").lstrip(".")
+        if "table_and_format" in args:
             async def async_table_exists(t):
                 return await self.ds.table_exists(database, t)
             table, _ext_format = await resolve_table_and_format(
                 table_and_format=urllib.parse.unquote_plus(
-                    kwargs["table_and_format"]
+                    args["table_and_format"]
                 ),
-                table_exists=async_table_exists
+                table_exists=async_table_exists,
+                allowed_formats=self.ds.renderers.keys()
             )
             _format = _format or _ext_format
-            kwargs["table"] = table
-            del kwargs["table_and_format"]
-        elif "table" in kwargs:
-            kwargs["table"] = urllib.parse.unquote_plus(
-                kwargs["table"]
+            args["table"] = table
+            del args["table_and_format"]
+        elif "table" in args:
+            args["table"] = urllib.parse.unquote_plus(
+                args["table"]
             )
+        return _format, args
+
+    async def view_get(self, request, database, hash, correct_hash_provided, **kwargs):
+        _format, kwargs = await self.get_format(request, database, kwargs)
 
         if _format == "csv":
             return await self.as_csv(request, database, hash, **kwargs)
 
         if _format is None:
-            # HTML views default to expanding all forign key labels
+            # HTML views default to expanding all foriegn key labels
             kwargs['default_labels'] = True
 
         extra_template_data = {}
@@ -358,7 +365,7 @@ class BaseView(RenderMixin):
 
             else:
                 data, extra_template_data, templates = response_or_template_contexts
-        except InterruptedError as e:
+        except InterruptedError:
             raise DatasetteError("""
                 SQL query took too long. The time limit is controlled by the
                 <a href="https://datasette.readthedocs.io/en/stable/config.html#sql-time-limit-ms">sql_time_limit_ms</a>
@@ -379,92 +386,37 @@ class BaseView(RenderMixin):
             value = self.ds.metadata(key)
             if value:
                 data[key] = value
-        if _format in ("json", "jsono"):
-            # Special case for .jsono extension - redirect to _shape=objects
-            if _format == "jsono":
-                return self.redirect(
+
+        # Special case for .jsono extension - redirect to _shape=objects
+        if _format == "jsono":
+            return self.redirect(
+                request,
+                path_with_added_args(
                     request,
-                    path_with_added_args(
-                        request,
-                        {"_shape": "objects"},
-                        path=request.path.rsplit(".jsono", 1)[0] + ".json",
-                    ),
-                    forward_querystring=False,
-                )
+                    {"_shape": "objects"},
+                    path=request.path.rsplit(".jsono", 1)[0] + ".json",
+                ),
+                forward_querystring=False,
+            )
 
-            # Handle the _json= parameter which may modify data["rows"]
-            json_cols = []
-            if "_json" in request.args:
-                json_cols = request.args["_json"]
-            if json_cols and "rows" in data and "columns" in data:
-                data["rows"] = convert_specific_columns_to_json(
-                    data["rows"], data["columns"], json_cols,
-                )
+        if _format in self.ds.renderers.keys():
+            # Dispatch request to the correct output format renderer
+            # (CSV is not handled here due to streaming)
+            result = self.ds.renderers[_format](request.args, data, self.name)
+            if result is None:
+                raise NotFound("No data")
 
-            # unless _json_infinity=1 requested, replace infinity with None
-            if "rows" in data and not value_as_boolean(
-                request.args.get("_json_infinity", "0")
-            ):
-                data["rows"] = [remove_infinites(row) for row in data["rows"]]
-
-            # Deal with the _shape option
-            shape = request.args.get("_shape", "arrays")
-            if shape == "arrayfirst":
-                data = [row[0] for row in data["rows"]]
-            elif shape in ("objects", "object", "array"):
-                columns = data.get("columns")
-                rows = data.get("rows")
-                if rows and columns:
-                    data["rows"] = [dict(zip(columns, row)) for row in rows]
-                if shape == "object":
-                    error = None
-                    if "primary_keys" not in data:
-                        error = "_shape=object is only available on tables"
-                    else:
-                        pks = data["primary_keys"]
-                        if not pks:
-                            error = "_shape=object not available for tables with no primary keys"
-                        else:
-                            object_rows = {}
-                            for row in data["rows"]:
-                                pk_string = path_from_row_pks(row, pks, not pks)
-                                object_rows[pk_string] = row
-                            data = object_rows
-                    if error:
-                        data = {
-                            "ok": False,
-                            "error": error,
-                            "database": database,
-                        }
-                elif shape == "array":
-                    data = data["rows"]
-            elif shape == "arrays":
-                pass
-            else:
-                status_code = 400
-                data = {
-                    "ok": False,
-                    "error": "Invalid _shape: {}".format(shape),
-                    "status": 400,
-                    "title": None,
-                }
-            headers = {}
-            if self.ds.cors:
-                headers["Access-Control-Allow-Origin"] = "*"
-            # Handle _nl option for _shape=array
-            nl = request.args.get("_nl", "")
-            if nl and shape == "array":
-                body = "\n".join(json.dumps(item) for item in data)
-                content_type = "text/plain"
+            response_args = {
+                'content_type': result.get('content_type', 'text/plain'),
+                'status': result.get('status_code', 200)
+            }
+
+            if type(result.get('body')) == bytes:
+                response_args['body_bytes'] = result.get('body')
             else:
-                body = json.dumps(data, cls=CustomJSONEncoder)
-                content_type = "application/json"
-            r = response.HTTPResponse(
-                body,
-                status=status_code,
-                content_type=content_type,
-                headers=headers,
-            )
+                response_args['body'] = result.get('body')
+
+            r = response.HTTPResponse(**response_args)
         else:
             extras = {}
             if callable(extra_template_data):
@@ -476,6 +428,10 @@ class BaseView(RenderMixin):
             url_labels_extra = {}
             if data.get("expandable_columns"):
                 url_labels_extra = {"_labels": "on"}
+
+            renderers = {
+                key: path_with_format(request, key, {**url_labels_extra}) for key in self.ds.renderers.keys()
+            }
             url_csv_args = {
                 "_size": "max",
                 **url_labels_extra
@@ -486,9 +442,7 @@ class BaseView(RenderMixin):
                 **data,
                 **extras,
                 **{
-                    "url_json": path_with_format(request, "json", {
-                        **url_labels_extra,
-                    }),
+                    "renderers": renderers,
                     "url_csv": url_csv,
                     "url_csv_path": url_csv_path,
                     "url_csv_hidden_args": [
@@ -504,23 +458,29 @@ class BaseView(RenderMixin):
                 context["metadata"] = self.ds.metadata
             r = self.render(templates, **context)
             r.status = status_code
-        # Set far-future cache expiry
-        if self.ds.cache_headers and r.status == 200:
-            ttl = request.args.get("_ttl", None)
-            if ttl is None or not ttl.isdigit():
-                if correct_hash_provided:
-                    ttl = self.ds.config("default_cache_ttl_hashed")
-                else:
-                    ttl = self.ds.config("default_cache_ttl")
+
+        ttl = request.args.get("_ttl", None)
+        if ttl is None or not ttl.isdigit():
+            if correct_hash_provided:
+                ttl = self.ds.config("default_cache_ttl_hashed")
             else:
-                ttl = int(ttl)
+                ttl = self.ds.config("default_cache_ttl")
+
+        return self.set_response_headers(r, ttl)
+
+    def set_response_headers(self, response, ttl):
+        # Set far-future cache expiry
+        if self.ds.cache_headers and response.status == 200:
+            ttl = int(ttl)
             if ttl == 0:
                 ttl_header = 'no-cache'
             else:
                 ttl_header = 'max-age={}'.format(ttl)
-            r.headers["Cache-Control"] = ttl_header
-        r.headers["Referrer-Policy"] = "no-referrer"
-        return r
+            response.headers["Cache-Control"] = ttl_header
+        response.headers["Referrer-Policy"] = "no-referrer"
+        if self.ds.cors:
+            response.headers["Access-Control-Allow-Origin"] = "*"
+        return response
 
     async def custom_sql(
         self, request, database, hash, sql, editable=True, canned_query=None,
@@ -611,22 +571,3 @@ class BaseView(RenderMixin):
             "columns": columns,
             "query": {"sql": sql, "params": params},
         }, extra_template, templates
-
-
-def convert_specific_columns_to_json(rows, columns, json_cols):
-    json_cols = set(json_cols)
-    if not json_cols.intersection(columns):
-        return rows
-    new_rows = []
-    for row in rows:
-        new_row = []
-        for value, column in zip(row, columns):
-            if column in json_cols:
-                try:
-                    value = json.loads(value)
-                except (TypeError, ValueError) as e:
-                    print(e)
-                    pass
-            new_row.append(value)
-        new_rows.append(new_row)
-    return new_rows
diff --git a/datasette/views/database.py b/datasette/views/database.py
index 9f43980b..d1185eef 100644
--- a/datasette/views/database.py
+++ b/datasette/views/database.py
@@ -8,6 +8,7 @@ from .base import BaseView, DatasetteError
 
 
 class DatabaseView(BaseView):
+    name = 'database'
 
     async def data(self, request, database, hash, default_labels=False, _size=None):
         if request.args.get("sql"):
@@ -39,6 +40,7 @@ class DatabaseView(BaseView):
 
 
 class DatabaseDownload(BaseView):
+    name = 'database_download'
 
     async def view_get(self, request, database, hash, correct_hash_present, **kwargs):
         if not self.ds.config("allow_download"):
diff --git a/datasette/views/index.py b/datasette/views/index.py
index 70f7e943..4eb116f3 100644
--- a/datasette/views/index.py
+++ b/datasette/views/index.py
@@ -9,6 +9,7 @@ from .base import HASH_LENGTH, RenderMixin
 
 
 class IndexView(RenderMixin):
+    name = 'index'
 
     def __init__(self, datasette):
         self.ds = datasette
diff --git a/datasette/views/table.py b/datasette/views/table.py
index bc5e775e..87f6d2c6 100644
--- a/datasette/views/table.py
+++ b/datasette/views/table.py
@@ -174,6 +174,7 @@ class RowTableShared(BaseView):
 
 
 class TableView(RowTableShared):
+    name = 'table'
 
     async def data(self, request, database, hash, table, default_labels=False,  _next=None, _size=None):
         canned_query = self.ds.get_canned_query(database, table)
@@ -778,6 +779,7 @@ class TableView(RowTableShared):
 
 
 class RowView(RowTableShared):
+    name = 'row'
 
     async def data(self, request, database, hash, table, pk_path, default_labels=False):
         pk_values = urlsafe_components(pk_path)
diff --git a/docs/plugins.rst b/docs/plugins.rst
index 984e5c95..ae3c4607 100644
--- a/docs/plugins.rst
+++ b/docs/plugins.rst
@@ -551,3 +551,53 @@ The ``template``, ``database`` and ``table`` options can be used to return diffe
 The ``datasette`` instance is provided primarily so that you can consult any plugin configuration options that may have been set, using the ``datasette.plugin_config(plugin_name)`` method documented above.
 
 The string that you return from this function will be treated as "safe" for inclusion in a ``<script>`` block directly in the page, so it is up to you to apply any necessary escaping.
+
+.. _plugin_register_output_renderer:
+
+register_output_renderer(datasette)
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+``datasette`` - Datasette instance
+    You can use this to access plugin configuration options via ``datasette.plugin_config(your_plugin_name)``
+
+Allows the plugin to register a new output renderer, to output data in a custom format. The hook function should return a dictionary, or a list of dictionaries, which contain the file extension you want to handle and a callback function:
+
+.. code-block:: python
+
+        @hookimpl
+        def register_output_renderer(datasette):
+            return {
+                'extension': 'test',
+                'callback': render_test
+            }
+
+This will register `render_test` to be called when paths with the extension `.test` (for example `/database.test`, `/database/table.test`, or `/database/table/row.test`) are requested. When a request is received, the callback function is called with three positional arguments:
+
+``args`` - dictionary
+    The GET parameters of the request
+
+``data`` - dictionary
+    The data to be rendered
+
+``view_name`` - string
+    The name of the view where the renderer is being called. (`database`, `table`, and `row` are the most important ones.)
+
+The callback function can return `None`, if it is unable to render the data, or a dictionary with the following keys:
+
+``body`` - string or bytes, optional
+    The response body, default empty
+
+``content_type`` - string, optional
+    The Content-Type header, default `text/plain`
+
+``status_code`` - integer, optional
+    The HTTP status code, default 200
+
+A simple example of an output renderer callback function:
+
+.. code-block:: python
+
+        def render_test(args, data, view_name):
+            return {
+                'body': 'Hello World'
+            }

Plugin hook for additional data export formats
It would be nice to have a simple way for plugins to provide additional data export formats. Might require a bit of work on the internals. I can work around this at a lower level with the `prepare_sanic` hook from #437 in the mean time.

I guess plugins should be able to register a function which takes a row or list of rows and returns the rendered data. They'll also need to provide a file extension and probably a Content-Type.

Datasette could then automatically include this format in the list of export formats on each page.

Looks like this is related to #119.
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 1ca202f4..36f6254c 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -291,7 +291,7 @@ async def test_resolve_table_and_format(
     table_and_format, expected_table, expected_format
 ):
     actual_table, actual_format = await utils.resolve_table_and_format(
-        table_and_format, table_exists
+        table_and_format, table_exists, ['json']
     )
     assert expected_table == actual_table
     assert expected_format == actual_format

pip install -e .[dev]
parse_log_pytest
pytest
pytest pytest-asyncio aiohttp beautifulsoup4
3.7
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
aiofiles==23.2.1
aiohttp==3.8.6
aiosignal==1.3.1
async-timeout==4.0.3
asynctest==0.13.0
attrs @ file:///croot/attrs_1668696182826/work
beautifulsoup4==4.13.3
certifi @ file:///croot/certifi_1671487769961/work/certifi
charset-normalizer==3.4.1
click==8.1.8
click-default-group==1.2
-e git+https://github.com/simonw/datasette.git@11b352b4d52fd02a422776edebb14f12e4994d3b#egg=datasette
flit_core @ file:///opt/conda/conda-bld/flit-core_1644941570762/work/source/flit_core
frozenlist==1.3.3
httptools==0.6.0
hupper==1.0
idna==3.10
importlib-metadata @ file:///tmp/build/80754af9/importlib-metadata_1648562407465/work
iniconfig @ file:///home/linux1/recipes/ci/iniconfig_1610983019677/work
Jinja2==2.10.1
MarkupSafe==2.1.5
multidict==6.0.5
packaging @ file:///croot/packaging_1671697413597/work
Pint==0.8.1
pluggy @ file:///tmp/build/80754af9/pluggy_1648042572264/work
py @ file:///opt/conda/conda-bld/py_1644396412707/work
pytest==7.1.2
pytest-asyncio==0.21.2
sanic==0.7.0
soupsieve==2.4.1
tomli @ file:///opt/conda/conda-bld/tomli_1657175507142/work
typing_extensions @ file:///croot/typing_extensions_1669924550328/work
ujson==5.7.0
uvloop==0.18.0
websockets==11.0.3
yarl==1.9.4
zipp @ file:///croot/zipp_1672387121353/work

name: datasette
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - attrs=22.1.0=py37h06a4308_0
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2022.12.7=py37h06a4308_0
  - flit-core=3.6.0=pyhd3eb1b0_0
  - importlib-metadata=4.11.3=py37h06a4308_0
  - importlib_metadata=4.11.3=hd3eb1b0_0
  - iniconfig=1.1.1=pyhd3eb1b0_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - packaging=22.0=py37h06a4308_0
  - pip=22.3.1=py37h06a4308_0
  - pluggy=1.0.0=py37h06a4308_1
  - py=1.11.0=pyhd3eb1b0_0
  - pytest=7.1.2=py37h06a4308_0
  - python=3.7.16=h7a1cb2a_0
  - readline=8.2=h5eee18b_0
  - setuptools=65.6.3=py37h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tomli=2.0.1=py37h06a4308_0
  - typing_extensions=4.4.0=py37h06a4308_0
  - wheel=0.38.4=py37h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zipp=3.11.0=py37h06a4308_0
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - aiofiles==23.2.1
      - aiohttp==3.8.6
      - aiosignal==1.3.1
      - async-timeout==4.0.3
      - asynctest==0.13.0
      - beautifulsoup4==4.13.3
      - charset-normalizer==3.4.1
      - click==8.1.8
      - click-default-group==1.2
      - frozenlist==1.3.3
      - httptools==0.6.0
      - hupper==1.0
      - idna==3.10
      - jinja2==2.10.1
      - markupsafe==2.1.5
      - multidict==6.0.5
      - pint==0.8.1
      - pytest-asyncio==0.21.2
      - sanic==0.7.0
      - soupsieve==2.4.1
      - ujson==5.7.0
      - uvloop==0.18.0
      - websockets==11.0.3
      - yarl==1.9.4
prefix: /opt/conda/envs/datasette

tests/test_utils.py::test_resolve_table_and_format[blah-blah-None] tests/test_utils.py::test_resolve_table_and_format[blah.csv-blah-csv] tests/test_utils.py::test_resolve_table_and_format[blah.json-blah-json] tests/test_utils.py::test_resolve_table_and_format[blah.baz-blah.baz-None] tests/test_utils.py::test_resolve_table_and_format[exists.csv-exists.csv-None]
tests/test_utils.py::test_urlsafe_components[foo-expected0] tests/test_utils.py::test_urlsafe_components[foo,bar-expected1] tests/test_utils.py::test_urlsafe_components[123,433,112-expected2] tests/test_utils.py::test_urlsafe_components[123%2C433,112-expected3] tests/test_utils.py::test_urlsafe_components[123%2F433%2F112-expected4] tests/test_utils.py::test_path_with_added_args[/foo-added_args0-/foo?bar=1] tests/test_utils.py::test_path_with_added_args[/foo?bar=1-added_args1-/foo?bar=1&baz=2] tests/test_utils.py::test_path_with_added_args[/foo?bar=1&bar=2-added_args2-/foo?bar=1&bar=2&baz=3] tests/test_utils.py::test_path_with_added_args[/foo?bar=1-added_args3-/foo] tests/test_utils.py::test_path_with_added_args[/?_facet=prim_state&_facet=area_name-added_args4-/?_facet=prim_state&_facet=area_name&prim_state=GA] tests/test_utils.py::test_path_with_added_args[/?_facet=state&_facet=city&state=MI-added_args5-/?_facet=state&_facet=city&state=MI&city=Detroit] tests/test_utils.py::test_path_with_added_args[/?_facet=state&_facet=city-added_args6-/?_facet=state&_facet=city&_facet=planet_int] tests/test_utils.py::test_path_with_removed_args[/foo?bar=1-args0-/foo] tests/test_utils.py::test_path_with_removed_args[/foo?bar=1&baz=2-args1-/foo?baz=2] tests/test_utils.py::test_path_with_removed_args[/foo?bar=1&bar=2&bar=3-args2-/foo?bar=1&bar=3] tests/test_utils.py::test_path_with_replaced_args[/foo?bar=1-args0-/foo?bar=2] tests/test_utils.py::test_path_with_replaced_args[/foo?bar=1&baz=2-args1-/foo?baz=2] tests/test_utils.py::test_path_from_row_pks[row0-pks0-foo,bar] tests/test_utils.py::test_path_from_row_pks[row1-pks1-f%2Co,bar] tests/test_utils.py::test_path_from_row_pks[row2-pks2-123] tests/test_utils.py::test_path_from_row_pks[row3-pks3-1,feline] tests/test_utils.py::test_custom_json_encoder[obj0-{"CategoryID": tests/test_utils.py::test_validate_sql_select_bad[update tests/test_utils.py::test_validate_sql_select_bad[PRAGMA tests/test_utils.py::test_validate_sql_select_good[select tests/test_utils.py::test_validate_sql_select_good[SELECT\nblah tests/test_utils.py::test_validate_sql_select_good[WITH tests/test_utils.py::test_detect_fts tests/test_utils.py::test_is_url[http://www.google.com/-True] tests/test_utils.py::test_is_url[https://example.com/-True] tests/test_utils.py::test_is_url[www.google.com-False] tests/test_utils.py::test_is_url[http://www.google.com/ tests/test_utils.py::test_to_css_class[simple-simple] tests/test_utils.py::test_to_css_class[MixedCase-MixedCase] tests/test_utils.py::test_to_css_class[-no-leading-hyphens-no-leading-hyphens-65bea6] tests/test_utils.py::test_to_css_class[_no-leading-underscores-no-leading-underscores-b921bc] tests/test_utils.py::test_to_css_class[no tests/test_utils.py::test_to_css_class[--336d5e] tests/test_utils.py::test_temporary_docker_directory_uses_hard_link tests/test_utils.py::test_temporary_docker_directory_uses_copy_if_hard_link_fails tests/test_utils.py::test_compound_keys_after_sql tests/test_utils.py::test_table_columns tests/test_utils.py::test_path_with_format[/foo?sql=select+1-csv-extra_qs0-/foo.csv?sql=select+1] tests/test_utils.py::test_path_with_format[/foo?sql=select+1-json-extra_qs1-/foo.json?sql=select+1] tests/test_utils.py::test_path_with_format[/foo/bar-json-extra_qs2-/foo/bar.json] tests/test_utils.py::test_path_with_format[/foo/bar-csv-extra_qs3-/foo/bar.csv] tests/test_utils.py::test_path_with_format[/foo/bar.csv-json-extra_qs4-/foo/bar.csv?_format=json] tests/test_utils.py::test_path_with_format[/foo/bar-csv-extra_qs5-/foo/bar.csv?_dl=1] tests/test_utils.py::test_path_with_format[/foo/b.csv-json-extra_qs6-/foo/b.csv?_dl=1&_format=json] tests/test_utils.py::test_path_with_format[/sf-trees/Street_Tree_List?_search=cherry&_size=1000-csv-extra_qs7-/sf-trees/Street_Tree_List.csv?_search=cherry&_size=1000&_dl=1] tests/test_utils.py::test_format_bytes[120-120 tests/test_utils.py::test_format_bytes[1024-1.0 tests/test_utils.py::test_format_bytes[1048576-1.0 tests/test_utils.py::test_format_bytes[1073741824-1.0 tests/test_utils.py::test_format_bytes[1395864371.2-1.3 tests/test_utils.py::test_format_bytes[1099511627776-1.0
datasette/templates/query.html datasette/views/index.py datasette/app.py datasette/templates/row.html datasette/utils.py docs/plugins.rst datasette/views/table.py datasette/views/base.py datasette/templates/table.html datasette/views/database.py datasette/hookspecs.py datasette/renderer.py
datasette/templates/query.html datasette/views/index.py datasette/app.py datasette/templates/row.html datasette/utils.py docs/plugins.rst datasette/views/table.py datasette/views/base.py datasette/templates/table.html datasette/views/database.py datasette/hookspecs.py datasette/renderer.py