简体   繁体   English

在Jinja2模板中比较对称词典

[英]Comparing Symmetric Dictionaries in Jinja2 template

I am passing two symmetric dictionaries to a jinja2 template for rendering. 我将两个对称字典传递给jinja2模板进行渲染。 These two dictionaries are installed and required. 这两个字典已安装并且是必需的。 I want to compare the values in installed and required to ensure they are the same version. 我想比较已安装和必需的值,以确保它们是相同的版本。 The version numbers are stored as strings. 版本号存储为字符串。 If the versions are not equal, I want to highlight that row in my table. 如果版本不相等,我想突出显示表格中的该行。

The values print out correctly, but the comparison always fails. 值正确打印,但比较总是失败。

<table border="1">
  <tr>
    <td>Package</td>
    <td>Version Required</td>
    <td>Version Installed</td>
  </tr>

{% for key, value in required.items() %}
{% if installed[key] == required[key] %} 
    <tr>
    <td>{{key}}</td>
    <td>{{value}}</td>
    <td>{{installed[key]}}</td>
    </tr>
{% else %}
    <tr bgcolor="#ff0000">
    <td>{{key}}</td>
    <td>{{value}}</td>
    <td>{{installed[key]}}</td>
    </tr>
{% endif %} 
{% endfor %}
</table>

It is better to keep logic out of jinja2 template as much as possible and create well usable data in Python code: 最好将逻辑尽可能地远离jinja2模板,并在Python代码中创建可用的数据:

render.py

Instead of two dictionaries, we may pass in a list with tuples (package_name, req_ver, installed_ver) 代替两个字典,我们可以传入一个带有元组的列表(package_name, req_ver, installed_ver)

from jinja2 import Template

with open("table.jinja2") as f:
    templ = Template(f.read())
required = {"boto": "1.1.1", "plac": "2.2.3", "pyyaml": "3.3.3", "jinja2": "4.4.4"}
installed = {"boto": "1.1.1", "plac": "2.2.2", "pyyaml": "3.3.3", "jinja2": "4.4.4"}

packages = [(key, installed[key], required[key]) for key in sorted(installed)]

print templ.render(packages=packages)

table.jinja2

In the template, we may modify just the bgcolor , either by hardcoded color code, or even better by changing a style (not shown here). 在模板中,我们可以只修改bgcolor ,要么通过硬编码的颜色代码进行修改,要么通过更改样式进行更好的修改(此处未显示)。

I also used white space controll - inside the control structures. 我还用空格控制研究-控制结构内。

<table border="1">
    <tr><td>Package</td> <td>Version Required</td><td>Version Installed</td></tr>
{%- for name, ver_required, ver_installed in packages %}
    <tr{% if ver_required != ver_installed %} bgcolor="#ff0000" {% endif %}>
        <td>{{name}}</td> <td>{{ver_required}}</td> <td>{{ver_installed}}</td>
    </tr>
{%- endfor %}
</table>

Run it: 运行:

$ python render.py >tabl.html

And preview the table in web browser. 并在Web浏览器中预览表格。

<table border="1">
    <tr><td>Package</td> <td>Version Required</td><td>Version Installed</td></tr>
    <tr>
        <td>boto</td> <td>1.1.1</td> <td>1.1.1</td>
    </tr>
    <tr>
        <td>jinja2</td> <td>4.4.4</td> <td>4.4.4</td>
    </tr>
    <tr bgcolor="#ff0000" >
        <td>plac</td> <td>2.2.2</td> <td>2.2.3</td>
    </tr>
    <tr>
        <td>pyyaml</td> <td>3.3.3</td> <td>3.3.3</td>
    </tr>
</table>

Jan's answer is excellent, but I also found the specific issue I was having. Jan的回答很好,但我也发现了自己遇到的特定问题。 The problem was one of the strings had a special character on the end, while the other did not. 问题是其中一个字符串的末尾有特殊字符,而另一个则没有。

I was able to fix the code by adding .rstrip("\\r\\n") to the end of each string. 我可以通过在每个字符串的末尾添加.rstrip(“ \\ r \\ n”)来修复代码。

You can tell if you are having a similar issue by putting each string into len() and outputting their sizes. 您可以通过将每个字符串放入len()并输出其大小来判断是否遇到类似的问题。 If they are not equal, then there is a special character. 如果它们不相等,则有一个特殊字符。

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

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