0

Working on my code I found out a problem with one of the features for my project: Code preview. I open a file with code inside it and reading all the lines I have an array with the lines.

This is the code: Main.py

with open(path.join(files_folder, prj + '.' + file_ext)) as file:
            lines = file.readlines()

Then I use the lines variable in HTML: index.html

<ol>
  {% for line in lines %}
    <li>{{ line }}</li>
  {% endfor %}
</ol>

Code inside the file:

while True:
    if 10 > 8:
        print('Hello World!')

The problem is, using print(lines) in Main.py file, it prints out the lines and the blank spaces but when i print it on HTML there aren't blank spaces.

P.S. I've tried also to print just line but it is equal as the lines print

Jeff B
  • 8,572
  • 17
  • 61
  • 140

1 Answers1

0

By default, (repeated) spaces are ignored in HTML documents. If you want to show whitespace exactly as it is in the HTML source, use a <pre> tag or the white-space: pre; CSS style.

<ol>
  {% for line in lines %}
    <li style="white-space: pre">{{ line }}</li>
  {% endfor %}
</ol>

Here's an example: https://jsfiddle.net/6dwzhs8b/

Wander Nauta
  • 18,832
  • 1
  • 45
  • 62