使用matplotlib与Django通用视图 [英] Using matplotlib with Django Generic views

查看:271
本文介绍了使用matplotlib与Django通用视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为表中的每一行数据写一个详细的页面。我想使用每行的数据为每个页面动态生成一个matplotlib图。



我已经尝试使用正常视图的代码,它的工作原理。但是,当与 detail_view 页面一起使用时,该图像将显示为断开的链接。我应该在 DetailView 类中添加什么来生成每个页面的图表?



graph.py

  def plotResults(request):
p = get_object_or_404(Read_alignment, pk = id)
x = []
y = []
x.append(p.start)
x.append(p.stop)
y.append p.count)
y.append(p.count)
fig = plt.figure()
ax = fig.add_subplot(311)
ax.set_xlim(right = 30 )
ax.step(x,y,' - ',其中='post',label ='post')
canvas = FigureCanvas(fig)
response = HttpResponse(mimetype =图像/ png')
canvas.print_png(响应)
返回响应






url.py

  url(r'^(?P pk> \d +)/ align / plotResults.png $',plotResults),
url(r'^(?P< pk> \d +)/ align / $',AlignDetailView.as_view(),name ='AlignDetailView'),






views.py

  class AlignDetailView(DetailView):
model = Read_alignment

def get_queryset(self):
queryset = Read_alignment.objects.filter(chr__icontains = 3l)
return queryset

def get_context_data(self,** kwargs):
context = super(AlignDetailView,self).get_context_data(** kwargs)
context ['alignment'] = self.object
返回上下文

我应该如何将图形链接到模板,而不是静态或媒体标签?可以生成图形而不将PNG图像保存到静态文件夹中?

解决方案

有两种方法可以依赖关于你想要做什么。


  1. 如果你只想要一个没有任何HTML的图像,那么只需使用一个正常的函数视图rand返回一个 HttpResponse 或者如果你真的真的想使用基于类的视图覆盖 get 方法和返回一个 HttpResponse


  2. 如果你想显示嵌入其中某处的图像的HTML页面。然后你可以做两件事情


    1. 创建一个单独的视图,只会像您在示例中一样响应图像,在您的HTML页面中,只需添加< img src =路径到该视图url/>


    2. 或者如果您不想单独查看图像,则必须在DetailView中创建一个图像,将其保存为字节流,base64将其编码并返回到上下文中(例如)图像。然后在您的模板中,使用< img src =data:image / png; base64,{{image}}/> 。




I am trying to write a detailed page for each row of data in the table. I would like to generate a matplotlib graph dynamically for each page using the data from each row.

I have tried the code with a normal view and it works. However, when used with the detail_view page the image appears as a broken link. What should I include in the DetailView class to generate the graph for each page?

graph.py:

def plotResults(request):
    p=get_object_or_404(Read_alignment,pk=id)
    x =[]
    y=[]
    x.append(p.start)
    x.append(p.stop)
    y.append(p.count)
    y.append(p.count)
    fig=plt.figure()
    ax= fig.add_subplot(311)
    ax.set_xlim(right=30)
    ax.step(x,y,'-',where='post', label='post')
    canvas = FigureCanvas(fig)
    response= HttpResponse(mimetype='image/png')
    canvas.print_png(response)
    return response


url.py:

url(r'^(?P<pk>\d+)/align/plotResults.png$',plotResults),
url(r'^(?P<pk>\d+)/align/$',AlignDetailView.as_view(), name='AlignDetailView'),


views.py:

class AlignDetailView(DetailView):
    model = Read_alignment

    def get_queryset(self):
        queryset= Read_alignment.objects.filter(chr__icontains=3l)
        return queryset

    def get_context_data(self, **kwargs):
        context = super(AlignDetailView,self).get_context_data(**kwargs)
        context['alignment'] = self.object
        return context

How should I link the graph to the template preferably without static or media tags? Is it possible to generate the graphs without saving the PNG images to the static folder?

解决方案

There are two ways to do this depending on what you want to do.

  1. If you just want an image to be shown without any HTML, then just use a normal function view rand return a HttpResponse or if you really, really really want to use class based views override the get method and return a HttpResponse.

  2. If you want to show an HTML page with the image embedded somewhere in it. Then you can do two things

    1. Create a separate view that will respond with just the image as you did in your example, and in your HTML page just add <img src="path to that view url" />.

    2. Or if you don't want a separate view for the image then you have to create an image in the DetailView, save it to byte stream, base64 encode it and return it in your context as (for example) image. Then in your template you create an image tag with the base64 encoded data using <img src="data:image/png;base64,{{ image }}"/>.

这篇关于使用matplotlib与Django通用视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆