当前位置:网站首页>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
- 理解TCP长连接(Keepalive)
- 第十五单元 分页、过滤
- 【ROS】编译软件包packages遇到进度缓慢或卡死,使用swap
- The most complete ever!A collection of 47 common terms of "digital transformation", read it in seconds~
- [ROS] Compiling packages packages encounters slow progress or stuck, use swap
- 第十单元 前后连调
- Verilog学习 系列
- [ROS](02)创建&编译ROS软件包Package
- 第十二单元 关联序列化处理
猜你喜欢
logback源码阅读(一)获取ILoggerFactory、Logger
[ROS](01)创建ROS工作空间
Flask项目的完整创建 七牛云与容联云
世界上最大的开源基金会 Apache 是如何运作的?
Cloin 控制台乱码
[ROS](05)ROS通信 —— 节点,Nodes & Master
paddleocr window10 first experience
Deep learning framework pytorch rapid development and actual combat chapter4
[ROS](06)ROS通信 —— 话题(Topic)通信
史上最全!47个“数字化转型”常见术语合集,看完秒懂~
随机推荐
chapter7
网页设计(新手入门)[通俗易懂]
Unit 10 Continuous Tuning
LayoutParams的详解
paddleocr window10初体验
第十四单元 视图集及路由
第七单元 ORM表关系及操作
window10下半自动标注
STM32(F407)—— 堆栈
Some impressions of the 519 plummet 2021-05-21
php开源的客服系统_在线客服源码php
chapter7
此次519暴跌的几点感触 2021-05-21
What is the difference between web testing and app testing?
无序数组排序并得到最大间隔
使用云GPU+pycharm训练模型实现后台跑程序、自动保存训练结果、服务器自动关机
What are the file encryption software?Keep your files safe
How does Apache, the world's largest open source foundation, work?
YOLOv7使用云GPU训练自己的数据集
世界上最大的开源基金会 Apache 是如何运作的?