Aladine Aladine - 18 days ago 8
HTML Question

Python/Flask/HTML <plaintext> Output is adding spaces

So i am using python flask to output the result of a script to plain text on a webpage. When I print the function from CLI the output looks like this:

1
2
3
4
5


However when it is on the website it looks like this:

1

2

3

4

5


The output HTML file looks like this:

<plaintext>
{% for rows in report %}
{{rows}}
{% endfor %}


Im really not great at HTML, but is there a way to remove the new lines that are added?

Answer
  1. <plaintext> is obsolete. Use <pre> instead. See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/plaintext

  2. The rows variable you used may contains line breaks already. And you don't need to use for loop if you just want to output every line in report. Try this:

    <pre>{{ ''.join(report) }}</pre>
    
  3. Considering the control structure. You can use Jinja2's whitespace control feature to avoid extra spaces (and line breaks):

    <pre>
    {% for row in report -%}
        {{ row }}
    {%- endfor %}
    </pre>
    
Comments