Search code examples
pythonhtmlflaskoutputtabular

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


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?


Solution

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