HTML中如何显示output table 根据output判断属于Flask的哪一列

How to display output in HTML table based on output to determine which column it belongs to Flask

我的 Flask 会调用一个 exe 程序。当它运行时,它会将输出和 return 字符串连接到 html,如下所示:

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 >

然后我循环它并通过拆分 < br > 在列表中显示它。我的 html 看起来像这样:

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

但是,我希望我的输出以这种方式显示在 table 表单中,它将检查消息的输出并确定它应该属于哪一列。

我的 table 标题是:

File Name | Encryption Status | Validation Status

我想做这样的事情:

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

这可能吗?怎么做?或者对此有更好的解决方案吗?

您可能想要定义一个函数来解析您的数据,然后在您的模板中使用它。

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

现在模板将如下所示:

<table>
<thead> ... </thead>
<tbody>
{% for row in split_to_rows(outputLog) %}
<tr>
    {% for out in row %}
        <td>{{ out }} </td>
    {% endfor %}
</tr>
{% endfor %}
</tbody>
</table>

您应该在数据到达 Jinja 之前对其进行解析,但要完全在 Jinja 中进行解析:

<table>
    <tr>
        {% for out in outputLog.split("<br>") %}
            {% if loop.index % 3 == 0 %}
    </tr>
    <tr>
            {% endif %}
        <td>{{ out }} </td>
        {% endfor %}
    </tr>
</table>