当前位置:网站首页>Use of flask
Use of flask
2022-07-27 08:33:00 【pink_ Pig___】
1. install
- install Flask
pip install Flask==1.1.4
- Install some of his configurations , Create a req_new.txt file , Save saved content to req_new.txt In the document
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
- Click the command on the terminal data
pip install -r req_new.txt
It is best to configure in a virtual environment , In case of conflict with other libraries
2. Flask Use
- Create a app.py file , Write code in it
from flask import Flask
app = Flask(__name__)
# Static methods “ route ”
@app.route('/hello')
def hello():
return 'hello word'
# Right click to run , Click the link to enter the route
if __name__ == '__main__':
app.run()
- There are three ways to automatically refresh and save ,
The first method , Create a settings Create a... In the folder config.py Write code in the file
class DefaultConfig:
# Attribute name It has to be all in capitals
NAME = 'zhangsan'
DEBUG = True
stay app.py Use... In the document
from flask import Flask
from settings.config import DefaultConfig
app = Flask(__name__)
# Load from object
app.config.from_object(DefaultConfig)
print(app.config)
@app.route('/hello')
def hello():
return "hello"
if __name__ == '__main__':
app.run()
The second method , Create one directly under the root directory setting.py File write code
AGE = 18
stay app.py Use... In the document
from flask import Flask
app = Flask(__name__)
# Load from file
app.config.from_pyfile('setting.py')
print(app.config)
@app.route('/hello')
def hello():
return 'hello word'
if __name__ == '__main__':
app.run()
The third method , Compared with the above two methods, it should be safe , It's used to put some inscriptions , Create a... On the desktop pass.py File write code
PASS = 123456
You need to configure environment variables when using 

Click OK after setting
stay app.py Use... In the document
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. Interface
# Interface
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(' Please enter the animal type Cat/Dog:')
print(' What was received was :', a)
Forset().sav(a)
4. encapsulation Flask Method
- Create a create_app.py File write code
from flask import Flask
def create_app():
flask_app = Flask(__name__)
return fiask_app
create_app()
- There are also three ways to use
The first one is
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()
The second kind
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()
The third method
from create_app import create_app
from flask import url_for
app = create_app()
print(app)
# No decorators
def ccc():
return 'ccc'
app.add_url_rule('/ccc', 'ccc', ccc)
When it runs, it needs to change the environment variables 

function 
success 
边栏推荐
- Plato farm is expected to further expand its ecosystem through elephant swap
- It's better to be full than delicious; It's better to be drunk than drunk
- Redis configuration file download
- How to view instances of software objects in QSIM?
- 如何在qsim查看软件对象的实例?
- Breadth first search
- JS rotation chart
- JS advanced knowledge - function
- while Loop
- 1176 questions of Olympiad in informatics -- who ranked K in the exam
猜你喜欢

面试官:什么是脚手架?为什么需要脚手架?常用的脚手架有哪些?

redis配置文件下载

VS Code中#include报错(新建的头文件)

Hundreds of people participated. What are these people talking about in the opengauss open source community?

It's better to be full than delicious; It's better to be drunk than drunk

海量数据肖枫:共建共治openGauss根社区,共享欣欣向荣新生态

UVM入门实验1

开怀一笑

Vcenter7.0 installation of ibm3650m4 physical machine
![[NPUCTF2020]ReadlezPHP 1](/img/d9/590446b45f917be3f077a9ea739c20.png)
[NPUCTF2020]ReadlezPHP 1
随机推荐
SSTI template injection
Containerd failed to pull private database image (kubelet)
[geek challenge 2019] finalsql 1
CMD command and NPM command
IBM3650M4实体机安装VCenter7.0
海量数据肖枫:共建共治openGauss根社区,共享欣欣向荣新生态
docker 安装mysql后进入容器内部发现登录不了mysql
Installation and use of Supervisor
Introduction, installation and use of netdata performance monitoring tool
Management of product pictures
Openresty + keepalived to achieve load balancing + IPv6 verification
Openresty + keepalived 实现负载均衡 + IPV6 验证
2022/7/26 exam summary
Ubuntu: install PostgreSQL
Have a good laugh
Luogu Taotao picks apples
Element display mode: block level, inline, inline block, nesting specification, display mode conversion
Redis configuration file download
Initial summary of flask framework creation project
Use of "PHP Basics" Boolean