0

Here is my sample flask code,

from flask import Flask
from flask import render_template
app = Flask(__name__)
@app.route('/')
def root():
    report = ["str1       1\n","     str2 2"]
    report = "".join(report)
    report = report.replace('\n', '<br>')
    return render_template('detailed_report.html', report = report)
app.run(debug=True)

Here is my corresponding HTML code,

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Index</title>
</head>
<body>
<div>
    {% autoescape false %}
        {{report}}
    {% endautoescape %}
</div>
</body>
</html>

Expected result:

str1       1
     str2 2

Actual result:

str1 1
str2 2

What should I do to preserve the space? Thanks in advance :)

1 Answers1

2

A little bit of css is required in order to make spacing work!

.barcode{
    white-space:pre;
}
<div class="barcode">{% autoescape false %}{{report}}{% endautoescape %}</div>

Here is an example

.barcode {
  white-space: pre;
  /* or pre-wrap if you want wrapping to still work.*/
}
<div class="barcode">HELlo world eg a    

nd i am a            good go </div>
Fareed Khan
  • 2,613
  • 1
  • 11
  • 19