-3

My Flask would called an exe program. And when it runs, it would concatenate the output and return a string to html like this:

File1.dll< br >Encryption Passed< br >Validation Passed< br >File2.dll< br >Encryption Passed< br >Validation Passed< br >File3.dll< br >Encryption Passed< br >Validation Passed< br >

Then I loop it and display it in list from by splitting the < br >. My html looks like this:

<ul>
{% for out in outputLog.split("<br>")%}
  <li>{{ out }} </li>
{% endfor %}
</ul>

However, I want my output to display in this way in a table form where it will check the output of the message and determine which column it should belongs to.

My table headings are:

File Name | Encryption Status | Validation Status

I want to do something like this:

if out == "Encryption Passed":
   print at "Encryption Status" column
elif out == "Validation Passed":
   print at "Validation Status" column
else:
   print at "File Name" column

Does it possible and how to do that? Or is there any better solution to this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
kerrysugu
  • 123
  • 1
  • 1
  • 9

2 Answers2

1

You should parse the data before it gets to Jinja, but to do it purely in Jinja:

<table>
    <tr>
        {% for out in outputLog.split("<br>") %}
            {% if loop.index % 3 == 0 %}
    </tr>
    <tr>
            {% endif %}
        <td>{{ out }} </td>
        {% endfor %}
    </tr>
</table>
abigperson
  • 5,252
  • 3
  • 22
  • 25
0

You probably want to define a function that parses your data, and then use that in your template.

In [4]: def split_to_rows(result):
   ...:     items = result.split('< br >')
   ...:     row = []
   ...:     for item in items:
   ...:         if not (item.startswith('Encryption') or item.startswith('Validation')):
   ...:             if len(row) > 0: yield row
   ...:             row = []
   ...:         row.append(item)
   ...:     if len(row) > 0: yield row # make sure to yield final row
   ...:     

In [5]: s = 'File1.dll< br >Encryption Passed< br >Validation Passed< br >File2.dll< br >Encryption Passed< br >Validation Passed< br >File3.dll< br >Encryption Passed< br >Valid
   ...: ation Passed< br >'

In [6]: list(split_to_rows(s)) # lets see what the output looks like
Out[6]: 
[['File1.dll', 'Encryption Passed', 'Validation Passed'],
 ['File2.dll', 'Encryption Passed', 'Validation Passed'],
 ['File3.dll', 'Encryption Passed', 'Validation Passed'],
 ['']]

Now the template will look something like:

<table>
<thead> ... </thead>
<tbody>
{% for row in split_to_rows(outputLog) %}
<tr>
    {% for out in row %}
        <td>{{ out }} </td>
    {% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
Peter Gibson
  • 19,086
  • 7
  • 60
  • 64