简体   繁体   English

Mako模板-设置变量

[英]Mako template - setting variable

I have a python module builds different charts / graphs using pyplot. 我有一个python模块使用pyplot构建不同的图表/图形。 The method I'm including in my Mako template returns a string which is the absolute path to the image it produces. 我在Mako模板中包含的方法返回一个字符串,这是它生成的图像的绝对路径。 I'm trying to include this image in my html site, but keep getting a syntax error with Mako. 我正在尝试将此图像包含在我的html网站中,但是Mako不断出现语法错误。 Here is how everything looks: 这是所有内容的外观:

mako_template.html mako_template.html

<!DOCTYPE html>
<%! from my_plotter import MyPlotter>
<html>
.
.
<%
    plotter = MyPlotter()
    image = plotter.build_graph()
    <img src="${image}" height="200" width="100%">
%>
.
.
</html>

I can call plotter.build_graph() without a problem, but as soon as I assign a variable from its returned value I get a syntax error. 我可以毫无问题地调用plotter.build_graph(),但是一旦我从其返回值中分配了一个变量,就会出现语法错误。

In the tag <% %> you put only python code, you can save your image using 在标记<%%>中,您仅放置了python代码,您可以使用

savefig('image.png')

and then in the html access to it with the img tag 然后在html中使用img标签对其进行访问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM