cannot display a line by line list on flask webpage

Issue

this is the function i used to return the list line by line :

def listing(table):
    x=0
    tab=[]
    for item in range(0,len(table)):
        x+=1
        result= f'{x}- {table[item]}' 
        tab.append(result)
    final = '\n'.join(tab)
    #print(final)
    return final

and i made sure that it is printing line by line in the terminal

this is the app.route :

if request.method=='POST':
    word = request.form['kword']
    result=getListOfKeyWord(word)
    table=listing(result)
    return render_template('advance_search.html',key=table)
return render_template('advance_search.html')

and this is the placeholder i’m trying to display the result in it :

<div class="row center">
    {{key}}
  </div>

the result is displayed in the terminal line by line like desired, but on the webpage it is all concatinated side by side

can anyone help please ?

Solution

Rather joining the list with \n, join it with <br> , <br> tag means break line.

final = '<br>'.join(tab)

Use safe filter to render it as html rather then plain text.

{{Key|safe}}

Answered By – charchit

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published