单击按钮后运行python脚本

单击按钮后运行python脚本,python,html,django,flask,web-applications,Python,Html,Django,Flask,Web Applications,我有一个html页面,如下所示: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Find the words</title> <h1>Find the Words</h1> </head> <body> <p>Find the largest and

我有一个html页面,如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Find the words</title>
    <h1>Find the Words</h1>
</head>
<body>
<p>Find the largest and the smallest word</p>
<textarea rows="25" cols="100"></textarea>
<br>
<button type="button">Find now!</button>
<br><br>
<label>Largest Word</label>
<input type='largest word'/>

<label>Smallest Word</label>
<input type='smallest word'/>

</body>
</html>
#!/usr/bin/env python3

def find_small_large_words(given_string):
    new_list = given_string.split(" ")
    sorted_list = sorted(new_list, key=len)
    return 'Smallest: {0} & Largest: {1}'.format(sorted_list[0], sorted_list[-1])


words = "Ten years on from the financial crisis, it’s hard not to have a sense of déjà vu.Financial scandal and wrangles over financial rule-making still dominate " \
         "the headlines.The cyberhacking at Equifax compromisedpersonal records for half of the adult population of the United States.At SoFi, a one-time fintech darling"

print(find_small_large_words(words))
最后,我希望在单击FindWords按钮时运行python脚本,并将它们放回两个框中。我在这里怎么做?我已经阅读了一些关于使用django框架的建议。还有其他简单的方法吗?如果是,怎么做

编辑:我正在烧瓶里试这个。到目前为止,我的代码是:

#!/usr/bin/env python3

from flask import *
from flask import render_template

app = Flask(__name__)

@app.route('/', methods = ['GET', 'POST'])
def homepage():

    import words
    return render_template("index.html")

if __name__ == "__main__":
    app.run()

但是,我不确定如何专门在textarea输入上执行python脚本并将输出发回。

首先,您需要一个表单来发送它的内容

$document.readyfunction{ /// }; $submit.onclick函数{ $.post url_用于'main.calculate',函数数据{ $minimate_word.valdata.minimate; $longest_word.valdata.longest; }; };
从您在这里的目标来看,我建议您编写一个javascript/jquery脚本,而不是python,因为python需要服务器进行不必要的处理,因为您只希望将其发布回html,而不将其保存在数据库中或进行任何I/O

但是,如果您真的想在python中运行它,您可能需要像您提到的flask或django这样的框架。如果这样做,请在执行python脚本之前,尝试将用户输入保存到字符串变量中。例如:

the_string_var = request(your_form_data)

但同样,您可能需要先创建表单或使用AJAX/jQuery,以便将数据发送到服务器。

您是否用按钮尝试过onclick事件?如果您有web服务器,您可以通过谷歌搜索cgi。除此之外,请阅读您最喜欢的web框架教程。@Tomm我认为onclick事件只执行用php或js编写的脚本。我会看一看。@AnttiHaapala使用Flask可以完成这个任务吗?使用Flash应该可以,我建议使用Flask而不是jquery。我刚刚开始学习Flask。到目前为止,我的flask应用程序代码是:/usr/bin/env python3 from flask import*from flask import render_template app=flask__name__@app.route'/',methods=['GET','POST']def主页:import words返回render_templateindex.html如果_; name___=====\u__main\uuu:app.run,我不知道如何在textarea中给出的输入上运行python函数。您应该将此代码粘贴到您的问题中,这样会更容易阅读,也更适合它,我将为您更新我的答案,但我只给您一个代码片段,以便您可以尝试学习!