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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
2022-01-11 Python模块之xlutils - 写入excel的xls文件
2022-01-11 Python模块之xlwt -写入excel的xls文件
2022-01-11 Python模块之xlrd - 读取excel的xls文件