Re: How to make an action generate html?
Ralf Schlatterbeck <[email protected]> Wed, 14 Feb 2024 11:37:01 +0100
| Newsgroups | gmane.comp.bug-tracking.roundup.devel |
|---|---|
| Message-ID | <[email protected]> |
On Tue, Feb 13, 2024 at 01:01:24PM -0500, John P. Rouillard wrote:
> Hi all:
>
> I am trying to find out how to return a templated page from an action.
> My thought is that the page template would be something like:
>
> index.chart.html
>
> and would be expanded/returned from the action with the action somehow
> embedding an svg as the main content.
I've done a similar thing: Return a picture (plotted with matplotlib)
from an action. The idea is to simply return the contents from the
handle() method.
This is not 'templated' in that it doesn't use roundup's templating
engine. But it is computed contents.
Some code here:
class Log_Status_Statistics (actions.Action, _Helper):
def handle (self, outfile = None):
...
h = self.client.additional_headers
h ['Content-Type'] = 'image/png'
self.client.header ()
if self.client.env ['REQUEST_METHOD'] == 'HEAD':
return 'dummy'
io = outfile
if io is None:
io = self.client.request.wfile
sio = BytesIO ()
pyplot.axis ('equal')
keys = ['service', 'down', 'up']
colr = dict (service = '#9999FF', down = '#FF8989', up = '#C0DDFF')
st = [stat [k] for k in keys if stat [k]]
lbl = [k for k in keys if stat [k]]
clr = [colr [k] for k in keys if stat [k]]
matplotlib.rc ('font', size = 24)
pyplot.title ('Average Uptime')
pyplot.pie (st, labels = lbl, autopct="%1.2f%%", colors = clr)
pyplot.savefig (sio, dpi=100, format='png', transparent=True)
pyplot.clf ()
v = sio.getvalue ()
sio.close ()
return v
Hope that helps!
Kind regards,
Ralf
--
Dr. Ralf Schlatterbeck Tel: +43/2243/26465-16
Open Source Consulting www: www.runtux.com
Reichergasse 131, A-3411 Weidling email: [email protected]