本篇实现的作用是利用Jinja2模板根据需要生成html 页面。 ghtml.py内容如下: 1# cat ghtml.py 2#!/usr/bin/env python 3# coding=utf-8 4# code from www.361way.com 5import os 6from jinja2 import Environment, FileSystemLoader 7PATH = os.path.dirname(os.path.abspath(__file__)) 8TEMPLATE_ENVIRONMENT = Environment( 9 autoescape=False, 10 loader=FileSystemLoader(os.path.join(PATH, 'templates')), 11 trim_blocks=False) 12def render_template(template_filename, context): 13 return TEMPLATE_ENVIRONMENT.get_template(template_filename).render(context) 14def create_index_html(): 15 fname = "output.html" 16 urls = ['http://www.361way.com/tag/python', 'http://www.361way.com/tag/linux', 'http://www.361way.com/tag/mysql'] 17 context = { 18 'urls': urls 19 } 20 # 21 with open(fname, 'w') as f: 22 html = render_template('index.html', context) 23 f.write(html) 24def main(): 25 create_index_html() 26######################################## 27if __name__ == "__main__": 28 main() templates/in……
Continue reading