Creating HTML wrapper in django for matplotlib images

I would like to use ppton generated matplotlib images and embed them in the HTML page that django creates. I'm relatively new to django and struggling to get this to work. I can only successfully create a matplotlib image on a web page, but could not insert it into the HTML page. Django makes sense, since my application will have many users who will have user views with different data and often change the data coming from the database. I would like to avoid creating many static files.

I looked through a few posts, but I obviously missed something. For example: Creating dynamic diagrams using Matplotlib in Django , images on the django site from matplotlib, and dynamically displaying matplotlib images on the Internet using python .

I am creating an image in the form of matplotlib with temp, and I think the cover is the details. the details do not seem to work. File name plotdata.py and sample survey django tutorial

from datetime import datetime, time
from django.http import HttpResponse
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
from django.contrib.auth.decorators import login_required
from django.shortcuts import render

@login_required()
def temp(request,x_id):

    #... code to generate fig for ploting - works well

    #This works but does not seem to pass file to HTML
    canvas = FigureCanvas(fig)
    response = HttpResponse(content_type='image/png')
    canvas.print_png(response)
    return response

@login_required()
def detail(request, x_id):
    render(request, 'polls/plotdata.html', {'x_id': x_id})

My urls.py looks like this. temp works fine

from django.conf.urls import patterns, url
from django.views.generic import DetailView, ListView
from polls.models import Poll
from polls import plotdata

urlpatterns = patterns('',
    #polls url chopped out for brevity - follows tutorial
    url(r'^(?P<x_id>\d+)/plotdata/temp.png$', plotdata.temp, name='temp'),
    url(r'^(?P<x_id>\d+)/plotdata/detail$', plotdata.detail, name='detail'),
)

My plotdata.html is as follows

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="style.css" />
    <title>{% block title %}Plotting Template{% endblock %}</title>
</head>

<body>
    <div id="content">
        {% block content %}
        <img src="{% url 'temp' x_id %}" >
        {% endblock %}
    </div>
</body>
</html>

The generated error is as follows.

NoReverseMatch at /polls/1303070002/plotdata/detail
Reverse for 'temp' with arguments '('1303070002',)' and keyword arguments '{}' not found.

This is probably not the only problem with the above. I'm sure I missed something critical.

I tried hardcoding as a test,

<img src="/polls/1303070002/plotdata/temp.png" >

but he created the following error

ValueError at /polls/1303070002/plotdata/detail
The view polls.plotdata.detail didn't return an HttpResponse object.

, , . . !

, plotadata.py,

    #same header information from above before this line        
    canvas = FigureCanvas(fig)      #This needs to remain for savefig or print_png command
    response = HttpResponse(content_type='image/png')
    fig.savefig(response, format='png')     #Produces all white background
    return response

def detail(request, salesorder_id):
    return render(request, 'rsa/plotdata.html', {'x_id':x_id})

... hmtl, . savefig print_png, . urls.py - . plotdata.html , , url {{x_id}}, - . , , NoReverseMatch. , plotdata.html

<img src="{% url 'temp' x_id %}" >

{% load staticfiles %}
<img src="{% static '/rsa/1303070001/plotdata/temp.png' %}" >

. ,

<img src="{% static '/rsa/{{ x_id }}/plotdata/temp.png' %}" >

x_id = > /rsa/%7B%7B%20x_id%20%7D%7D/plotdata/temp.png. x_id | safe ...% 7B% 7B %20x_id% 7Csafe %20% 7D% 7D. url static. . , - , , x_id

+3

All Articles