mardi 5 mai 2015

Render matplotlib graph into html via views in django

I have a django view as follows. I am using matplotlib to generate a bargraph.

def myview(request, pk, sub):
    x=[1,2,3,4,5,6]
    y=[5,2,6,8,2,7]
    plot(x,y, linewidth=2)
    xlabel('x-axis')
    ylabel('yaxis')
    title('sample graph')
    grid(True)

    buffer = StringIO.StringIO()
    canvas = pylab.get_current_fig_manager().canvas
    canvas.draw()
    graphIMG = PIL.Image.fromstring("RGB", canvas.get_width_height(), canvas.tostring_rgb())
    graphIMG.save(buffer, "PNG")
    pylab.close()

    assgn = MyModel.objects.filter(data=pk, subject=sub)
    return render_to_response('mypage.html', {'assgn':assgn})

How do i pass the bargraph along with my query in my render_to_response ? Also how will i plot this graph in my html?

Aucun commentaire:

Enregistrer un commentaire