Tipfy: How to display blob in a template?

Given that gae uses tipfy (python) the following model:

greeting.avatar = db.Blob(avatar) 

What is a tag template for displaying a drop (here is an image)?

+3
source share
2 answers

In this case, blob is an image that is great. Just use images.get_serving_url(blob_key) and you are happy. I have used this feature and trust me, it is awesome for image service. Just add =sxx to the url, where xx is the size of the pixel you want. It automatically resizes the image and maintains it.

If the drop is not an image, you're out of luck wrt Perhaps use BlobReader to create a string representation and output it?

If this is not an image or text, what might you want to write in HTML?

+2
source

There is no built-in template tag to display such arbitrary data. I can imagine two approaches:

  • Write a request handler to serve avatars as images. Based on your example, the request handler just needs to find a greeting to get the image data, send the corresponding Content-Type: image/jpeg header Content-Type: image/jpeg (or image/png or whatever), and then write the blob data to the response stream.

    Then in your template you will see the image as follows:

     <img src="/show-avatar/{{ greeting.key().id() }}"> 
  • Create your own template tag / template filter that takes blob data and generates a suitable data URL that encodes the image data directly in the HTML document that you create.

    Then in your template you will see an image something like this:

     <img src="{{ greeting.avatar|dataurl }}"> 

    which will lead to output on these lines in the template:

     <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IAAAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1JREFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C7OwQg2JoQ9LE1exdlYvBBeZ7jqch9//q1uH4TLzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI97CER3N0vr4MkhoXe0rZigAAAABJRU5ErkJggg==" /> 
+1
source

Source: https://habr.com/ru/post/1333821/


All Articles