mmdgz
mmdgz

Reputation: 13

Display multiple pictures generated by matplotlib with parms from POST in Django

I'm trying to create a simple Django Web page for the following requirement:

  1. Submit some parms by POST via Fields in a Form
  2. Get more data from internet or database according to the parms in POST and do some complex calculation. Finally generate two figures and a table of data.
  3. Display the figures and data table on the same page that submitting the parms (so that user can change the parms, submit again, and then see changes)
  4. Don't need to keep figures and data in server.

I'm wondering how to implement this without generating temporary pictures in server.

I checked some questions related to django and matplotlib, but only find examples for either returning one picture stream by writing to Response of a picture ULR, or writing to a temp image file and then embedding to template.

RESULT:

def get_img_url(): imgbuf = cStringIO.StringIO() pylab.savefig(imgbuf,format='png',bbox_inches='tight') imgbuf.seek(0) return "data:image/jpg;base64,%s" % imgbuf.getvalue().encode("base64").strip()

Upvotes: 1

Views: 488

Answers (1)

nivix zixer
nivix zixer

Reputation: 1651

You could generate the image from matplotlib, base64 encode the image data, then write the data to the page via Data URI.

This way you never actually save the images to the server or database, and using Base64 you don't have to deal with binary data.

Upvotes: 2

Related Questions