当前位置:网站首页>Flask的使用
Flask的使用
2022-07-27 05:02:00 【pink_Pig___】
1. 安装
- 安装Flask
pip install Flask==1.1.4
- 安装他的一些配置,在根目录下创建一个req_new.txt文件,将已下内容保存到req_new.txt文件里
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
- 在终端数据一下命令
pip install -r req_new.txt
最好是在虚拟环境中配置,以防与其他的库发生冲突
2. Flask的使用
- 创建一个app.py 文件,里面写入代码
from flask import Flask
app = Flask(__name__)
# 静态方法“路由”
@app.route('/hello')
def hello():
return 'hello word'
# 直接右键运行,点击链接输入路由
if __name__ == '__main__':
app.run()
- 自动刷新保存有三种方法,
第一种方法,在根目录下创建一个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)
@app.route('/hello')
def hello():
return "hello"
if __name__ == '__main__':
app.run()
第二种方法,在根目录下直接创建一个setting.py文件写入代码
AGE = 18
在app.py文件中使用
from flask import Flask
app = Flask(__name__)
# 从文件中加载
app.config.from_pyfile('setting.py')
print(app.config)
@app.route('/hello')
def hello():
return 'hello word'
if __name__ == '__main__':
app.run()
第三种方法,相比以上两种方法要有安全性,用来放一些铭感东西,在桌面创建一个pass.py 文件写入代码
PASS = 123456
使用时需要配置一下环境变量

设置好之后点击确定
在app.py文件中使用
from flask import Flask
app = Flask(__name__)
app.config.from_envvar('password')
print(app.config)
@app.route('/hello')
def hello():
return 'hello word'
if __name__ == '__main__':
app.run()
3. 接口
# 接口
class Animal():
def do_say(self):
pass
class Cat(Animal):
def do_say(self):
print('miao miao')
class Dog(Animal):
def do_say(self):
print('wang wang')
class Forset():
def sav(self, animal_type):
eval(animal_type)().do_say()
if __name__ == '__main__':
a = input('请输入动物类型Cat/Dog:')
print('接收到的是:', a)
Forset().sav(a)
4. 封装Flask方法
- 在根目录下创建一个create_app.py文件写入代码
from flask import Flask
def create_app():
flask_app = Flask(__name__)
return fiask_app
create_app()
- 使用也有三种方法使用
第一种
from create_app import create_app
from flask import url_for
app = create_app()
print(app)
@app.route('/hello', methods=["get", "post"], endpoint="aaa")
def hello():
return 'hello'
if __name__ == '__main__':
app.run()
第二种
from create_app import create_app
from flask import url_for
app = create_app()
print(app)
@app.route('/hello', methods=["get", "post"], endpoint="aaa")
def hello():
return 'hello'
@app.route('/path')
def get_path():
u = url_for('aaa')
print(u)
return u
if __name__ == '__main__':
app.run()
第三种方法
from create_app import create_app
from flask import url_for
app = create_app()
print(app)
# 不用装饰器
def ccc():
return 'ccc'
app.add_url_rule('/ccc', 'ccc', ccc)
他的运行的时候需要改环境变量

运行
成功
边栏推荐
- JVM上篇:内存与垃圾回收篇十二--StringTable
- JVM Part 1: memory and garbage collection part 9 - runtime data area - object instantiation, memory layout and access location
- mq设置过期时间、优先级、死信队列、延迟队列
- 2022 Zhengzhou light industry Freshmen's competition topic - I won't say if I'm killed
- B1029 旧键盘
- File processing (IO)
- Detailed description of binary search tree
- 秒杀系统设计
- The interface can automatically generate E and other asynchronous access or restart,
- Shell course summary
猜你喜欢

Use ngrok for intranet penetration

JVM Part 1: memory and garbage collection part 11 -- execution engine

Database connection pool & Druid usage

Use of collection framework

李宏毅机器学习组队学习打卡活动day06---卷积神经网络

JVM上篇:内存与垃圾回收篇七--运行时数据区-堆

JVM Part 1: memory and garbage collection -- runtime data area 4 - program counter

Acticiti中startProcessInstanceByKey方法在variable表中的如何存储

pytorch 数据类型 和 numpy 数据 相互转化

Typescript details
随机推荐
JVM Part 1: memory and garbage collection part 9 - runtime data area - object instantiation, memory layout and access location
Integrate SSM
简化JDBC的MyBits框架
The receiver sets the concurrency and current limit
Detailed explanation of pointer constant and constant pointer
Derivation and explanation of PBR physical illumination calculation formula
Detailed description of polymorphism
2022年郑州轻工业新生赛题目-打死我也不说
Typescript details
秒杀系统设计
Database connection pool & Druid usage
李宏毅机器学习组队学习打卡活动day02---回归
numpy 数据类型转化
Use of collection framework
牛客剑指offer--JZ12 矩阵中的路径
JDBC API 详解
实用小工具: Kotlin 代码片段
B1023 group minimum
mq设置过期时间、优先级、死信队列、延迟队列
The difference between strlen and sizeof