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')}}
相关推荐
E_ICEBLUE38 分钟前
Python 实现 PDF 表单域的自动化创建与智能填充
python·pdf·自动化·表单域
jdbcaaa43 分钟前
Go 语言 runtime 包的使用与注意事项
开发语言·后端·golang·runtime
YJlio7 小时前
1.7 通过 Sysinternals Live 在线运行工具:不下载也能用的“云端工具箱”
c语言·网络·python·数码相机·ios·django·iphone
l1t7 小时前
在wsl的python 3.14.3容器中使用databend包
开发语言·数据库·python·databend
青云计划8 小时前
知光项目知文发布模块
java·后端·spring·mybatis
Victor3568 小时前
MongoDB(9)什么是MongoDB的副本集(Replica Set)?
后端
Victor3568 小时前
MongoDB(8)什么是聚合(Aggregation)?
后端
山塘小鱼儿8 小时前
本地Ollama+Agent+LangGraph+LangSmith运行
python·langchain·ollama·langgraph·langsimth
码说AI9 小时前
python快速绘制走势图对比曲线
开发语言·python