gpt4 book ai didi

python - Jinja2:使用 pandas 数据框或字符串变量

转载 作者:太空宇宙 更新时间:2023-11-03 15:04:52 25 4
gpt4 key购买 nike

我从 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 代码

最佳答案

感谢@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>

关于python - Jinja2:使用 pandas 数据框或字符串变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44761753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com