mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Expose current SQLite row to render_cell hook, closes #1300
This commit is contained in:
parent
9f1eb0d4ea
commit
6373bb3414
6 changed files with 14 additions and 7 deletions
|
|
@ -60,7 +60,7 @@ def publish_subcommand(publish):
|
||||||
|
|
||||||
|
|
||||||
@hookspec
|
@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"""
|
"""Customize rendering of HTML table cell values"""
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -375,6 +375,7 @@ class QueryView(DataView):
|
||||||
# pylint: disable=no-member
|
# pylint: disable=no-member
|
||||||
plugin_display_value = None
|
plugin_display_value = None
|
||||||
for candidate in pm.hook.render_cell(
|
for candidate in pm.hook.render_cell(
|
||||||
|
row=row,
|
||||||
value=value,
|
value=value,
|
||||||
column=column,
|
column=column,
|
||||||
table=None,
|
table=None,
|
||||||
|
|
|
||||||
|
|
@ -895,6 +895,7 @@ async def display_columns_and_rows(
|
||||||
# pylint: disable=no-member
|
# pylint: disable=no-member
|
||||||
plugin_display_value = None
|
plugin_display_value = None
|
||||||
for candidate in pm.hook.render_cell(
|
for candidate in pm.hook.render_cell(
|
||||||
|
row=row,
|
||||||
value=value,
|
value=value,
|
||||||
column=column,
|
column=column,
|
||||||
table=table_name,
|
table=table_name,
|
||||||
|
|
|
||||||
|
|
@ -373,12 +373,15 @@ Examples: `datasette-publish-fly <https://datasette.io/plugins/datasette-publish
|
||||||
|
|
||||||
.. _plugin_hook_render_cell:
|
.. _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.
|
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
|
The value that was loaded from the database
|
||||||
|
|
||||||
``column`` - string
|
``column`` - string
|
||||||
|
|
|
||||||
|
|
@ -98,12 +98,13 @@ def extra_body_script(
|
||||||
|
|
||||||
|
|
||||||
@hookimpl
|
@hookimpl
|
||||||
def render_cell(value, column, table, database, datasette):
|
def render_cell(row, value, column, table, database, datasette):
|
||||||
async def inner():
|
async def inner():
|
||||||
# Render some debug output in cell with value RENDER_CELL_DEMO
|
# Render some debug output in cell with value RENDER_CELL_DEMO
|
||||||
if value == "RENDER_CELL_DEMO":
|
if value == "RENDER_CELL_DEMO":
|
||||||
return json.dumps(
|
return json.dumps(
|
||||||
{
|
{
|
||||||
|
"row": dict(row),
|
||||||
"column": column,
|
"column": column,
|
||||||
"table": table,
|
"table": table,
|
||||||
"database": database,
|
"database": database,
|
||||||
|
|
|
||||||
|
|
@ -181,12 +181,13 @@ def test_hook_render_cell_demo(app_client):
|
||||||
response = app_client.get("/fixtures/simple_primary_key?id=4")
|
response = app_client.get("/fixtures/simple_primary_key?id=4")
|
||||||
soup = Soup(response.body, "html.parser")
|
soup = Soup(response.body, "html.parser")
|
||||||
td = soup.find("td", {"class": "col-content"})
|
td = soup.find("td", {"class": "col-content"})
|
||||||
assert {
|
assert json.loads(td.string) == {
|
||||||
|
"row": {"id": "4", "content": "RENDER_CELL_DEMO"},
|
||||||
"column": "content",
|
"column": "content",
|
||||||
"table": "simple_primary_key",
|
"table": "simple_primary_key",
|
||||||
"database": "fixtures",
|
"database": "fixtures",
|
||||||
"config": {"depth": "table", "special": "this-is-simple_primary_key"},
|
"config": {"depth": "table", "special": "this-is-simple_primary_key"},
|
||||||
} == json.loads(td.string)
|
}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue