Posted 鍊╁€╁皬骞窰u
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。
鈥斺€斺€斺€斿搱鍠斤紝澶у€╁€╁張鏉ュ暒 馃檱鈥嶁檧锔忦煓団€嶁檧锔忦煓団€嶁檧锔?/span>
鈥斺€?鈥斺€旀眰璧忔眰鍏虫敞锛屼箞涔堝搾鏃ュ父鍞犲彣
鍦ㄦ垜鐨勭悊瑙g湅鏉ワ紝Flask 灏辨槸缁撳悎 Python 鍜?HTML 缂栧啓 web 椤甸潰銆?/p>
1锛塒ython 鐢ㄦ潵缂栧啓 URL銆佽繑鍥炵粨鏋滐紱
2锛塇TML 缂栧啓椤甸潰鍙傛暟锛?/p>
3锛夐€氳繃缃戦〉绔闂?WEB 椤甸潰锛?/p>
涓嬮潰灏卞ぇ姒傝璁?Flask 鐨勪娇鐢紝鎴戣寰楁垜灏辨帉鎻′簡鐐圭毊姣涳紝娆㈣繋鍚勪綅琛ュ厖鍒嗕韩
Flask 鍏ラ棬
1銆佷粈涔堟槸 Flask
Flask 妗嗘灦鐨勪富瑕佺壒寰佹槸鏍稿績鏋勬垚姣旇緝绠€鍗曪紝浣嗗叿鏈夊緢寮虹殑鎵╁睍鎬у拰鍏煎鎬э紝绋嬪簭鍛樺彲浠ヤ娇鐢?Python 璇█蹇€熷疄鐜颁竴涓綉绔欐垨 Web 鏈嶅姟銆?/p>
2銆佸畨瑁?Flask
pip install flask
3銆佸疄渚?1
-
鎵撳紑 Pycharm 缂栬緫鍣紝鏂板缓涓€涓?py 鏂囦欢銆傚锛歞emo.py
from flask import Flask
#褰撳墠妯″潡瀹炰緥鍖栦竴涓狥lask瀵硅薄
app=Flask(__name__)
#鎸傝浇鎺ュ彛鍦板潃
@app.route('/add')
#鍐欎釜鍑芥暟
def add():
a=1
b=2
return str(a+b)
if __name__=="__main__": #濡傛灉鏄粠鏈ā鍧楄繍琛岀殑灏辫繍琛屼笅闈㈢殑
app.run()
-
杩愯鏂囦欢锛屽嚭鐜颁笅杩扮殑缁撴灉璇存槑杩愯鎴愬姛 璁块棶:http://127.0.0.1:5000/add锛屽氨鍙互鐪嬪埌杩斿洖缁撴灉浜唦~
杩欓噷闇€瑕佹敞鎰忕殑鍦版柟鏄細return 杩斿洖鐨勬槸 str 鏍煎紡锛屽惁鍒欎細鎶ラ敊锛侊紒
4銆佸疄渚?2
涓婅堪鏄竴涓瘮杈冪畝鍗曠殑 Flask 鐨勫啓娉曪紝涓嬮潰缁欏ぇ瀹惰璁插浣曞埄鐢?Flask 鍙戦€?POST/Get 璇锋眰.
-
鎵撳紑 Pycharm 缂栬緫鍣紝鏂板缓涓€涓?py 鏂囦欢銆傚锛歞emo1.py
from flask import Flask,request,jsonfy
#褰撳墠妯″潡瀹炰緥鍖栦竴涓狥lask瀵硅薄
app=Flask(__name__)
@app.route('/sub1',methods=["post",'get']) #鎺ュ彛绫诲瀷
def reduce():
a = request.json.get('a')
b = request.json.get('b')
final = int(a) - int(b)
print(final)
return jsonify({"code":1,"msg":"鎴愬姛","result":str(final)})
if __name__=="__main__": #濡傛灉鏄粠鏈ā鍧楄繍琛岀殑灏辫繍琛屼笅闈㈢殑
app.run()
濡備笂杩板啓娉曪紝jsonfy 鏄?Flask 涓嬭鍏惰繑鍥炰负 json 鏍煎紡锛?span class="mq-65">姝ゆ椂鎴戜滑鍙?/span>浠ラ€氳繃 PostMan 宸ュ叿杩涜璁块棶~~
5銆佸疄渚?3
鎷撳睍锛?/strong>
姣斿涓婅瘔鍐欑殑鍑忔硶鍙兘浼氬嚭鐜扮殑鎯呭喌锛?/p>
1.a=39,b=12 27
2.a=10 ,b 鍙傛暟缂哄け
3.a='abc',b=3 鍙傛暟蹇呴』涓?int
...........绛夌瓑鎯呭喌銆?/p>
from flask import Flask,request,jsonfy
app=Flask(__name__)
@app.route('/sub2',methods=["post"]) def reduce():
a = request.json.get('a')
b = request.json.get('b')
if a is None or b is None:
return jsonify({"code":0,"msg":"鍙傛暟缂哄け","result":None})
if not isinstance(a,int) or not isinstance(b,int):
return jsonify({"code": 1, "msg": "鍙傛暟蹇呴』涓篒nt绫诲瀷", "result": None})
final = a-b
return jsonify({"code":2,"msg":"鎴愬姛","result":str(final)})
if __name__=="__main__": #濡傛灉鏄粠鏈ā鍧楄繍琛岀殑灏辫繍琛屼笅闈㈢殑
app.run()
鍐欏畬鍚庣户缁?POSTMAN 璋冪敤鏌ョ湅鏄惁鎴愬姛锛?/p>
6銆佸疄渚?4
涓嬮潰缁欏ぇ瀹惰璁?Flask 缂栧啓 HTML 缃戦〉銆?/p>
-
1銆佹柊寤烘枃浠跺す锛?/span> templates[鍥哄畾鍐欐硶]
-
2銆佹柊寤?login.html[鏀惧湪templates涓璢
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!-- 鏂?Bootstrap4 鏍稿績 CSS 鏂囦欢 -->
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
<title>Title</title>
</head>
<body>
<h1>鎺ュ彛娴嬭瘯鎶ュ憡<h1>
<h2>鎵ц浜猴細{{tester}}<h2> #2涓嫭鍙疯〃绀哄彉閲?/span>
<p>鎴愬姛锛歿{pass_NO}}</p>
<p>澶辫触锛歿{fail_NO}}</p>
<table class="table">
<div class="container">
<tr><td>鐢ㄤ緥鍚嶇О</td><td>鎵ц缁撴灉</td></tr>
{% for item in data %}
<tr><td>{{item['case']}}</td><td>{{item['result']}}</td></tr>
{% endfor %}
</div>
</table>
</body>
</html>
-
3銆佺紪鍐?demo3.py
from flask import Flask,request,jsonify,render_template
app=Flask(__name__)
data=[
{'case':'test_sub01','result':"PASS"},
{'case': 'test_sub02', 'result': "FAIL"},
{'case': 'test_sub03', 'result': "PASS"},
{'case': 'test_sub04', 'result': "PASS"}
]
@app.route('/login')
def login():
return render_template('login.html',tester="鍊╁€?,pass_NO=4,fail_NO=1,data=data)
if __name__=="__main__":
app.run()
璧炴垜 馃 璧炴垜 馃 璧炴垜 馃 璧炴垜 馃 璧炴垜 馃 璧炴垜 馃
以上是关于的主要内容,如果未能解决你的问题,请参考以下文章