美好的一天,我正在python上编写脚本,用于发送stastistic并需要制作模板,该模板将附加到电子邮件正文
import jinja2
import os
JINJA_ENVORIMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__) + '\\templates'))
data = {'250': {'incoming': 0, 'outgoing': 0, 'inner': 0, 'talk_time': []},
'260': {'incoming': 4, 'outgoing': 2, 'inner': 0, 'talk_time': [273, 17, 140, 266, 148, 1077]},
'280': {'incoming': 2, 'outgoing': 23, 'inner': 1, 'talk_time': [83, 52, 287, 17, 398, 162, 67, 479, 23, 766, 643, 30, 72, 40, 8, 11, 12, 31, 130, 325, 113, 464, 115, 46, 23, 1]},
'290': {'incoming': 2, 'outgoing': 16, 'inner': 0, 'talk_time': [49, 69, 39, 2315, 69, 77, 336, 33, 96, 193, 202, 55, 698, 1114, 34, 384, 37, 2874]},
'310': {'incoming': 13, 'outgoing': 17, 'inner': 0, 'talk_time': [5, 4, 6, 426, 502, 104, 838, 426, 49, 69, 86, 1, 1470, 408, 597, 425, 178, 11, 746, 216, 159, 129, 1101, 239, 61, 358, 149, 65, 63, 199]},
'320': {'incoming': 16, 'outgoing': 17, 'inner': 0, 'talk_time': [460, 21, 606, 16, 54, 80, 139, 382, 488, 406, 946, 15, 44, 1185, 58, 111, 3, 160, 351, 25, 28, 14, 49, 67, 56, 67, 66, 72, 1194, 391, 384, 231, 417]},
'340': {'incoming': 21, 'outgoing': 4, 'inner': 1, 'talk_time': [107, 492, 262, 137, 184, 1033, 262, 31, 88, 228, 375, 140, 110, 684, 115, 1404, 140, 418, 363, 87, 687, 239, 109, 460, 65, 319]},
'350': {'incoming': 11, 'outgoing': 8, 'inner': 1, 'talk_time': [125, 290, 3, 9, 170, 3, 41, 280, 771, 77, 18, 21, 198, 43, 347, 285, 65, 28, 67, 47]},
'360': {'incoming': 9, 'outgoing': 17, 'inner': 0, 'talk_time': [61, 78, 20, 111, 74, 61, 27, 376, 461, 3, 507, 16, 1151, 181, 147, 21, 123, 51, 121, 83, 16, 545, 1256, 203, 1154, 929]},
'370': {'incoming': 13, 'outgoing': 28, 'inner': 0, 'talk_time': [49, 225, 20, 148, 41, 672, 257, 96, 573, 106, 121, 113, 247, 340, 265, 138, 96, 131, 493, 810, 49, 43, 514, 306, 431, 536, 624, 15, 16, 14, 76, 24, 15, 161, 419, 113, 25, 5, 109, 470, 41]},
'510': {'incoming': 4, 'outgoing': 19, 'inner': 0, 'talk_time': [503, 531, 141, 104, 364, 96, 100, 197, 175, 57, 132, 224, 144, 204, 23, 91, 44, 123, 54, 65, 48, 81, 10]},
'130': {'incoming': 0, 'outgoing': 2, 'inner': 1, 'talk_time': [212, 36, 75]},
'180': {'incoming': 0, 'outgoing': 5, 'inner': 0, 'talk_time': [66, 87, 269, 91, 92]},
'150': {'incoming': 4, 'outgoing': 22, 'inner': 0, 'talk_time': [47, 20, 1, 119, 46, 94, 56, 33, 42, 43, 1, 70, 80, 47, 43, 2, 5, 62, 28, 63, 40, 4, 39, 9, 109, 104]}}
template = JINJA_ENVORIMENT.get_template('test_template.html')
print(template.render(data))
此处模板https://codepen.io/vankalif/pen/NOEmdY
但是print(template.render(data))
不会打印带有传输数据的模板。 <tbody>
块为空。我做错了什么?谢谢。
答案 0 :(得分:1)
我认为您需要使用以下命令进行渲染:
print(template.render(data=data))
在data=data
中,第一个data
是模板变量的名称(即模板中的名称),第二个data
是提供值的python变量的名称(无法说出数据!)作为模板变量。