Python Flask 返回函数 、带值的函数、装饰器设置全局函数、|(管道符)传值
前言全局说明
一、安装flask模块
官方源:
pip3 install flask==2.3.2
国内源:
pip3 install flask==2.3.2 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
以上二选一,哪个安装快用哪个
flask 安装时间 2023-11
更多国内源: https://www.cnblogs.com/wutou/p/17949398
二、引用模块
from flask import Flask
三、启动服务
https://www.cnblogs.com/wutou/p/17949220
四、返回函数
4.1.1文件名:index.py
from flask import Flask, render_template, request
app=Flask(__name__)
def func():
return '你好'
@app.route('/')
def index():
if request.method == 'GET':
## GET请求
return render_template('index.html', content = 'Hello Flask', f = func)
if __name__ == '__main__':
# app.debug = True
# app.run(host='127.0.0.1',port = 5000)
app.run(host='0.0.0.0',port = 5000)
4.1.2 文件名:index.html
<html lang="zh-cn">
<head>
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
</head>
<body>
<h2>{{ content }}</h2>
{{ f() }}
</body>
</html>
4.2 访问连接:
4.3 效果:

五、返回函数 带参数
5.1.1 文件名:index.py
from flask import Flask, render_template, request
app=Flask(__name__)
def func(args):
return '你好' + ' ' + args
@app.route('/')
def index():
if request.method == 'GET':
## GET请求
return render_template('index.html', content = 'Hello Flask', f = func)
if __name__ == '__main__':
# app.debug = True
# app.run(host='127.0.0.1',port = 5000)
app.run(host='0.0.0.0',port = 5000)
def func(args) 增加了 args 接收参数值
5.1.2 文件名:index.html
<html lang="zh-cn">
<head>
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
</head>
<body>
<h2>{{ content }}</h2>
{{ f("Flask") }}
</body>
</html>
{{ f("Flask") }} html 里给函数传值 Flask
5.2 访问连接:
5.3 效果:

六、装饰器把函数,设置成全局函数
上面看到,render_template 可以返回函数,给html。
但是,如果每个html需要用到函数都要传值,太麻烦。
可以像设置全局变量一样,用装饰器把函数设置成全局函数,这样任意 html 都可以直接调用
6.1.1 文件名:index.py
from flask import Flask, render_template, request
app=Flask(__name__)
@app.template_global()
def func(args):
return '你好' + ' ' + args
@app.route('/')
def index():
if request.method == 'GET':
return render_template('index.html')
if __name__ == '__main__':
# app.debug = True
# app.run(host='127.0.0.1',port = 5000)
app.run(host='0.0.0.0',port = 5000)
使用 @app.template_global() 装饰器
注意: 如果是在"蓝图"(blue print) 注册,那么就只在蓝图中生效
6.1.2 文件名:index.html
<html lang="zh-cn">
<head>
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
</head>
<body>
{{ func("Flask") }}
</body>
</html>
6.2 访问连接:
6.3 效果:

七、装饰器 设置 |(管道符)传值
7.1.1 文件名:index.py
from flask import Flask, render_template, request
app=Flask(__name__)
@app.template_filter()
def func(args, name):
return '你好' + ' ' + args + ' ' + name
@app.route('/')
def index():
if request.method == 'GET':
return render_template('index.html')
if __name__ == '__main__':
# app.debug = True
# app.run(host='127.0.0.1',port = 5000)
app.run(host='0.0.0.0',port = 5000)
使用了 @app.template_filter() 装饰器
注意: 如果是在"蓝图"(blue print) 注册,那么就只在蓝图中生效
7.1.2 文件名:index.html
<html lang="zh-cn">
<head>
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
</head>
<body>
{{ "Hello"|func("Flask") }}
</body>
</html>
7.2 访问连接:
7.3 效果:

免责声明:本号所涉及内容仅供安全研究与教学使用,如出现其他风险,后果自负。
参考、来源:
https://www.bilibili.com/video/BV11Y411h71J?p=31
浙公网安备 33010602011771号