abhi_47
abhi_47

Reputation: 25

html table ignoring the white space characters in python string when rendred with flask jinja template

I have a python string like this:

my_string = 'Variance           -2592.41'

I want to display this string as it is on html page rendered using flask render_template function. But the same string appears like 'Variance-2592.41' on the UI (all the white spaces in between are ignored automatically). I am not able to understand why this is happening. Please help!

Upvotes: 1

Views: 694

Answers (2)

Suraj Raut
Suraj Raut

Reputation: 581

use Flask.Markup for escaping tags from string.

from markupsafe import Markup

edit render_tempale methode.

return render_template("index.html",message = Markup('Variance &nbsp &nbsp &nbsp &nbsp -2592.41'));

this will solve the problem. Tested.

Upvotes: 1

Jürgen Gmach
Jürgen Gmach

Reputation: 6121

You have to either wrap it in pre tags, or you need to use the   element, one for each desired empty space.

Background info: in HTML all spaces get folded to a single one.

Also, you could split your string and put the data into separate <td> tags.

Upvotes: 2

Related Questions