0

I have to create a web app to test my function. The website should have an input allowing us to type in a list of word and submit , then display the resulting list with occurrences ordered by frequency and alphabetical order .

My function calculating the word occurrence:

def word_occur(List):
    dict_= dict()
    L_prime = []
    cpt = 0
    for i in range(len(List)):
        word_occ =  List[i]
        if word_occ not in L_prime:
            for word in List:
                if word_occ == word:
                    cpt += 1
            dict_[List[i]] = cpt
            L_prime.append(List[i])
            cpt = 0

    L_sort = sorted(dict_.items(),key = lambda x :(-x[1],x[0]))
    return L_sort

My function flask:

from flask import Flask, request
from word import word_occur

app = Flask(__name__)
app.config["DEBUG"] = True


@app.route('/',methods = ["GET","POST"])
def adder_page():
    errors = ""
    if request.method == "POST":
        List = None
        try:
            List = request.form.getlist["List"]
        except:
            errors += "<p>{!r} is not a list.</p>\n".format(request.form["List"])

        if List is not None:
            result = word_occur(List)
            return '''
                <html>
                    <body>
                        <p>The result is {result}</p>
                    </body>
                </html>
            '''.format(result=result)
    return'''
        <html>
            <body>
                {errors}
                <p>Enter your list of word:</p>
                <form method="post" action=".">
                    <p><input name="List" /></p>
                    <p><input type="submit" value="Calculate the occurence"/></p>
                </form>
            </body>
        </html>
    '''.format(errors=errors)

The problem is my web page doesn't accept a list of element like : L = ["apple","juice","banana"] , so I can't display the output of the function .

Thank you for your help.

RMPR
  • 3,368
  • 4
  • 19
  • 31
Redox2
  • 3
  • 1
  • Is this helping? https://stackoverflow.com/questions/12096522/render-template-with-multiple-variables – xcen Feb 12 '20 at 08:38

2 Answers2

0

You could possibly use as return render_template('result_list.html', result=result)

api

Anvesh
  • 607
  • 1
  • 5
  • 19
0

Try making an HTML File in the templates folder

lets call it displayList.html with the following code

                <html>
                <body>
                    <p>The result is {% for x in result %}
                        <li>{{x}}</li>
                    </p>
                </body>
            </html>

Then in your app.py when you are returning the values instead of returning the HTML string

return render_template("displayList.html", result=List)

Hope this helps

Moiz Ahmed
  • 11
  • 3