Original title: Generating URL for a row inside render_cell hook
Hey,
I am using Datasette to view a database that contains video metadata. It has BLOB columns that contain video thumbnails in JPG format (around 100-500KB per row).
I've registered an output formatter that extends datasette.blob_renderer.render_blob function and serves the column with image/jpeg content type.
from datasette.blob_renderer import render_blob
async def render_jpg(datasette, database, rows, columns, request, table, view_name):
response = await render_blob(datasette, database, rows, columns, request, table, view_name)
response.content_type = "image/jpeg"
response.headers["Content-Disposition"] = f'inline; filename="image.jpg"'
return response
@hookimpl
def register_output_renderer():
return {
"extension": "jpg",
"render": render_jpg,
"can_render": lambda: True,
}
This works well. I can visit http://localhost:8001/mydb/videos/1.jpg?_blob_column=thumbnail and view the image.
I want to display the image directly with an <img> tag (lazy-loaded of course). So, I need a URL, because embedding base64 would increase the page size too much (each image > 100KB).
Datasette generates a link with .blob extension for blob columns. It does this by calling datasette.urls.row_blob
|
display_value = jinja2.Markup( |
|
'<a class="blob-download" href="{}"><Binary: {} byte{}></a>'.format( |
|
self.ds.urls.row_blob( |
|
database, |
|
table, |
|
path_from_row_pks(row, pks, not pks), |
|
column, |
|
), |
|
len(value), |
|
"" if len(value) == 1 else "s", |
|
) |
But I have no way of getting the row inside the render_cell hook.
@hookimpl
def render_cell(value, column, table, database, datasette):
if isinstance(value, bytes) and imghdr.what(None, value):
# generate url
return '$renderedLink'
Any pointers?
Original title: Generating URL for a row inside
render_cellhookHey,
I am using Datasette to view a database that contains video metadata. It has BLOB columns that contain video thumbnails in JPG format (around 100-500KB per row).
I've registered an output formatter that extends
datasette.blob_renderer.render_blobfunction and serves the column withimage/jpegcontent type.This works well. I can visit
http://localhost:8001/mydb/videos/1.jpg?_blob_column=thumbnailand view the image.I want to display the image directly with an
<img>tag (lazy-loaded of course). So, I need a URL, because embedding base64 would increase the page size too much (each image > 100KB).Datasette generates a link with
.blobextension for blob columns. It does this by callingdatasette.urls.row_blobdatasette/datasette/views/table.py
Lines 169 to 179 in 7a2ed9f
But I have no way of getting the row inside the
render_cellhook.Any pointers?