当前位置:网站首页>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
边栏推荐
猜你喜欢
![[ROS] Introduction to common tools in ROS (to be continued)](/img/ea/e390106f750bf697e62a3a296014d2.png)
[ROS] Introduction to common tools in ROS (to be continued)

paddleocr window10 first experience

Deep learning framework pytorch rapid development and actual combat chapter4

yolov5,yolov4,yolov3乱七八糟的

How to solve mysql service cannot start 1069
![[ROS] The software package of the industrial computer does not compile](/img/a8/a1347568170821e8f186091b93e52a.png)
[ROS] The software package of the industrial computer does not compile

跑yolov5又出啥问题了(1)p,r,map全部为0

logback源码阅读(一)获取ILoggerFactory、Logger

chapter7
创建ROS工作空间](/img/2a/11e5023ef6d052d98b4090d2eea017.png)
[ROS](01)创建ROS工作空间
随机推荐
Tornado框架路由系统介绍及(IOloop.current().start())启动源码分析
xshell连接虚拟机步骤_建立主机与vm虚拟机的网络连接
Chapter6 visualization (don't want to see the version)
瑞吉外卖笔记——第05讲Redis入门
8583 顺序栈的基本操作
跑跑yolov5吧
瑞吉外卖笔记——第10讲Swagger
Flask请求应用上下文源码分析
AWVS工具介绍[通俗易懂]
IDEA打包jar包
yolov5改进(一) 添加注意力集中机制
8581 Linear linked list inversion
如何解决mysql服务无法启动1069
Linux:CentOS 7 安装MySQL5.7
Shell脚本完成pxe装机配置
Unit 11 Serializers
第二届中国Rust开发者大会(RustChinaConf 2021~2022)线上大会正式开启报名
Flask框架
世界上最大的开源基金会 Apache 是如何运作的?
[ROS](05)ROS通信 —— 节点,Nodes & Master