jinja2 模板使用

task

有时候需要把数据(比如csv,或者\t分割)转换成html,方便查看。

jinja安装

pip install Jinja2
doc: http://docs.jinkan.org/docs/jinja2/index.html

过程

demo数据

1,2,3,4,5
6,7,8,9,10

把这两行数据渲染在表格里

  1. 创建your_template.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="cmn-Hans-CN">
    <head>
        <title>
            format
        </title>
        <meta charset={{ encoding }}>
        <style type="text/css">
            body{FONT-SIZE: 13px; COLOR: #fffffff; LINE-HEIGHT: 19px;word-spacing:1px}
            table{border-collapse:collapse; border: 1px;} th{background: #3e83c9; border:
            1px solid #95bce2;font-weight:bold;} td{padding: 4px;border: 1px solid
            #95bce2;vertical-align: top;background-color:expression((this.parentElement.sectionRowIndex%2==1)?'#ECF6FC':'#ffffff');
            text-aligin:right;} td.name {font-weight:bold;} td.top {background: #d2e9ff;}
            td.bg {background: #f0f0f0;}
        </style>
    </head>
    <body>
        <table>
            <tr>
                <th nowrap="">
                    mt
                </th>
                <th nowrap="">
                    flow
                </th>
                <th nowrap="">
                    总消费
                </th>
                <th nowrap="">
                    关闭消费
                </th>
                <th nowrap="">
                    关闭占比
                </th>
                <th nowrap="">
                    打开占比
                </th>
            </tr>
            {% for item in data %}
            <tr>
                <td class="bg">
                    {{ item[0] }}
                </td>
                <td class="bg">
                    {{ item[1] }}
                </td>
                <td class="bg">
                    {{ item[2] }}
                </td>
                <td class="bg">
                    {{ item[3] }}
                </td>
                <td class="bg">
                    {{ item[4] }}
                </td>
                <td class="bg">
                    {{ item[5] }}
                </td>
            </tr>
            {% endfor %}
        </table>
    </body>

</html>
  1. html生成脚本gen_html.py
 # encoding=utf-8
 import sys
 from jinja2 import Environment, FileSystemLoader

 env = Environment(loader=FileSystemLoader('./', encoding='utf-8'))

 data = []
 with open('./data') as f:
     for line in f:
         line = line.decode('gb18030', 'ignore').strip()
         cols = line.split(u'\t')
         data.append(cols)

 template = env.get_template('your_template.html')
 html = template.render(date=date, encoding='utf-8', data=data)  # unicode string
 print >> sys.stdout, html.encode('utf-8', 'ignore')
  1. email
 python gen_html.py > temp.html

 email_to="xxx@xxx.com"
 /usr/lib/sendmail -t <<< "To: $email_to
 From: yyy@yyy.com
 Subject: demo
 Content-type: text/html; charset=utf-8
 $(cat temp.html)
 "

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容