Skip to content

Latest commit

 

History

History
66 lines (44 loc) · 1.55 KB

jinja.邮件报告.md

File metadata and controls

66 lines (44 loc) · 1.55 KB

Jinja2 快速生成报告

一、引入相关库文件

import pandas as pd
from jinja2 import Environment, FileSystemLoader

二、 编写代码

1. 基础渲染

data = {'strategy_name': '第一个策略',
        'start_time': '2020-01-01',
        'end_time': '2021-06-01',
        'money': 20000}

env = Environment(loader=FileSystemLoader('./'))

template = env.get_template('jinjia.email-template.html')

with open("out.html", 'w+', encoding='utf-8') as f:
    out = template.render(strategy_name=data['strategy_name'],
                          start_time=data['start_time'],
                          end_time=data['end_time'],
                          money=data['money'])
    f.write(out)
    f.close()

2. 表格渲染

df = pd.read_csv('jinjia.601318-中国平安-历史日行情.csv')

data = df.to_dict('records')

results = {}
results.update({'strategy_name': '第一个策略',
                'start_time': '2020-01-01',
                'end_time': '2021-06-01',
                'money': 20000,
                'items': data})

env = Environment(loader=FileSystemLoader('./'))

template = env.get_template('jinjia.email-template.html')

with open("out.html", 'w+', encoding='utf-8') as f:
    out = template.render(strategy_name=results['strategy_name'],
                          start_time=results['start_time'],
                          end_time=results['end_time'],
                          money=results['money'],
                          items=results['items'])
    f.write(out)
    f.close()