我只是在用已經寫好的python應用程序來研究Jinja2。我可能會以錯誤的方式解決這個問題,但這是我想要做的。Jinja2/Python將圖像插入到html中
from jinja2 import Environment, FileSystemLoader
from weasyprint import HTML
env = Environment(loader=FileSystemLoader('.'))
template = env.get_template("really.html")
template_vars = {"title":"TITLE","graph":'total.png'}
html_out = template.render(template_vars)
HTML(string=html_out).write_pdf("report.pdf")
這幾乎產生我想要什麼,我得到了一個名爲report.pdf PDF格式,但附加的文件,而不是,它是total.png的字符串。這是我第一次使用Jinja,所以希望附上這樣的圖像是可能的。謝謝。
這是模板,沒有多少內容,只是試圖做這件作品。
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>{{ title }}</title>
</head>
<body>
<h2>Graph Goes Here</h2>
{{ graph }}
</body>
</html>
我們展示模板 – yedpodtrzitko