Python 如何在html页面中输入元素列表?

Python 如何在html页面中输入元素列表?,python,flask,Python,Flask,我必须创建一个web应用程序来测试我的功能。网站应该有一个输入,允许我们输入一个单词列表并提交,然后显示结果列表,按频率和字母顺序排列 我的计算单词出现率的函数: 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:

我必须创建一个web应用程序来测试我的功能。网站应该有一个输入,允许我们输入一个单词列表并提交,然后显示结果列表,按频率和字母顺序排列

我的计算单词出现率的函数:

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

我的功能瓶:

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)
从烧瓶导入烧瓶,请求
从word导入word\u
app=烧瓶(名称)
app.config[“DEBUG”]=True
@app.route('/',methods=[“GET”,“POST”])
def加法器页面()
errors=“”
如果request.method==“POST”:
列表=无
尝试:
List=request.form.getlist[“List”]
除:
错误+=“{!r}不是一个列表。

\n”.format(request.form[“list”]) 如果列表不是无: 结果=单词出现(列表) 返回“”' 结果是{result}

''。格式(结果=结果) 返回“”' {错误} 输入您的单词列表:

''。格式(错误=错误)
问题是我的网页不接受像:L=[“apple”,“juice”,“banana”]这样的元素列表,所以我无法显示函数的输出

谢谢您的帮助。

您可以使用
返回渲染模板('result\u list.html',result=result)


尝试在templates文件夹中创建HTML文件

让我们用以下代码调用它displayList.html

                <html>
                <body>
                    <p>The result is {% for x in result %}
                        <li>{{x}}</li>
                    </p>
                </body>
            </html>
希望这有帮助

这有帮助吗?
return render_template("displayList.html", result=List)