I was looking for a simple way to display an image stored in a database inside Perspective, and I accidentally found a very clean solution.
No Blob module, no WebDev, no endpoints — just pure Python inside a component change script.
This approach works with DB column containing raw image bytes (BLOB).
Named Query example
Query name: img_select
Path: MyProject/img_select
sql
SELECT img AS "Content", 'image/png' AS "ContentType"
FROM MyTable
WHERE description = :name
Perspective change script example
python
import base64
result = system.db.runNamedQuery("MyProject", "img_select", {"name": currentValue.value})
content = result.getValueAt(0, "Content") if result.getRowCount() else None
# If no image → send a 1×1 transparent PNG
if not content or len(content) == 0:
blank_png_base64 = (
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8"
"/w8AAn8B9p2u3wAAAABJRU5ErkJggg=="
)
value = "data:image/png;base64," + blank_png_base64
self.getSibling("Image").props.source = value
else:
# Convert BLOB → base64
b64 = base64.b64encode(content).decode("utf-8")
value = "data:image/png;base64," + b64
# Send to the Image component
self.getSibling("Image").props.source = value
Notes
-
No WebDev or file endpoints needed.
-
The Image component accepts base64 directly via
props.source.