1

this the first function that returns the list:

def getListOfKeyWord(keyword):
df=pd.read_excel('finaltrue.xlsx')
corpus=[]
for i in range(len(df)):
    if keyword in df["text"][i]:
        corpus.append(df["text"][i])
return corpus

this the function where i printing the list as line by line:

def listing(result):
    x=0
    for item in range(0,len(result)):
        x+=1
        table = print(x,"",result[item])
    return table

this is the placeholder in html :

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

and here where i call the functions at the app.routing

 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')

now i get a "none" word at the placeholder position can anyone help please ?

  • 1
    `print()` returns `None`, not the printed data. – Klaus D. Jul 04 '21 at 04:11
  • `for item in range(0,len(result)): x+=1 table = print(x,"",result[item]) return table` is effectively reseting value of table evey time – Epsi95 Jul 04 '21 at 04:14
  • 1
    You have indentation errors. – no ai please Jul 04 '21 at 04:17
  • Also what type is the of lthe returned value by function `listing`? Is it a `list` or a string? – AmaanK Jul 04 '21 at 04:24
  • then how can i return the printed data ? @KlausD. – omar elhabbak Jul 04 '21 at 04:28
  • You don't, you assemble the string with string formatting (f-strings, `.format()` or old `%` style) – Klaus D. Jul 04 '21 at 04:29
  • @Epsi95 then how to fix it in order to return the value that contains the line by line list? – omar elhabbak Jul 04 '21 at 04:33
  • @KlausD. can u please show me the syntax, how to write it ? in order to fix it – omar elhabbak Jul 04 '21 at 04:34
  • Well, I could, but I think it would on the long run (and maybe even a shorter) that you educate yourself your string formatting. It is an important concept in Python. There are countless resources on the internet. One example: https://docs.python.org/3/tutorial/inputoutput.html – Klaus D. Jul 04 '21 at 04:36
  • @KlausD. i really searched but i don't have that much time cause of the deadline of my graduation project, so if u can help me to get it done faster, i will be thankful – omar elhabbak Jul 04 '21 at 04:40
  • Does this answer your question? [cannot display a line by line list on flask webpage](https://stackoverflow.com/questions/68242143/cannot-display-a-line-by-line-list-on-flask-webpage) – nngeek Jul 06 '21 at 14:28

1 Answers1

0

First of all, you cannot assign the output of print() function to a variable.

The key issue here is your listing function. You can use this code instead:

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

You need to use <br> instead of \n to join the list since HTML will show <br> as new line, not \n.

nngeek
  • 1,912
  • 16
  • 24