-2

I have written a python script that reads a csv file and displays it on a web page. The web page is a simple html.

I am trying to display it with a certain format. I have searched through stackoverflow and found this: Formatting output of CSV file in Python which was helpful.

However, it works fine when using the print function but when I try to render into my webpage the format is not the same and it also displays only one line of result instead of the whole file.

So how can I have it display on my webpage the same format it displays on the python shell?

Here's what I have so far:

@app.route('/results/view')
def my_results():

        with open(str(get_file(filename)), "r") as f:
            content = csv.reader(f)
            for row in content:
                content =(('{:^15}  {:^15}  {:^20} {:^25}'.format(*row)))

            return render_template("results.html",content=content)


if __name__ == '__main__':
    app.debug = True #Uncomment to enable debugging
    app.run() #Run the Server

results.html

<!DOCTYPE html>
<html>
<head>
<title>My webpage</title>
</head>
<body>
  <a>{{ content }}</a>
</body>
</html>

results w/ print statement instead of 'content=' (what I want)

  A       B
 blue     Car
 yellow   Bike
 green    Boat

result I am getting with content =

A green B Boat
ana
  • 21
  • 9

2 Answers2

0

With:

    content = csv.reader(f)
    for row in content:
        content =(('{:^15}  {:^15}  {:^20} {:^25}'.format(*row)))

You are:

  • assigning the content of the csv to content
  • iterating through content
  • assigning a formatted string to content (with print you don't have this step)

So content is overwritten at each iteration.

You probably want something like:

    content = csv.reader(f)
    html_content=""
    for row in content:
        html_content += (('{:^15}  {:^15}  {:^20} {:^25}'.format(*row)))
        html_content += "<br>" #or p/div/…

And then return render_template("results.html",content=html_content).

Or

    content = csv.reader(f)
    html_content=[]
    for row in content:
        html_content.append(('{:^15}  {:^15}  {:^20} {:^25}'.format(*row)))

And then do a for loop in your view. Using jinja, something like:

{% for row in content %}
  <p>{{ row|e }}</p>
{% endfor %}
fredtantini
  • 15,966
  • 8
  • 49
  • 55
  • Thank you for the clarification. Although, your suggestion works as far as display all the results, it still doesn't format correctly. It is just a long line with no breaks – ana Aug 03 '17 at 11:39
  • Did you add `html_content += "
    "`? maybe they are escaped?
    – fredtantini Aug 03 '17 at 11:41
  • I did. The '
    ' just gets printed in between the results. So A
    blue
    car ...
    – ana Aug 03 '17 at 11:43
  • Your template engine is escaping html tags (which is a good thing actually). Using the second solution (passing a list to the template and iterating over it in the template) will avoid this problem. Also, in HTML, a sequence of N consecutive whitespaces is treated as one single space, so your line formatting won't work either. If you want to display tabular data, use the proper html tags (table / th / tr / td) instead. – bruno desthuilliers Aug 03 '17 at 12:03
  • I am using flask but it sort of worked. Now it prints one letter at every line. I'll update my answer once get it right. – ana Aug 03 '17 at 12:04
  • either use `.append` or `+= [ ... ]` – fredtantini Aug 03 '17 at 12:08
0

After a bit of research I found this Python dictionary in to html table which is was needed.

Fredtantini's answer was close. I needed an additional nested for loop in my html code.

<table>
{% for row in content %}
    <tr>
    {% for i in row %}
        <td>{{ i}}</td>
    {% endfor %}
    </tr>
{% endfor %}
 </table>

This worked for me

ana
  • 21
  • 9