0

I have this array of arrays:

[['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']]]

and I want to print each field

so, with flask I tried like this:

{% 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 %}

I would like to print something like this:

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

But I can't figure it out..


EDIT.

Is it possible in some way, print also the machine utilized in the previous cycle? for example:

frutta

  • M01, 2018-08-06 08:35:00, 2018-08-06 10:13:00 #I want to print here , NULL
  • M02, 2018-08-06 10:18:00, 2018-08-06 11:42:00 #Here the , M01
  • M04, 2018-08-06 15:19:00, 2018-08-06 16:37:00 #here the , M02

verdura

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

2 Answers 2

5

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Your solution is fantastic, just one more question, how can I print in the template the machine utilized in the previous cycle? I'll do an example in the question. thankyou
2

I think you'll have an easier time if you add an extra level of nesting to each group. So that the array of arrays is actually list of [[name, list_of_machines], [name, list_of_machines], ...]. If you do that, you can create a loop that creates a table for each headline using the following template:

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.