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

2 Answers 2

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

6 Comments

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
Did you add html_content += "<br>"? maybe they are escaped?
I did. The '<br>' just gets printed in between the results. So A<br> blue<br>car ...
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.
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.
|
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

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.