我的jinja2模板输出意外。
我有一个实例,其中一个表填充了单个值或一组值。每个的表示略有不同,所以我想我可以用条件{% if my_variable is mapping %}
检查模板变量的状态,然后使用我的模板代码进行相应的处理。
这是我的模板代码:
<table class="summary_table halfpage">
<thead>
<tr>
<th>
My Table
</th>
</tr>
</thead>
<tbody>
{% if my_variable is mapping %}
{% for key,value in my_variable.iterrows() %}
<tr>
<td>
<p><strong>{{ value['Column1'] }} : </strong> {{ value['Column2'] }}</p>
</td>
</tr>
{% endfor %}
{% else %}
<tr>
<td>
<p><strong>{{ my_variable }}</strong></p>
</td>
</tr>
{% endif %}
</tbody>
</table>
当my_variable是一个字符串(即不是映射)时,这很好。但是当它是一个映射时,我没有得到一个很好的渲染表:
<table class="summary_table halfpage">
<thead>
<tr>
<th>
My Table
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<p><strong> Column1 Column2
0 6th name 1
1 7th name2
2 8th name 2
3 9th name 4</strong></p>
</td>
</tr>
</tbody>
</table>
这是生成该模板变量的python代码,并将其加载到模板中:
def load_data(data_name, grade=None):
file_path = os.path.join(data_path, for_who + "_" + when + "_" + data_name + ".csv")
if not os.path.isfile(file_path):
file_path = False
if file_path:
data = pd.read_csv(file_path)
else:
data = False
return data
def make_my_variable():
data = load_data("relevant_data")
if not isinstance(data, pd.DataFrame):
data = load_data("other_relevent_data")
#in the case of "other_relevent_data" the column names are different
data = data["ColumnA"].iloc[0]
return data
report = report_tmpl.render(
my_variable = make_my_variable()
)
html_output = os.path.join(output_path, for_who + "_" + date_transform(when) + ".html")
with open(html_output, 'w') as fh:
fh.write(report)
知道我做错了什么吗?如果没有他有条件的话,这会很好。
编辑:添加了创建my_variable并呈现模板的python代码
答案 0 :(得分:1)
感谢@moooeeeep我能够解决这个问题。正如他所提到的,问题来自于pd.DataFrame未被识别为jinja2中的映射。所以,我将数据框转换为列表:
def make_my_variable():
data = load_data("relevant_data")
if not isinstance(data, pd.DataFrame):
data = load_data("other_relevent_data")
#in the case of "other_relevent_data" the column names are different
data = data["ColumnA"].iloc[0]
else:
data = [(v["Column1"], v["Column2"]) for k,v in data .iterrows()]
return data
在模板方面,我更改了条件来自:
{% if my_variable is mapping %}
{% for key,value in my_variable.iterrows() %}
#and latter...
<p><strong>{{ value['Column1'] }} grade:</strong> {{ value['Column2'] }}</p>
以强>
{% if most_popular_games is iterable and my_variable is not string %}
{% for value in my_variable %}
#and..
<p><strong>{{ value[0] }} grade:</strong> {{ value[1] }}</p>
总而言之,最终的模板代码现在看起来像这样:
<table class="summary_table halfpage">
<thead>
<tr>
<th>
My Table
</th>
</tr>
</thead>
<tbody>
{% if my_variable is iterable and my_variable is not string %}
{% for value in my_variable %}
<tr>
<td>
<p><strong>{{ value[0] }} : </strong> {{ value[1] }}</p>
</td>
</tr>
{% endfor %}
{% else %}
<tr>
<td>
<p><strong>{{ my_variable }}</strong></p>
</td>
</tr>
{% endif %}
</tbody>
</table>