[英]Why won't my HTML button run my Python script?
因此,我嘗試為我的網頁創建一個按鈕 。 該按鈕的功能是從Raspberry Pi運行我的Python腳本。 我試圖通過遵循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代碼並不像創建指向文件路徑的超鏈接那樣容易(但一旦了解了基礎知識,也就不會很復雜)。
為了使您的HTML代碼可從HTML頁面訪問,您需要在后面運行適當的Python Web服務器(例如Flask )。
堅持Flask示例-如果您想要一個按鈕來觸發某個功能(例如foo()
),則需要注冊一個將URL映射到該功能的“端點”(示例取自此處 ):
from flask import Flask
app = Flask(__name__)
@app.route("/")
def foo():
pass # define the action here
...
假設您正在與前端在同一主機上的端口5000上運行Flask服務器,則該按鈕需要鏈接到類似
...
<a href="http://localhost:5000/">click here</a>
...
沒有返回值,實際的處理僅發生在后端-前端沒有注意到它。 當涉及到返回值時(我假設您的示例就是這種情況),事情變得更具挑戰性。 我只能推薦Flask Mega Tutorial(由Miguel Grinberg撰寫) 。 它涵蓋了許多基礎知識以及相當高級的功能-可能有助於您布局我們的應用程序。
希望這可以幫助...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.