当前位置:网站首页>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
边栏推荐
- logback源码阅读(二)日志打印,自定义appender,encoder,pattern,converter
- redis分布式锁和看门狗的实现
- 线代:已知一个特征向量快速求另外两个与之正交的特征向量
- 第十五单元 分页、过滤
- 跑yolov5又出啥问题了(1)p,r,map全部为0
- Flask-RESTful请求响应与SQLAlchemy基础
- WeChat Mini Program-Recent Dynamic Scrolling Implementation
- 如何解决1045无法登录mysql服务器
- 8581 Linear linked list inversion
- [ROS](04)package.xml详解
猜你喜欢
[ROS] Introduction to common tools in ROS (to be continued)
瑞吉外卖笔记——第10讲Swagger
The most complete ever!A collection of 47 common terms of "digital transformation", read it in seconds~
你接受不了60%的暴跌,就没有资格获得6000%的涨幅 2021-05-27
Object detection scene SSD-Mobilenetv1-FPN
瑞吉外卖笔记——第08讲读写分离
如何解决mysql服务无法启动1069
window10下半自动标注
IDEA打包jar包
The 2nd China Rust Developers Conference (RustChinaConf 2021~2022) Online Conference Officially Opens Registration
随机推荐
deal!It's July 30th!
Raft对比ZAB协议
跑跑yolov5吧
8576 Basic operations of sequential linear tables
Go语言初始
第五单元 保持状态
window10下半自动标注
Chapter6 visualization (don't want to see the version)
8580 合并链表
Interview | with questions to learn, Apache DolphinScheduler Wang Fuzheng
How to solve 1045 cannot log in to mysql server
Data Organization---Chapter 6 Diagram---Graph Traversal---Multiple Choice Questions
How to solve mysql service cannot start 1069
Flask框架
[ROS] (01) Create ROS workspace
第八单元 中间件
[ROS](06)ROS通信 —— 话题(Topic)通信
Unit 3 view layer
ping命令的使用及代码_通过命令查看ping路径
第三单元 视图层