首页 > 解决方案 > 是否可以将两个数据帧发送到我的 html(烧瓶)?

问题描述

所以我的 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

标签: pythonhtmlflask

解决方案


由于render_template接受字典,您可以使用“table”和“title”键将名为 context 或其他任何内容的 Dict 对象传递给“render_template”函数。context.table然后在您的 Jinja 中,您可以通过和访问这些密钥context.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)


推荐阅读