阅读更多
week_jinja2.py
# coding=utf-8
import os, sys
import jinja2
import json
if len(sys.argv) ==2:
f1 = sys.argv[1]
else:
print('usage: week_jinja2.py fcode.json ')
sys.exit(1)
if not os.path.exists(f1):
print("Error: %s not found." % f1)
sys.exit(1)
fn,ext = os.path.splitext(f1)
if len(fn) !=6:
print('Error: len(%s) !=6' % fn)
sys.exit(1)
if ext !='.json':
print('Error: %s is not .json' % f1)
sys.exit(1)
fcode = fn +' K线图'
title = json.dumps(fcode)
# 读取 json 数据
fp = open(f1,'r')
data = fp.read()
fp.close()
# 1.配置模板文件搜索路径
loader = jinja2.FileSystemLoader(searchpath='/funds')
# 2.创建环境变量
env = jinja2.Environment(loader=loader)
# 3.加载模板,渲染数据
template = env.get_template("echarts_kline.html",'utf-8')
html = template.render(title=title, rawData=data)
# 4.写到文件
f2 = fn +'.html'
with open(f2,'w') as fp:
fp.write(html)
print(f2)