Add pks parameter to render_cell() plugin hook

The render_cell() hook now receives a pks parameter containing the list
of primary key column names for the table being rendered. This avoids
plugins needing to make redundant async calls to look up primary keys.

For tables without an explicit primary key, pks is ["rowid"]. For custom
SQL queries and views, pks is an empty list [].

https://claude.ai/code/session_01HFYfevAziq4fSYTNRD9ZCh
This commit is contained in:
Claude 2026-02-17 18:21:25 +00:00 committed by Simon Willison
commit 170f9de774
8 changed files with 62 additions and 5 deletions

View file

@ -204,11 +204,57 @@ async def test_hook_render_cell_demo(ds_client):
"column": "content",
"table": "simple_primary_key",
"database": "fixtures",
"pks": ["id"],
"config": {"depth": "table", "special": "this-is-simple_primary_key"},
"render_cell_extra": 1,
}
@pytest.mark.asyncio
async def test_hook_render_cell_pks_single_pk(ds_client):
"""pks should be ["id"] for a table with a single primary key"""
response = await ds_client.get("/fixtures/simple_primary_key?id=4")
soup = Soup(response.text, "html.parser")
td = soup.find("td", {"class": "col-content"})
data = json.loads(td.string)
assert data["pks"] == ["id"]
@pytest.mark.asyncio
async def test_hook_render_cell_pks_compound_pk(ds_client):
"""pks should list all primary key columns for a compound pk table"""
response = await ds_client.get("/fixtures/compound_primary_key?pk1=d&pk2=e")
soup = Soup(response.text, "html.parser")
td = soup.find("td", {"class": "col-content"})
data = json.loads(td.string)
assert data["pks"] == ["pk1", "pk2"]
@pytest.mark.asyncio
async def test_hook_render_cell_pks_rowid_table(ds_client):
"""pks should be ["rowid"] for a table with no explicit primary key"""
response = await ds_client.get(
"/fixtures/no_primary_key?content=RENDER_CELL_DEMO"
)
soup = Soup(response.text, "html.parser")
td = soup.find("td", {"class": "col-content"})
data = json.loads(td.string)
assert data["pks"] == ["rowid"]
@pytest.mark.asyncio
async def test_hook_render_cell_pks_custom_sql(ds_client):
"""pks should be [] for custom SQL queries"""
response = await ds_client.get(
"/fixtures/-/query?sql=select+'RENDER_CELL_DEMO'+as+content"
)
soup = Soup(response.text, "html.parser")
td = soup.find("td", {"class": "col-content"})
data = json.loads(td.string)
assert data["pks"] == []
assert data["table"] is None
@pytest.mark.asyncio
@pytest.mark.parametrize(
"path",