gpt4 book ai didi

python - 是否可以将两个数据帧发送到我的html( flask )?

转载 作者:行者123 更新时间:2023-12-01 00:14:17 26 4
gpt4 key购买 nike

所以我的 html 页面收到一个 pandas 数据框。

return render_template('example.html', tables=[data_frame.to_html(classes='data')], titles=dataframe.columns.values)

我的 html 页面显示它:

{% for table in tables %}
{{titles[loop.index]}}
{{ table|safe }}
{% endfor %}

我想要做的是在我的 html 页面中显示两个单独的数据框。我怎样才能做到这一点?

例如:如果我有数据帧 df1 和 df2,

在 html 页面中,我想在一些文本之后显示它们。

show df1

<h2> some text </h2>

show df2

最佳答案

由于 render_template 接受字典,因此您可以使用“table”和“title”键将名为 context 或其他名称的 Dict 对象传递给“render_template”函数。然后在 Jinja 中,您可以通过 context.tablecontext.title 访问这些键。

示例:

def my_function(request):
...
get your dataframe
...
context = {"tables":[data_frame.to_html(classes='data')],
"titles" : dataframe.columns.values,
}

return render_template('template.html', context=context)

关于python - 是否可以将两个数据帧发送到我的html( flask )?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59425578/

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