Create a browser view, set the title type and return image data, for example.
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)
buf = cStringIO.StringIO()
canvas.print_png(buf)
data = buf.getvalue()
self.request.response.setHeader("Content-type", "image/png")
return data
Then you can call this TTW view (via the Internet) to get an image, for example. http://localhost:8080/Plone/@@plone_matplotlib. Or in a page template, for example:
<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>
More details here:
source
share