0

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 ?

  • Does this answer your question? [Line break in HTML with '\n'](https://stackoverflow.com/questions/39325414/line-break-in-html-with-n) – IoaTzimas Jul 04 '21 at 06:26

2 Answers2

1

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}}
charchit
  • 1,492
  • 2
  • 6
  • 17
0

As @charchit pointed out, you should use <br> (HTML line break) to join your list so that new line can be displayed in your HTML page.

Aside from that, it seems you would like to loop through a list to get both index and value. You actually can do it with enumerate() function. By combining with Python list comprehension, your listing() function can be rewritten as below:

def listing(table):
    final = '<br>'.join([f'{i+1}- {item}' for i, item in enumerate(table)])
    print(final)
    return final
nngeek
  • 1,912
  • 16
  • 24