简体   繁体   English

如何在 jinja2 中使用 enumerate(zip(seq1,seq2))?

[英]How to use enumerate(zip(seq1,seq2)) in jinja2?

I'm trying to color some sequences of RNA using Django.我正在尝试使用 Django 为一些 RNA 序列着色。 I'm using enumerate and zip to find equals index in list.我正在使用 enumerate 和 zip 在列表中查找等于索引。 for example:例如:

for i, (a, b) in enumerate(zip(seq1, seq2)):
        if a == b and i not in green:
            <p style="color: green;">{{i}}</p>

        elif a != b and i not in red:
            <p style="color: red;">{{i}}</p>

I recive this error in my template:我在模板中收到此错误:

'for' statements should use the format 'for x in y': for i, (a, b) in enumerate(zip(seq1, seq2)): “for”语句应使用“for x in y”格式:for i, (a, b) in enumerate(zip(seq1, seq2)):

Django doesn't allow arbitrary code in for loop templates ; Django不允许在 for 循环模板中使用任意代码 you can't even loop over a simple range defined in the template .您甚至无法遍历模板中定义的简单range It's basically telling you you're only allowed to do simple for loops, reading one item per loop from a simple input iterable.它基本上是在告诉您只允许执行简单的 for 循环,从一个简单的输入迭代中每个循环读取一个项目。

The solution is to make your "thing to iterate over" in the code that renders the template, and pass it in as part of the context, then iterate that.解决方案是在呈现模板的代码中制作“要迭代的东西”,并将其作为上下文的一部分传入,然后对其进行迭代。

I think the Jinja template engine has problems parsing the i, (a, b) part in the for loop here, so perhaps it is worth submitting a ticket for this.我认为Jinja模板引擎在解析for循环中的i, (a, b)部分时有问题,所以也许值得为此提交一张票。 Perhaps it is intended behavior.也许这是预期的行为。

Anyway, you can zip with a 3-tuple here.无论如何,您可以在此处使用 3 元组进行压缩。 As first iterable to zip, we can take itertools.count [python-doc] .作为第一个可迭代压缩的文件,我们可以使用itertools.count [python-doc] You thus pass a reference 'count' with the itertools.count() to the context, and then you render with:因此,您将带有itertools.count()的引用'count'传递给上下文,然后使用以下命令进行渲染:

{% for i, a, b in zip(indices(), seq1, seq2) %}
     {# ... #}
{% endfor %}

For example:例如:

>>> from jinja2 import Template
>>> from itertools import count
>>> Template('{% for i, a, b in zip(indices(), seq1, seq2) %} {{ (i, a, b) }}{% endfor %}').render(indices=count, seq1='foobar', seq2='babbaa', zip=zip)
" (0, 'f', 'b') (1, 'o', 'a') (2, 'o', 'b') (3, 'b', 'b') (4, 'a', 'a') (5, 'r', 'a')"

That being said, I strongly advise not to write business logic in the templates.话虽如此,我强烈建议不要在模板中编写业务逻辑。 In fact this is the main reason why Django template engines do not allow such syntax in the first place.事实上,这是 Django 模板引擎一开始就不允许这种语法的主要原因。 It is probably much better to create the zip object in the view, and pass it through the context to the render engine.在视图中创建zip对象并通过上下文将其传递给渲染引擎可能会好得多。

This code may help, try the code looks linke below, it works fine for me这段代码可能会有所帮助,试试下面的代码链接,它对我来说很好用

(modified from some working code I've used) (从我使用过的一些工作代码修改而来)

when using 'for loop' in Jinja2, use loop.xxx to access some special vars在 Jinja2 中使用 'for loop' 时,使用 loop.xxx 来访问一些特殊的变量

such as:
  loop.index        # index (1 inexed)
  loop.index0       # index (0 inexed)
  loop.revindex     # reversed ...
  loop.revindex0    # reversed ...
  loop.first        # True if first iteration
  loop.last         # True if last iteration
  loop.length
  loop.depth        # Recursive loop depth
  loop.depth0       # Recursive loop depth (0 inexed)

Code:代码:

{% for (item_a, item_b) in zip(seq1, seq2) %}
{# important note: you may need to export __builtin__.zip to Jinja2 template engine first! I'm using htmlPy for my app GUI, I'm not sure it will or not affect the Jinja2 Enviroment, so you may need this #}
  <tr>
    <td>No.{{ loop.index0 }}</td>{# index (0 inexed) #}
    <td>No.{{ loop.index }}</td>{# index (1 started) #}
    <td>{{item_a}}</td>
    <td>{{item_b}}</td>
  </tr>
{% endfor %}

My Environment:我的环境:

python 2.7.11 (I have py35 py36 but the code wasn't tested with them)

>py -2
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] on win32
>pip2 show jinja2
Name: Jinja2
Version: 2.8

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

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