当前位置:网站首页>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
边栏推荐
- 如何解决mysql服务无法启动1069
- Minio文件上传
- logback源码阅读(二)日志打印,自定义appender,encoder,pattern,converter
- 跑跑yolov5吧
- Cloin 控制台乱码
- How does Apache, the world's largest open source foundation, work?
- 动态刷新日志级别
- Unit 11 Serializers
- [ROS] The software package of the industrial computer does not compile
- The future of financial services will never stop, and the bull market will continue 2021-05-28
猜你喜欢
run yolov5
瑞吉外卖笔记——第10讲Swagger
动手学ocr(一)
yolov5,yolov4,yolov3 mess
[ROS] The software package of the industrial computer does not compile
此次519暴跌的几点感触 2021-05-21
Chapter6 visualization (don't want to see the version)
[ROS]ROS常用工具介绍(待续)
The world's largest Apache open source foundation is how it works?
史上最全!47个“数字化转型”常见术语合集,看完秒懂~
随机推荐
专访|带着问题去学习,Apache DolphinScheduler 王福政
Flask-RESTful请求响应与SQLAlchemy基础
[ROS]ROS常用工具介绍(待续)
How to solve mysql service cannot start 1069
The world's largest Apache open source foundation is how it works?
MySQL数据库设计规范
Sentinel源码(四)(滑动窗口流量统计)
redis分布式锁和看门狗的实现
Mysql's case the when you how to use
Flask框架
window10下半自动标注
8583 顺序栈的基本操作
Flask项目的完整创建 七牛云与容联云
paddle window10环境下使用conda安装
8581 线性链表逆置
chapter6可视化(不想看版)
第六单元 初识ORM
IDEA打包jar包
Supervision strikes again, what about the market outlook?2021-05-22
How to solve 1045 cannot log in to mysql server