我正在使用带有matplotlib的Plone 4.3。我有绘图窗口,但我想在页面模板中渲染绘图窗口。有些人建议我使用HTML5 canvas
在模板页面中渲染绘图窗口。但我无法理解这个概念。所以任何人都可以帮助我?
答案 0 :(得分:8)
创建浏览器视图,设置标题类型,然后返回图像数据,例如
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
。或者在页面模板中,例如:
<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>
更多信息: