html網(wǎng)頁調(diào)用后端python代碼的方法實(shí)例
當(dāng)我們利用html代碼制作網(wǎng)頁時(shí),可以用以下方法進(jìn)行python代碼的調(diào)用:
1.簡(jiǎn)單的python代碼例如輸出‘hello world’時(shí),可以選擇直接在網(wǎng)頁寫入python代碼的方式調(diào)用,這時(shí)候我們就需要了解Pyscript了。以下是在網(wǎng)頁里直接運(yùn)行簡(jiǎn)易python語段的代碼:
<html> <head> <meta charset="utf-8"> </head> <body> <pyscript> print('Hello world') </pyscript> </body> </html>
2.當(dāng)python代碼稍微比較復(fù)雜,且處于網(wǎng)頁構(gòu)建初期時(shí),我們可以考慮用flask框架對(duì)網(wǎng)頁的按鈕進(jìn)行整體布局。
方法 1)
當(dāng)網(wǎng)頁代碼較為簡(jiǎn)單時(shí),可以直接用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="">進(jìn)入測(cè)試</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="">進(jìn)入測(cè)試</button></a> if __name__ == '__main__': app.run(debug=True)
運(yùn)行test1.py,ctrl+單擊點(diǎn)開下圖終端中出來的網(wǎng)址:
點(diǎn)擊按鈕運(yùn)行即可出現(xiàn)hello word字樣。
方法 2)
當(dāng)網(wǎng)頁代碼較為復(fù)雜且長(zhǎng)時(shí),可以使用render_template來進(jìn)行前后端交互。此時(shí)我們需要在包含flask的python代碼同文件夾下新建一個(gè)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="">進(jìn)入測(cè)試</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)
測(cè)試的方式同方法1),這里不多贅述。
3.網(wǎng)頁設(shè)計(jì)初期,以上兩種方法足以,但是博主在設(shè)計(jì)網(wǎng)頁時(shí)是設(shè)計(jì)到一半才發(fā)現(xiàn),在前期寫純Html文件后再使用flask框架設(shè)計(jì)按鈕響應(yīng)python腳本,會(huì)出現(xiàn)網(wǎng)頁不穩(wěn)定的情況,博主的圖啊網(wǎng)頁跳轉(zhuǎn)都不見了。經(jīng)過研究之后,博主又發(fā)現(xiàn)了一個(gè)不管在網(wǎng)頁設(shè)計(jì)前期中期都可以使用的python腳本調(diào)用方法!那就是ActiveX控件。
這個(gè)控件只有IE瀏覽器中有(至少博主在熟悉的其他瀏覽器中沒有找到這個(gè)控件),在我們想要使用它之前需要檢查我們的IE瀏覽器是否已經(jīng)啟用ActiveX控件。手動(dòng)打開IE的ActiceX控件需要如下幾步:打開設(shè)置-Internet選項(xiàng)-安全-自定義級(jí)別-把和ActiveX有關(guān)的選項(xiàng)全部點(diǎn)啟用或者提示。
然后我們運(yùn)行一下代碼進(jìn)行測(cè)試。
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)址,點(diǎn)擊按鈕運(yùn)行即可。
運(yùn)行前會(huì)出現(xiàn)彈窗如下所示,點(diǎn)是和允許即可。
由于是輸出,所以黑框一閃而逝很正常,要想看到print出來的hello world字樣,得再加個(gè)輸入input()?;蛘吣愕膒ython運(yùn)行出來是個(gè)ui窗口,那也會(huì)停留很久,別把黑框點(diǎn)叉叉即可。
總結(jié)
到此這篇關(guān)于html網(wǎng)頁調(diào)用后端python代碼的文章就介紹到這了,更多相關(guān)html網(wǎng)頁調(diào)用后端python內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PyTorch加載數(shù)據(jù)集梯度下降優(yōu)化
這篇文章主要介紹了PyTorch加載數(shù)據(jù)集梯度下降優(yōu)化,使用DataLoader方法,并繼承DataSet抽象類,可實(shí)現(xiàn)對(duì)數(shù)據(jù)集進(jìn)行mini_batch梯度下降優(yōu)化,需要的小伙伴可以參考一下2022-03-03python標(biāo)準(zhǔn)日志模塊logging的使用方法
python的標(biāo)準(zhǔn)庫里的日志系統(tǒng)從Python2.3開始支持。只要import logging這個(gè)模塊即可使用。2013-11-11Python中反轉(zhuǎn)二維數(shù)組的行和列問題
這篇文章主要介紹了Python中反轉(zhuǎn)二維數(shù)組的行和列問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01python爬蟲之你好,李煥英電影票房數(shù)據(jù)分析
這篇文章主要介紹了python爬蟲之你好,李煥英電影票房數(shù)據(jù)分析,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python爬蟲的小伙伴們有一定的幫助,需要的朋友可以參考下2021-04-04使用python代碼進(jìn)行身份證號(hào)校驗(yàn)的實(shí)現(xiàn)示例
這篇文章主要介紹了使用python代碼進(jìn)行身份證號(hào)校驗(yàn)的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11使用Python的Twisted框架編寫非阻塞程序的代碼示例
Twisted是基于異步模式的開發(fā)框架,因而利用Twisted進(jìn)行非阻塞編程自然也是必會(huì)的用法,下面我們就來一起看一下使用Python的Twisted框架編寫非阻塞程序的代碼示例:2016-05-05