html網(wǎng)頁調(diào)用后端python代碼的方法實例
當我們利用html代碼制作網(wǎng)頁時,可以用以下方法進行python代碼的調(diào)用:
1.簡單的python代碼例如輸出‘hello world’時,可以選擇直接在網(wǎng)頁寫入python代碼的方式調(diào)用,這時候我們就需要了解Pyscript了。以下是在網(wǎng)頁里直接運行簡易python語段的代碼:
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<pyscript> print('Hello world') </pyscript>
</body>
</html>2.當python代碼稍微比較復雜,且處于網(wǎng)頁構建初期時,我們可以考慮用flask框架對網(wǎng)頁的按鈕進行整體布局。
方法 1)
當網(wǎng)頁代碼較為簡單時,可以直接用html代碼代替render_template:
test1.py
def run():
print('hello world')
run()test.py(含flask包)
from flask import(
Flask, render_template, request, redirect, globals
)
import test1
app= Flask(__name__)
@app.route("/",methods=['GET', 'POST'])
def index():
return '<form action = "http://localhost:5000/b" method = "post"></form><a href="/test" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><button onclick="">進入測試</button></a><a href="/test1" rel="external nofollow" >
@app.route("/test",methods=['GET', 'POST'])
def test():
test1.run()
return '<form action = "http://localhost:5000/b" method = "post"></form><a href="/test" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><button onclick="">進入測試</button></a>
if __name__ == '__main__':
app.run(debug=True)運行test1.py,ctrl+單擊點開下圖終端中出來的網(wǎng)址:


點擊按鈕運行即可出現(xiàn)hello word字樣。

方法 2)
當網(wǎng)頁代碼較為復雜且長時,可以使用render_template來進行前后端交互。此時我們需要在包含flask的python代碼同文件夾下新建一個template文件夾:

test.py代碼同上,
b.html
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form action = "http://localhost:5000/" method = "post">
</form>
<a href="/test" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><button onclick="">進入測試</button></a>
</body>
</html>test1.py
from flask import(
Flask, render_template, request, redirect, globals
)
import test1
app= Flask(__name__)
@app.route("/",methods=['GET', 'POST'])
def index():
return render_template(
"b.html"
)
@app.route("/test",methods=['GET', 'POST'])
def test():
test1.run()
return render_template(
"b.html"
)
if __name__ == '__main__':
app.run(debug=True)測試的方式同方法1),這里不多贅述。
3.網(wǎng)頁設計初期,以上兩種方法足以,但是博主在設計網(wǎng)頁時是設計到一半才發(fā)現(xiàn),在前期寫純Html文件后再使用flask框架設計按鈕響應python腳本,會出現(xiàn)網(wǎng)頁不穩(wěn)定的情況,博主的圖啊網(wǎng)頁跳轉(zhuǎn)都不見了。經(jīng)過研究之后,博主又發(fā)現(xiàn)了一個不管在網(wǎng)頁設計前期中期都可以使用的python腳本調(diào)用方法!那就是ActiveX控件。
這個控件只有IE瀏覽器中有(至少博主在熟悉的其他瀏覽器中沒有找到這個控件),在我們想要使用它之前需要檢查我們的IE瀏覽器是否已經(jīng)啟用ActiveX控件。手動打開IE的ActiceX控件需要如下幾步:打開設置-Internet選項-安全-自定義級別-把和ActiveX有關的選項全部點啟用或者提示。

然后我們運行一下代碼進行測試。
a.html
<!DOCTYPE html>
<html>
<head>
<meta charset="gb2312">
<title>ceshi</title>
<script language="javascript">
function exec1 (command) {
var ws = new ActiveXObject("WScript.Shell");
ws.exec(command);
}
</script>
</head>
<body>
<button class='button1' onclick="exec1('python D:/xgcs/test1.py')">執(zhí)行程序</button></p>
</body>
</html>利用IE瀏覽器打開網(wǎng)址,點擊按鈕運行即可。

運行前會出現(xiàn)彈窗如下所示,點是和允許即可。


由于是輸出,所以黑框一閃而逝很正常,要想看到print出來的hello world字樣,得再加個輸入input()?;蛘吣愕膒ython運行出來是個ui窗口,那也會停留很久,別把黑框點叉叉即可。
總結
到此這篇關于html網(wǎng)頁調(diào)用后端python代碼的文章就介紹到這了,更多相關html網(wǎng)頁調(diào)用后端python內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
PyTorch加載數(shù)據(jù)集梯度下降優(yōu)化
這篇文章主要介紹了PyTorch加載數(shù)據(jù)集梯度下降優(yōu)化,使用DataLoader方法,并繼承DataSet抽象類,可實現(xiàn)對數(shù)據(jù)集進行mini_batch梯度下降優(yōu)化,需要的小伙伴可以參考一下2022-03-03
Python中反轉(zhuǎn)二維數(shù)組的行和列問題
這篇文章主要介紹了Python中反轉(zhuǎn)二維數(shù)組的行和列問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
python爬蟲之你好,李煥英電影票房數(shù)據(jù)分析
這篇文章主要介紹了python爬蟲之你好,李煥英電影票房數(shù)據(jù)分析,文中有非常詳細的代碼示例,對正在學習python爬蟲的小伙伴們有一定的幫助,需要的朋友可以參考下2021-04-04
使用Python的Twisted框架編寫非阻塞程序的代碼示例
Twisted是基于異步模式的開發(fā)框架,因而利用Twisted進行非阻塞編程自然也是必會的用法,下面我們就來一起看一下使用Python的Twisted框架編寫非阻塞程序的代碼示例:2016-05-05

