render_cell(..., request) argument, closes #2007

pull/1999/head
Simon Willison 2023-01-27 19:34:14 -08:00
rodzic 0728c2e41b
commit 226dde0d66
7 zmienionych plików z 30 dodań i 18 usunięć

Wyświetl plik

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

Wyświetl plik

@ -396,6 +396,7 @@ class QueryView(DataView):
table=None,
database=database,
datasette=self.ds,
request=request,
):
candidate = await await_me_maybe(candidate)
if candidate is not None:

Wyświetl plik

@ -51,6 +51,7 @@ class RowView(DataView):
rows,
link_column=False,
truncate_cells=0,
request=request,
)
for column in display_columns:
column["sortable"] = False

Wyświetl plik

@ -783,6 +783,7 @@ class TableView(DataView):
sortable_columns=await self.sortable_columns_for_table(
database_name, table_name, use_rowid=True
),
request=request,
)
metadata = (
(self.ds.metadata("databases") or {})
@ -911,6 +912,7 @@ async def display_columns_and_rows(
link_column=False,
truncate_cells=0,
sortable_columns=None,
request=None,
):
"""Returns columns, rows for specified table - including fancy foreign key treatment"""
sortable_columns = sortable_columns or set()
@ -992,6 +994,7 @@ async def display_columns_and_rows(
table=table_name,
database=database_name,
datasette=datasette,
request=request,
):
candidate = await await_me_maybe(candidate)
if candidate is not None:

Wyświetl plik

@ -380,8 +380,8 @@ Examples: `datasette-publish-fly <https://datasette.io/plugins/datasette-publish
.. _plugin_hook_render_cell:
render_cell(row, value, column, table, database, datasette)
-----------------------------------------------------------
render_cell(row, value, column, table, database, datasette, request)
--------------------------------------------------------------------
Lets you customize the display of values within table cells in the HTML table view.
@ -403,6 +403,9 @@ Lets you customize the display of values within table cells in the HTML table vi
``datasette`` - :ref:`internals_datasette`
You can use this to access plugin configuration options via ``datasette.plugin_config(your_plugin_name)``, or to execute SQL queries.
``request`` - :ref:`internals_request`
The current request object
If your hook returns ``None``, it will be ignored. Use this to indicate that your hook is not able to custom render this particular value.
If the hook returns a string, that string will be rendered in the table cell.

Wyświetl plik

@ -98,23 +98,24 @@ def extra_body_script(
@hookimpl
def render_cell(row, value, column, table, database, datasette):
def render_cell(row, value, column, table, database, datasette, request):
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,
"config": datasette.plugin_config(
"name-of-plugin",
database=database,
table=table,
),
}
)
data = {
"row": dict(row),
"column": column,
"table": table,
"database": database,
"config": datasette.plugin_config(
"name-of-plugin",
database=database,
table=table,
),
}
if request.args.get("_render_cell_extra"):
data["render_cell_extra"] = 1
return json.dumps(data)
elif value == "RENDER_CELL_ASYNC":
return (
await datasette.get_database(database).execute(

Wyświetl plik

@ -187,7 +187,9 @@ async def test_hook_render_cell_link_from_json(ds_client):
@pytest.mark.asyncio
async def test_hook_render_cell_demo(ds_client):
response = await ds_client.get("/fixtures/simple_primary_key?id=4")
response = await ds_client.get(
"/fixtures/simple_primary_key?id=4&_render_cell_extra=1"
)
soup = Soup(response.text, "html.parser")
td = soup.find("td", {"class": "col-content"})
assert json.loads(td.string) == {
@ -196,6 +198,7 @@ async def test_hook_render_cell_demo(ds_client):
"table": "simple_primary_key",
"database": "fixtures",
"config": {"depth": "table", "special": "this-is-simple_primary_key"},
"render_cell_extra": 1,
}