i have model image stored binary blob. want display image, along other data object, in template. since image not separate file, can't figure out how display it. i've tried setting headers, or using send_file or render_template, either don't image or only image , not rest of template. how can display binary blob image in template?
class a(ndb.model): id= ndb.integerproperty() x= ndb.stringproperty() y= ndb.stringproperty() image = ndb.blobproperty()
the image stored bytes. encode base64 , insert data uri in rendered html. can pass both object , encoded image template.
from base64 import b64encode @app.route('/show/<int:id>') def show(id): obj = a.query(a.id == id).fetch(1)[0] image = b64encode(obj.image) return render_template('show_a.html', obj=obj, image=image) <p>{{ obj.x }}<br/> {{ obj.y }}</p> <img src="data:;base64,{{ image }}"/> this sub-optimal, because data uris sent every time page rendered, while image file can cached client. better store image file in directory, store path in database, , serve image file separately.
Comments
Post a Comment