Expose current SQLite row to render_cell hook, closes #1300

This commit is contained in:
Simon Willison 2022-07-07 09:30:49 -07:00
commit 6373bb3414
6 changed files with 14 additions and 7 deletions

View file

@ -60,7 +60,7 @@ def publish_subcommand(publish):
@hookspec
def render_cell(value, column, table, database, datasette):
def render_cell(row, value, column, table, database, datasette):
"""Customize rendering of HTML table cell values"""

View file

@ -375,6 +375,7 @@ class QueryView(DataView):
# pylint: disable=no-member
plugin_display_value = None
for candidate in pm.hook.render_cell(
row=row,
value=value,
column=column,
table=None,

View file

@ -895,6 +895,7 @@ async def display_columns_and_rows(
# pylint: disable=no-member
plugin_display_value = None
for candidate in pm.hook.render_cell(
row=row,
value=value,
column=column,
table=table_name,

View file

@ -373,12 +373,15 @@ Examples: `datasette-publish-fly <https://datasette.io/plugins/datasette-publish
.. _plugin_hook_render_cell:
render_cell(value, column, table, database, datasette)
------------------------------------------------------
render_cell(row, value, column, table, database, datasette)
-----------------------------------------------------------
Lets you customize the display of values within table cells in the HTML table view.
``value`` - string, integer or None
``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

View file

@ -98,12 +98,13 @@ def extra_body_script(
@hookimpl
def render_cell(value, column, table, database, datasette):
def render_cell(row, value, column, table, database, datasette):
async def inner():
# Render some debug output in cell with value RENDER_CELL_DEMO
if value == "RENDER_CELL_DEMO":
return json.dumps(
{
"row": dict(row),
"column": column,
"table": table,
"database": database,

View file

@ -181,12 +181,13 @@ def test_hook_render_cell_demo(app_client):
response = app_client.get("/fixtures/simple_primary_key?id=4")
soup = Soup(response.body, "html.parser")
td = soup.find("td", {"class": "col-content"})
assert {
assert json.loads(td.string) == {
"row": {"id": "4", "content": "RENDER_CELL_DEMO"},
"column": "content",
"table": "simple_primary_key",
"database": "fixtures",
"config": {"depth": "table", "special": "this-is-simple_primary_key"},
} == json.loads(td.string)
}
@pytest.mark.parametrize(