Reputation: 11879
I have a Django application, with charts from Googles image chart api. I want to replace the charts (they are depreciated), and start using Matplotlib.
So I have followed the example here, to get Django / Matplot to return a png:
http://wiki.scipy.org/Cookbook/Matplotlib/Django.
Now, if I want to return an HTML page, with Matplotlib images embedded, how do I go about that? I can see two options.
1: Should I set up the HTML as one view, with another view for the chart, treating these two parts separately (I guess data would be encoded in the image URL). The HTML will be rendered, and as the image tag is rendered , it makes the second call to Django, and the chart is generated. This seems easy enough to set up, but also seems like the chart data will be passed back and forward unnecessary.
2: Or can I generate the Matplot lib images at the same time as the HTML, have the png's ready and send them at the same time? I guess I would have to write the file to where static files are served from. And what would be considered best practice for a naming scheme for files / cleaning up afterwards?
Is there one method that is considered best practice over the other for this sort of thing? I assume it isn't so uncommon.
Upvotes: 1
Views: 1668
Reputation: 599480
The first option would almost certainly be easier. Just map a URL to the view that calls the matplotlib code, and return the image data there.
For the second option, you could embed image data in HTML by using a data
uri as the source of your image in the template, but this doesn't really seem to be necessary.
Upvotes: 2