Flask 入门4:Flask 模板

1. 前言

Flask 拥有丰富的扩展方法,且都有统一的特点:简单和即学即用。当我们要实现某个功能之前,可以提前去搜一搜这个功能包是否已经存在,这样也能帮助我剩下很多时间。那么要去哪里找到这些扩展包呢,这里推荐两个方法供大家使用:

2. 模板

模板即一个服务器页面,在模板中我们可以使用服务端的语法,来控制最终的 html 页面输出。

jinja2 :https://jinja.palletsprojects.com/en/3.1.x/templates/#filters

python 复制代码
@app.template_filter('md')
def markdown_to_html(txt):
    return markdown(txt)


def read_md(filename):
    with open(filename) as md_file:
        content = reduce(lambda x, y: x + y, md_file.readlines())
    return content.decode('utf-8')


@app.context_processor
def inject_method():
    return dict(read_md=read_md)
html 复制代码
{{body | md |safe }}
{{read_md('xxxx.md')}}
相关推荐
共享家95275 小时前
搭建 AI 聊天机器人:”我的人生我做主“
前端·javascript·css·python·pycharm·html·状态模式
Hgfdsaqwr6 小时前
Python在2024年的主要趋势与发展方向
jvm·数据库·python
一晌小贪欢6 小时前
Python 测试利器:使用 pytest 高效编写和管理单元测试
python·单元测试·pytest·python3·python测试
小文数模6 小时前
2026年美赛数学建模C题完整参考论文(含模型和代码)
python·数学建模·matlab
Halo_tjn6 小时前
基于封装的专项 知识点
java·前端·python·算法
Hgfdsaqwr6 小时前
掌握Python魔法方法(Magic Methods)
jvm·数据库·python
weixin_395448917 小时前
export_onnx.py_0130
pytorch·python·深度学习
s1hiyu7 小时前
使用Scrapy框架构建分布式爬虫
jvm·数据库·python
2301_763472467 小时前
使用Seaborn绘制统计图形:更美更简单
jvm·数据库·python
无垠的广袤7 小时前
【VisionFive 2 Lite 单板计算机】边缘AI视觉应用部署:缺陷检测
linux·人工智能·python·opencv·开发板