打印烧瓶中数组的数组值

时间:2018-08-20 13:53:24

标签: python flask

我有这个数组数组:

[['Frutta', 
['M01', '2018-08-06 08:35:00', '2018-08-06 10:13:00'], ['M02', 
'2018-08-06 10:18:00', '2018-08-06 11:42:00'], ['M04', '2018-08-06 15:19:00', 
'2018-08-06 16:37:00']], 

['verdura', 
['M01', '2018-08-06 08:35:00', '2018-08-06 10:25:00']]]

我想打印每个字段

所以,我用烧瓶试了像这样:

{% for prodotto in range(inserisci_ordine[1]| length) %}
{{ inserisci_ordine[prodotto][0] }}<br>
{% for macchine in range(inserisci_ordine[prodotto]| length) %}
{{ inserisci_ordine[prodotto][macchine] }}<br>
{{ inserisci_ordine[prodotto][macchine][2] }}<br>
{% endfor %}
{% endfor %}

我想打印这样的东西:

frutta

  • M01, 2018-08-06 08:35:00, 2018-08-06 10:13:00
  • M02, 2018-08-06 10:18:00, 2018-08-06 11:42:00
  • M04, 2018-08-06 15:19:00, 2018-08-06 16:37:00

verdura

  • M01, 2018-08-06 08:35:00, 2018-08-06 10:25:00

但是我不知道。


编辑。

是否可以通过某种方式打印上一周期中使用的机器?例如:

frutta

  • M01, 2018-08-06 08:35:00, 2018-08-06 10:13:00#我要在这里打印, NULL
  • M02, 2018-08-06 10:18:00, 2018-08-06 11:42:00#这里, M01
  • M04, 2018-08-06 15:19:00, 2018-08-06 16:37:00#此处, M02

verdura

  • M01, 2018-08-06 08:35:00, 2018-08-06 10:25:00 #here , NULL

2 个答案:

答案 0 :(得分:1)

我认为,如果为每个组添加额外级别的嵌套,您会更轻松。这样数组的数组实际上就是[[name, list_of_machines], [name, list_of_machines], ...]的列表。如果这样做,则可以使用以下模板创建一个循环,为每个标题创建一个表:

from jinja2 import Environment, BaseLoader

template_string = """
{% for group_index in range(arrays| length) %}
    <h3>{{ arrays[group_index][0] }}</h3>
    <table>
        {% for machine in range(arrays[group_index][1] | length) %}
            <tr>
                <td>{{ arrays[group_index][1][machine][0] }}</td>
                <td>{{ arrays[group_index][1][machine][1] }}</td>
                <td>{{ arrays[group_index][1][machine][2] }}</td>
            </tr>
        {% endfor %}
        </tr>
    </table>
{% endfor %}

"""


data = [
['Frutta',
 [['M01', '2018-08-06 08:35:00', '2018-08-06 10:13:00'],
  ['M02', '2018-08-06 10:18:00', '2018-08-06 11:42:00'],
  ['M04', '2018-08-06 15:19:00', '2018-08-06 16:37:00']]],

['verdura',
[['M01', '2018-08-06 08:35:00', '2018-08-06 10:25:00']]]]

template = Environment(loader=BaseLoader).from_string(template_string)
print(template.render(arrays=data))

答案 1 :(得分:1)

base.html

{% for row in data %}
    {{row[0]}}
    <ul>
        {% for element in row[1:] %}
            <li>{{element|join(', ')}}<li>
        {% endfor %}
    </ul>
{% endfor %}

view.py

@app.route('/')
def sample_view():
    data = [
        [
            'Frutta', 
            ['M01', '2018-08-06 08:35:00', '2018-08-06 10:13:00'], 
            ['M02', '2018-08-06 10:18:00', '2018-08-06 11:42:00'],
            ['M04', '2018-08-06 15:19:00', '2018-08-06 16:37:00']
         ], 
        ['verdura', ['M01', '2018-08-06 08:35:00', '2018-08-06 10:25:00']]
    ]

    return render_template("base.html", data=data)