Render canvas object (figure) to html files(templates) in Django.

Hi all,

I am using matplotlib with Django.

I want to display figures processed in Django apps by matplotlib in browser using html5.

I tried:

def plot_file(request):

import matplotlib.cbook as cbook

fig = figure()

fname = cbook.get_sample_data(‘msft.csv’, asfileobj=False)

plotfile(fname, (0,1,2,3))

canvas = FigureCanvas(fig)

response = HttpResponse(content_type=“image/png”)

canvas.print_png(response)

fig.clear()

return response

This response takes whole page and displays figure.

I have other contents as well to display along with figure.

How can I send it as a canvas object or something else(I am unaware of) in order to have print logic in html file(django templates)

in order to position figure as my requirement:

I thought of something like this, but not sure:

def plot_file(request):

···

---------------

---------------

return render_to_response(‘template.html’,{

’canvas’:canvas,

**}, **

context_instance=RequestContext(request),

**) **


Please make me inform, If I need to provide more context.

Regards,

Navid Shaikh.

We have fairly recently merged in a WebAgg backend that makes it possible for matplotlib to serve out an interactive figure window, using html5. This is currently in the master branch and has not been released yet. Don’t know how well it would work within Django, but might be worth investigating.

https://github.com/matplotlib/matplotlib/pull/1426

Ben Root

···

On Fri, Jan 11, 2013 at 6:14 AM, Navid Shaikh <shaikhnavid14@…287…> wrote:

Hi all,

I am using matplotlib with Django.

I want to display figures processed in Django apps by matplotlib in browser using html5.

I tried:

def plot_file(request):

import matplotlib.cbook as cbook

fig = figure()

fname = cbook.get_sample_data(‘msft.csv’, asfileobj=False)

plotfile(fname, (0,1,2,3))

canvas = FigureCanvas(fig)

response = HttpResponse(content_type=“image/png”)

canvas.print_png(response)

fig.clear()

return response

This response takes whole page and displays figure.

I have other contents as well to display along with figure.

How can I send it as a canvas object or something else(I am unaware of) in order to have print logic in html file(django templates)

in order to position figure as my requirement:

I thought of something like this, but not sure:

def plot_file(request):

---------------

---------------

return render_to_response(‘template.html’,{

’canvas’:canvas,

**}, **

context_instance=RequestContext(request),

**) **


Please make me inform, If I need to provide more context.

Regards,

Navid Shaikh.