non ascii code in template for cheetah generator erorr

229 views
Skip to first unread message

Eelco F

unread,
Nov 5, 2016, 6:10:22 PM11/5/16
to weewx-user
Hi,

I'm trying to include some html from an external source in one of my templates. However these html contains some non-ascii symbols like the degree symbol or ë, á ï for instance.
These html files are encoded in UTF-8.

The cheetah generator doesn't accept them:

Traceback (most recent call last):
Nov  5 23:05:17 weerstation weewx[1367]: ****    File "/usr/share/weewx/weewx/cheetahgenerator.py", line 315, in generate
Nov  5 23:05:17 weerstation weewx[1367]: ****      print >> _file, text
Nov  5 23:05:17 weerstation weewx[1367]: ****    File "/usr/lib/python2.7/dist-packages/Cheetah/Template.py", line 1005, in __str__
Nov  5 23:05:17 weerstation weewx[1367]: ****      rc = getattr(self, mainMethName)()
Nov  5 23:05:17 weerstation weewx[1367]: ****    File "cheetah__etc_weewx_skins_Aangepast_beoordeling_html_tmpl_1478382317_27_88088.py", line 90, in respond
Nov  5 23:05:17 weerstation weewx[1367]: ****    File "/usr/lib/python2.7/dist-packages/Cheetah/Template.py", line 1597, in _handleCheetahInclude
Nov  5 23:05:17 weerstation weewx[1367]: ****      nestedTemplateClass = compiler.compile(source=source, file=file)
Nov  5 23:05:17 weerstation weewx[1367]: ****    File "/usr/lib/python2.7/dist-packages/Cheetah/Template.py", line 740, in compile
Nov  5 23:05:17 weerstation weewx[1367]: ****      settings=(compilerSettings or {}))
Nov  5 23:05:17 weerstation weewx[1367]: ****    File "/usr/lib/python2.7/dist-packages/Cheetah/Compiler.py", line 1579, in __init__
Nov  5 23:05:17 weerstation weewx[1367]: ****      source = unicode(source)
Nov  5 23:05:17 weerstation weewx[1367]: ****  UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 720: ordinal not in range(128)

Apparently only the 128 ascii codes are acceptable? So no extended ascii or UTF-8? Is there a way to get this right?

I could use sed to replace these characters, but I would rather leave them in place.....

gjr80

unread,
Nov 5, 2016, 7:17:19 PM11/5/16
to weewx-user
Hi,

So have you explicity set or tried changing the encoding parameter for the report concerned in skin.conf? eg

        [[[testreport]]]
            encoding = html_entities
            template = testreport.tmpl

You can read about the encoding parameter in here in the weewx Customization Guide

Gary

Eelco F

unread,
Nov 6, 2016, 4:36:31 AM11/6/16
to weewx-user
I tried your suggestion, but it still yields the same error.

To be more precise: I have a template which include a general header for my webpages. Further down on the page I want to include a simple html page from our weatherservice.
I could do this with an iframe, but then I get problems with the css. So I just wanted the template to include the html.

The template contains the line:

#include "/dir/weather_service.html"
weather_service.html is in UTF-8 encoding.

the error: File "/usr/lib/python2.7/dist-packages/Cheetah/Compiler.py", line 1579, in __init__
Nov  6 10:30:16 weerstation weewx[1367]: ****      source = unicode(source)
Nov  6 10:30:16 weerstation weewx[1367]: ****  UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 751: ordinal not in range(128)


Op zondag 6 november 2016 00:17:19 UTC+1 schreef gjr80:

Andrew Milner

unread,
Nov 6, 2016, 4:51:00 AM11/6/16
to weewx-user
If the html you are including is complete try
#include raw "/dir/weather_service.html"

Andrew Milner

unread,
Nov 6, 2016, 5:03:06 AM11/6/16
to weewx-user
.... or possibly, at the start of the template
#encoding UTF-8

to say that the source is UTF-8 encoded - both these suggestions come from reading the cheetah user guide:

Eelco F

unread,
Nov 6, 2016, 2:40:09 PM11/6/16
to weewx-user
All right, this seems to do the trick. Haven tried your second suggestion, but thanks al lot!!

Op zondag 6 november 2016 10:51:00 UTC+1 schreef Andrew Milner:
Reply all
Reply to author
Forward
0 new messages