当前位置:网站首页>Building and getting started with the Flask framework
Building and getting started with the Flask framework
2022-08-02 14:20:00 【czy1206527605】
预备(虚拟环境的搭建)
cd venv/Scripts
.\activate 进入虚拟环境
cd ../../
deactivate.bat 退出虚拟环境
pip install -r req_new.txt 指定文件下载
一.Flask搭建
Internal plugins to download
快捷下载(Put the following packages intxt文件中,加入到flask根目录下
运行 pip install -r ***.txt)
Flask==1.1.4
Flask-Caching==1.10.1
Flask-Cors==3.0.10
Flask-Migrate==2.7.0
Flask-RESTful==0.3.9
Flask-Script==2.0.6
Flask-SQLAlchemy==2.5.1
MarkupSafe==2.0.1
PyJWT==2.3.0
PyMySQL==1.0.2
qiniu==7.6.0
redis==4.2.2
二.Flask入门
Ss2.flask的必要元素
<1>.flask基本框架
#1
(导包)
from flask import Flask
#2
(实例化Flask对象)
app = Flask(__name__)
(Define functions and their routes)
@app.route("/hello")
def hello():
return "hello world"
#3
(运行)
if __name__ == '__main__'
app.run()
<2> 从对象中加载配置文件
- 在目录settings中创建配置文件 config.py
class DefaultConfig:
# 属性名字 必须全部大写
NAME = "zhangsan"
DEBUG = True
- 根目录下创建app.py文件
from flask import Flask
from settings.config import DefaultConfig
app = Flask(__name__)
app.config.from_object(DefaultConfig) # 从对象中加载配置
print(app.config)
if __name__ == '__main__':
app.run()
注意!!!
Running the code at this point willconfigto the following information
DEBUGThe method is developer mode,设置成TrueEnables automatic refresh of the run
<3> Load configuration file from file
- 根目录下创建setting.py
Feel free to write parameters
AGE="18"
- 根目录下创建app.py文件
from flask import Flask
from settings.config import DefaultConfig
app = Flask(__name__)
app.config.from_pyfile('setting.py') # 从文件中加载配置
print(app.config)
if __name__ == '__main__':
app.run()
<4> 从环境变量中加载配置文件
Haven't mastered it yet
三.Simple import of design patterns
1.根目录下创建forest.py文件
class Animal():
def do_say(self):
pass
class Cat(Animal):
def do_say(self):
print("miaomiao")
class Dog(Animal):
def do_say(self):
print("wangwang")
class Forest():
def say(self, animal_type):
eval(animal_type)().do_say()
if __name__ == '__main__':
a = input("Please enter the type of animal entered: Cat or Dog")
print("接收到的数据为:", a)
Forest().say(a)
2.工厂模式创建APP
在根目录创建create_app()
from flask import Flask
# 创建flask对象
def create_app():
flask_app = Flask(__name__)
print("我被调用了")
return flask_app
同级目录下创建app.py调用
from create_app import create_app
app = create_app()
if __name__ == "__main__":
app.run()
四.路由的定义
The basic format of the route
@app.route("/hello",methods=["get","post"],endpoint="aaa")
def hello():
return "hello"
- url 路由路径
- methods 请求方法
- endpoint 别名(可有可无)
通过url-for method to find the route aliasendpoint
@app.route("/path")
def get_path():
u = url_for("aaa")
print(u)
return u
边栏推荐
猜你喜欢
随机推荐
云GPU(恒源云)训练的具体操作流程
【VCU】详解S19文件(S-record)
8580 Merge linked list
Verilog学习 系列
drf视图组件
此次519暴跌的几点感触 2021-05-21
Flask框架
第十三单元 混入视图基类
[ROS](06)ROS通信 —— 话题(Topic)通信
About the development forecast of the market outlook?2021-05-23
[ROS] The difference between roscd and cd
动手学ocr(一)
8581 线性链表逆置
如何解决mysql服务无法启动1069
Unit 11 Serializers
深度学习框架pytorch快速开发与实战chapter4
Supervision strikes again, what about the market outlook?2021-05-22
关于密码加密的一点思路
replay视频播放器_怎么让手机音乐跟视频一起放
[ROS] Introduction to common tools in ROS (to be continued)








