jinja2 encoding error in python -
so trying use jinja2 simple html template keep getting error when call render()
:
warning: ironpythonevaluator.evaluateironpythonscript operation failed. traceback (most recent call last): file "c:\program files (x86)\ironpython 2.7\lib\jinja2\jinja2\loaders.py", line 125, in load file "c:\program files (x86)\ironpython 2.7\lib\jinja2\jinja2\environment.py", line 551, in compile file "c:\program files (x86)\ironpython 2.7\lib\jinja2\jinja2\environment.py", line 470, in _parse file "c:\program files (x86)\ironpython 2.7\lib\jinja2\jinja2\parser.py", line 31, in __init__ file "c:\program files (x86)\ironpython 2.7\lib\jinja2\jinja2\environment.py", line 501, in _tokenize file "c:\program files (x86)\ironpython 2.7\lib\jinja2\jinja2\environment.py", line 494, in preprocess file "<string>", line 21, in <module> file "c:\program files (x86)\ironpython 2.7\lib\jinja2\jinja2\environment.py", line 812, in get_template file "c:\program files (x86)\ironpython 2.7\lib\jinja2\jinja2\environment.py", line 786, in _load_template unicodeencodeerror: ('unknown', '\x00', 0, 1, '')
now understand jinja2 works utf-8 forcing python code formatting:
#-*- coding: utf-8 -*- import clr import sys pyt_path = r'c:\program files (x86)\ironpython 2.7\lib' sys.path.append(pyt_path) pyt_path1 = r'c:\program files (x86)\ironpython 2.7\lib\markupsafe' sys.path.append(pyt_path1) pyt_path2 = r'c:\program files (x86)\ironpython 2.7\lib\jinja2' sys.path.append(pyt_path2) #the inputs node stored list in in variable. dataenteringnode = in jinja2 import environment, filesystemloader j2_env = environment(loader=filesystemloader(r'c:\users\ksobon\documents\visual studio 2015\projects\website2'), trim_blocks=true) temp = j2_env.get_template('htmlpage2.html') out = temp.render(svgwidth = r'500')
here's template html file trying use:
<!doctype html> <meta charset="utf-8"> <style> .chart rect { fill: steelblue; stroke: white; } </style> <svg class="chart"></svg> <script src="http://d3js.org/d3.v3.min.js"></script> <script> var data = [ { name: "locke", value: 42 }, { name: "reyes", value: 8 }, { name: "ford", value: 15 }, { name: "jarrah", value: 16 }, { name: "shephard", value: 23 }, { name: "kwon", value: 42 } ]; var w = {{svgwidth}}, h = 400; var x = d3.scale.linear() .domain([0, 1]) .range([0, w]); var y = d3.scale.linear() .domain([0, 100]) .rangeround([0, h]); var chart = d3.select("body") .append("svg:svg") .attr("class", "chart") .attr("width", w * data.length - 1) .attr("height", h); chart.selectall("rect") .data(data) .enter().append("svg:rect") .attr("x", function (d, i) { return x(i) - .5; }) .attr("y", function (d) { return h - y(d.value) - .5; }) .attr("width", w) .attr("height", function (d) { return y(d.value); }); </script>
any ideas doing wrong? in ironpython2.7
this known bug in ironpython. can work around editing jinja2 source:
using jinja2 on ironpython non-ascii characters in template result in error. replace
to_string = unicode
to_string = lambda x: unicode(x)
near top of runtime.py work around issue.
Comments
Post a Comment