Im trying to render a simple graph that i create with matplotlib in my template. I want to render the graph in the body of the page not just return an HttpResponse with that graph. But i cant get it to work. Here is what i tried.
views.py code
def showGraph(request):
content_x = [10, 20, 30]
content_y = ['one', 'two', 'three']
plt.bar(content_x, content_y)
plt.title('Some chart')
plt.show()
buffer = io.BytesIO()
canvas = plt.get_current_fig_manager().canvas
canvas.draw()
graphIMG = PIL.Image.frombytes('RGB', canvas.get_width_height(), canvas.tostring_rgb())
graphIMG.save(buffer, "PNG")
content_type = "Image/png"
buffercontent = buffer.getvalue()
graphic = (buffercontent, content_type)
plt.close()
return render(request, 'projectRelated/LAN/graphs.html', {'graphic': graphic})
html page code
{% extends 'base.html' %}
{% load static %}
{% block container_content %}
<img src="data:image/png;base64,{{ graphic }}">
{% endblock container_content %}
The above code just displays a random icon..