为什么我的 HTML 按钮不会 运行 我的 Python 脚本?

Why won't my HTML button run my Python script?

因此,我尝试为我的网页创建一个 按钮 。该按钮的功能是运行我的Python脚本来自Raspberry Pi。我试图通过遵循 CgiScript 指南来做到这一点。但是当我点击按钮时,它只显示网页上的脚本,而不是 运行 从 Pi 上的 Thonny IDLE 中获取它。 这是我的 HTML 代码

<!DOCTYPE HTML>
<html>
<head>
<title>Twisted Twister</title>
<link rel="shortcut icon" href="favicon.png">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-Language" content="nl-nl">
<meta http-equiv="Content-Language" content="en-gb">
<meta http-equiv="content-type" content="text/html; charset=WINDOWS-1252" >
<style>
body {
 font-family: Arial, Helvetica, sans-serif;
    font-size: 30px;
    color: #FFB0FE
    background url("texture.png")
    }`

#fixed {
    position: fixed;
    top: 0;
    left: 0;
}

#arrowPad {
    border: 10px #FFFFFF;
}

#USERDATA {
    background: 10px #FFFFFF;

}

#USERDATA.h {
    background: 10px #FFFFFF;

}

#USERDATA.h:hover {
    background: 10px #FFFFFF;

}
</style>
<body>
<>
<div id="USERDATA"></div>
<script type="text/javascript" src="jquery-3.3.1.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        setInterval(function () {
            $('#USERDATA').load('RequestUserData.php')
        }, 200);
    })
</table>
</script>

<table>
<div id="PI"></div>
<script type="text/javascript" src="jquery-3.3.1.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        setInterval(function () {
            $('#PI').load('RequestPiData.php')
        }, 200);
    });

</script>
</table>

<div id="fixed">
  <table id="arrowPad">
        <tr>
        <td id="USERDATA" class="h"><a href="/usr/lib/cgi- 
bin/aantalSpelers2.py" ><img src="https://imgflip.com/s/meme/Blank-Nut- 
Button.jpg" width="150" height="150" id="a2" alt="PythonScript"></img></a> 
</td>
</tr>
</table>
</div>
</body>
</html>

这是我的 Python 脚本 def main():

# 0 changes to 1 when players are joining the game
speler_een = 0
speler_twee = 0
speler_drie = 0
speler_vier = 0

# This function makes an inventory of how many players are involved
class HoeveelSpelers:
    def verkrijg_aantal_spelers(spelers_totaal):
        hoeveelSpelers = int(input('Hoeveel spelers spelen er mee? (1-4) '))

        while (hoeveelSpelers > 4 or hoeveelSpelers < 1):
            print('Dit is niet een geldige hoeveelheid spelers. Probeer het 
opnieuw. ')
            hoeveelSpelers = int(input('Hoeveel spelers spelen er mee? (1-4) 
'))

        return hoeveelSpelers

h = HoeveelSpelers()
spelers_totaal = h.verkrijg_aantal_spelers()

if (spelers_totaal == 4):
    speler_een = 1
    speler_twee = 1
    speler_drie = 1
    speler_vier = 1
elif (spelers_totaal == 3):
    speler_een = 1
    speler_twee = 1
    speler_drie = 1
elif (spelers_totaal == 2):
    speler_een = 1
    speler_twee = 1
elif (spelers_totaal == 1):
    speler_een = 1

print(speler_een, speler_twee, speler_drie, speler_vier)

main()

基本上我想做的就是点击网页上的按钮和 运行 脚本。

从 HTML 调用 Python 代码并不像创建一个 hyperlink 到文件路径那么容易(但一旦你了解了基础知识也不会很复杂)。

为了让您的 Python 代码可以从您的 HTML 页面访问,您需要 运行 一个合适的 Python 网络服务器在后面(例如 Flask).

坚持 Flask 示例 - 如果您想要一个按钮,触发某个功能(例如 foo()),您需要注册一个映射 [=40= 的 "endpoint" ] 到该函数(示例取自 here):

后端

from flask import Flask
app = Flask(__name__)

@app.route("/")
def foo():
    pass # define the action here

...

假设您运行将 Flask 服务器连接到与您的前端相同的主机上的端口 5000,按钮将需要 link 类似

前端

...
<a href="http://localhost:5000/">click here</a>
...

没有 return 值,实际处理仅发生在后端 - 您的前端不会注意到它。当谈到 returning 值时(我假设你的例子就是这种情况),事情变得稍微更具挑战性。我只能推荐Flask Mega Tutorial (by Miguel Grinberg)。它涵盖了很多基础知识以及相当高级的功能 - 这可能会帮助您设计我们的应用程序。

希望这对您有所帮助...