1.安裝Flask-Scriptpython
1
|
pip install Flask
-
Script
|
2.將Flask-Script加入到Flask項目中flask
1 import MyApp 2 # 導入 Flask-Script 中的 Manager 3 from flask_script import Manager 4 5 app = MyApp.create_app() 6 # 讓app支持 Manager 7 manager = Manager(app) 8 9 if __name__ == '__main__': 10 #app.run() 11 # 替換原有的app.run(),而後大功告成了 12 manager.run() 13 14 MyApp/manager.py 15 16 MyApp/manager.py
3.使用命令啓動flask項目app
1
|
python manager.py runserver
|
4.啓動項目並更改配置參數(監聽ip地址,監聽端口)ide
1
|
python manager.py runserver
-
h
0.0
.
0.0
-
p
9527
|
5.高級操做-自定製腳本命令spa
5.1.方式一 : @manager.commandcode
1 import MyApp 2 # 導入 Flask-Script 中的 Manager 3 from flask_script import Manager 4 5 app = MyApp.create_app() 6 # 讓app支持 Manager 7 manager = Manager(app) # type:Manager 8 9 @manager.command 10 def DragonFire(arg): 11 print(arg) 12 13 if __name__ == '__main__': 14 #app.run() 15 # 替換原有的app.run(),而後大功告成了 16 manager.run() 17 18 MyApp/manager.py 19 20 MyApp/manager.py
1
|
python manager.py DragonFire
666
|
5.2.方式二 : @manager.opation("-短指令","--長指令",dest="變量名")
server
1 import MyApp 2 # 導入 Flask-Script 中的 Manager 3 from flask_script import Manager 4 5 app = MyApp.create_app() 6 # 讓app支持 Manager 7 manager = Manager(app) # type:Manager 8 9 @manager.command 10 def DragonFire(arg): 11 print(arg) 12 13 @manager.option("-n","--name",dest="name") 14 @manager.option("-s","--say",dest="say") 15 def talk(name,say): 16 print(f"{name}你可真{say}") 17 18 if __name__ == '__main__': 19 #app.run() 20 # 替換原有的app.run(),而後大功告成了 21 manager.run() 22 23 MyApp/manager.py 24 25 MyApp/manager.py
1
2
|
python manager.py talk
-
n 趙麗穎
-
s 漂亮
python manager.py talk
-
-
name DragonFire
-
-
say NB
-
Class
|