如何在Plone中使用matplotlib [英] How to use matplotlib in Plone
问题描述
我正在将Plone 4.3与matplotlib一起使用.我有绘图窗口,但是我想在页面模板中渲染绘图窗口.有些人建议我使用HTML5 canvas
在模板页面中绘制绘图窗口.但是我无法理解这个概念.有人可以帮助我吗?
I am using Plone 4.3 with matplotlib. I have the plot window but I want to render the plot window in a page template. Some folks suggested I use HTML5 canvas
to render the plot window in a template page. But I am unable to grasp that concept. So anyone can help me?
推荐答案
创建浏览器视图,设置标题类型,然后返回图像数据,例如
Create a browser view, set the header type, and return the image data e.g.
from zope.publisher.browser import BrowserPage
class PloneMatPlotLib(BrowserPage):
"""
"""
def __call__(self):
import cStringIO
from matplotlib.figure import Figure
from matplotlib.backends.backend_agg import FigureCanvasAgg
x, y = 4, 4
fig = Figure(figsize=[x, y])
ax = fig.add_axes([.1, .1, .8, .8])
ax.scatter([1, 2], [3, 4])
canvas = FigureCanvasAgg(fig)
# write image data to a string buffer and get the PNG image bytes
buf = cStringIO.StringIO()
canvas.print_png(buf)
data = buf.getvalue()
# write image bytes back to the browser
self.request.response.setHeader("Content-type", "image/png")
return data
然后您可以(通过网络)将此视图称为TTW以获取图像,例如http://localhost:8080/Plone/@@plone_matplotlib
.或在页面模板中,例如:
You can then call this view TTW (through the web) to get the image e.g. http://localhost:8080/Plone/@@plone_matplotlib
. Or in a page template e.g.:
<div metal:use-macro="here/main_template/macros/master">
<div metal:fill-slot="main">
<img tal:attributes="src python:context.absolute_url() + '/@@plone_matplotlib'">
</div>
</div>
更多信息:
这篇关于如何在Plone中使用matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!