python 利用jinja2模板生成html代码实例
作者:南鱼羁荒渡 时间:2023-11-19 18:56:41
这篇文章主要介绍了python 利用jinja2模板生成html代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
from jinja2 import Environment, FileSystemLoader
import json
def generate_html(data):
env = Environment(loader=FileSystemLoader('./')) # 加载模板
template = env.get_template('模板.html')
# template.stream(body).dump('result.html', 'utf-8')
with open("result.html", 'w') as fout:
html_content = template.render(data={})
fout.write(html_content) # 写入模板 生成html
if __name__ == "__main__":
result = {}
generate_html(result)
#### 脚本执行 添加进度信息ff = open('1.txt','w')
def open_file(file_name):
try:
with open(file_name,'a+')as f: # 例如打开一个大文件
fsize = os.path.getsize(file_name) # 先计算大文件大小
line_list_size = 0
for line_list in f:
line_list_size += sys.getsizeof(line_list) # 计算处理文件大小
done = int(float(line_list_size)/ fsize*10) # 计算进度
sys.stdout.write("\r[%s%s] %d%%" % ('█' * done*2, ' ' * (20 - done*2), 10 * done))
sys.stdout.flush() # 刷新到控制台
except Exception, e:
print e
return '文件操作失败'
if __name__ == "__main__":
file_name = '235'
print '---- start ----'
open_file(file_name)
print '\n---- end ----'
来源:https://www.cnblogs.com/nanyu/p/11644985.html
标签:python,jinja2,模板,生成,html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
tensorflow实现将ckpt转pb文件的方法
2023-03-24 19:05:05
浅谈JavaScript的自动垃圾收集机制
2023-08-13 15:18:57
Go编写定时器与定时任务详解(附第三方库gocron用法)
2024-05-09 09:40:19
Python配置文件解析模块ConfigParser使用实例
2023-10-19 09:10:19
SQL Server实现分页方法介绍
2024-01-15 12:54:45
![](https://img.aspxhome.com/file/2023/5/88245_0s.jpg)
Python requests上传文件实现步骤
2023-09-25 15:14:50
基于python的七种经典排序算法(推荐)
2023-06-16 18:21:43
![](https://img.aspxhome.com/file/2023/0/107220_0s.png)
Python使用defaultdict读取文件各列的方法
2021-08-05 00:34:24
如何提取python字符串括号中的内容
2021-01-11 01:29:03
![](https://img.aspxhome.com/file/2023/0/72870_0s.png)
python Dijkstra算法实现最短路径问题的方法
2022-02-21 03:08:51
TensorFlow如何指定GPU训练模型
2022-05-31 10:03:21
![](https://img.aspxhome.com/file/2023/8/82818_0s.png)
Jebrains付费插件Activation code[持续更新]
2022-01-31 10:09:28
Oracle常用命令大全集
2010-07-21 13:18:00
Python3中exp()函数用法分析
2023-06-11 03:17:24
对于Python中线程问题的简单讲解
2023-12-28 10:16:31
使用Python实现火车票查询系统(带界面)
2022-04-26 04:14:47
![](https://img.aspxhome.com/file/2023/9/134999_0s.jpg)
Python数据结构之双向链表的定义与使用方法示例
2023-06-29 06:20:45
![](https://img.aspxhome.com/file/2023/6/95126_0s.png)
php swoft框架实例用法
2024-05-11 09:45:02
FCKeditor 2.6.6在ASP中的安装及配置方法分享
2022-09-16 12:45:32
![](https://img.aspxhome.com/file/2023/6/70616_0s.bmp)
创建Django项目图文实例详解
2021-06-12 23:09:30
![](https://img.aspxhome.com/file/2023/7/110227_0s.png)