Search code examples
pythonlinuxwebmozillajinja2

How I render a file to an an html line by line using jinja2


I have file contains 100s of lines. I set all initializations of jinja2. I can able to render the file to my browser, but the problem is that it is not formated.(ie lines ar not come one by one).

So I want to render the file in formated way(ie is line by line in browser) using jinja2. What are the editing I need to done in my html file and python code.


Solution

  • add "<br/>" to the end of the line so that a browser would do a line break?

    Is that what you're aksing?

    [edit]

    read lines to an object in python:

    lines = open("myfile.txt")
    for line in lines:
        print line.rstrip() //rstrip removes whitespaces at the end
    lines.close()
    

    a template for jinja2 printing line by line from lines

    <title>{% filename %}</title>
    <body>
    {% for line in lines %}
      {{ line }}<br/>
    {% endfor %}
    </body>