简体   繁体   English

我们如何在jinja2 / weasyprint python中传递图像变量

[英]How can we pass image variables in jinja2/weasyprint python

the problem is that i am trying to create an image variable in python using jinja2 and weasyprint and then, i want to pass that image variable in my HTML template (html_out). 问题是我试图使用jinja2和weasyprint在python中创建一个图像变量,然后,我想在我的HTML模板(html_out)中传递该图像变量。

I was successful in creating the image variable and it appears in my variable explorer. 我成功创建了图像变量,它出现在变量浏览器中。 But passing it to the HTML template by including it in my dictionary was only trouble. 但是通过将其包含在字典中将其传递给HTML模板只是麻烦。

#this is my image variable
art_work = Image.open('D:/Course/report-automation/Huaweii.png')

#this is my dictionary

template_vars = {"title": title, "start_date": start_date, "end_date": end_date, "price": price, 
                 "total_Impressions": total_impressions, "art_work": art_work, "total_Clicks": total_clicks,
                 "cpm":cpm, "percentage_mobile": percentage_mobile}

below is the html template with my variable holder: 

 <div class="image-wrapper">
        {{ art_work }}
      </div>

To display image you should use <img> tag. 要显示图像,您应该使用<img>标签。 Also, just pass the src of image in dictionary. 另外,只需在字典中传递图片的src。

      <div class="image-wrapper">
         <img src=" {{ art_work }}" />
      </div>

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

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