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')}}
相关推荐
QD.Joker几秒前
高德MCP服务接入
python
孤客网络科技工作室4 分钟前
孤客截图工具 Pro - 从开发到打包的完整指南
python
Q_Q19632884754 分钟前
python+django/flask的医院财务管理系统
spring boot·python·django·flask·node.js
Moonbit17 分钟前
MoonBit Pearls Vol.14:哈希表避坑指南
后端·算法·编程语言
Moonbit29 分钟前
MoonBit Pearls Vol.13: 使用 MoonBit 开发一个 HTTP 文件服务器
服务器·后端·http
herogus丶30 分钟前
【LLM】LLaMA-Factory 训练模型入门指南
python·ai编程·llama
一 乐31 分钟前
个人博客|博客app|基于Springboot+微信小程序的个人博客app系统设计与实现(源码+数据库+文档)
java·前端·数据库·spring boot·后端·小程序·论文
谏书稀35 分钟前
LLaMA Factory微调大模型
python·transformer·llama
雨中散步撒哈拉1 小时前
16、做中学 | 初三上期 Golang面向对象_进阶
爬虫·python·golang
LucianaiB1 小时前
Qoder 降价,立即生效!首购 2 美金/月
后端