Python 在Flask上将数据帧呈现为html

Python 在Flask上将数据帧呈现为html,python,pandas,flask,jinja2,Python,Pandas,Flask,Jinja2,有谁能给我一些提示或链接来研究如何通过flask应用程序在web浏览器中修复df.descripe() 例如,如果我在IPython中打印(df.descripe()),这将以一种很好的格式出现: kW count 28219.000000 mean 134.723654 std 46.849081 min 24.300000 25% 91.300000 50% 135.900000 75% 168.600000 max

有谁能给我一些提示或链接来研究如何通过flask应用程序在web浏览器中修复
df.descripe()

例如,如果我在IPython中打印(df.descripe()),这将以一种很好的格式出现:

     kW
count  28219.000000
mean     134.723654
std       46.849081
min       24.300000
25%       91.300000
50%      135.900000
75%      168.600000
max      313.900000
但如果使用渲染模板尝试此操作,请将数据作为字符串传递:

maxStatsStr = str(df.describe())

resp = make_response(render_template('table.html',
maxStatsStr=maxStatsStr))

return resp
使用Jinja语法的前端HTML文件:

<p>{{maxStatsStr}}</p>
更好的方法是像表一样创建,并使用带有Jinja的for循环来显示数据吗?在后端,我只是不知道如何准备一个
df.descripe()
以表的形式呈现,如下所示:

{% for table in tables %}
            {{ table|safe }}
{% endfor %}
使用的最终代码

statsInfoStr = df.describe().to_html()

resp = make_response(render_template('table.html',
maxDateStr=maxDateStr,

tables=[statsInfoStr], titles=df.describe().T))
return resp
table.html
jinja循环数据:

        <h2>Summary Statistics</h2>
        {% for table in tables %}
                    {{ table|safe }}
        {% endfor %}

    </body>
</html>
摘要统计信息
{对于表%%中的表,%}
{{表|安全}}
{%endfor%}

首先,可以利用该方法将帧转换为html

在转换为html之前,您甚至可以使用

然后,您可能想改用该函数

e、 g:


你已经调查过了吗?如果你想把它装饰一下,你可以看看“谢谢”。这可以使用
df.descripe().to_html(class='data')
但是如何从
df.descripb()
中生成列名,例如
count mean,std,min,25%,50%,75%,max
?理想情况下,您需要在转换之前操纵数据帧结构。在您的情况下,可能是
df.descripe().T
?如果你对I/O有更具体的了解,它会有所帮助。谢谢你的帮助,我想我可以从这里得到:)还有一个新问题,我发布了有效的代码。我最终使用了一个表和Jinja来循环数据。是否可以在
resp=make_response
中显示多个表?对不起,我对
Jinja
不太熟悉,但我敢打赌,在渲染之前,您希望加入多个表或格式化多个表。鉴于您的
表=[…]
采用了一个列表,我猜想如果您的模板支持它,它应该支持多个表对象。如果没有,我建议你把它作为一个新的问题与相关的标签,以吸引其他答案。
        <h2>Summary Statistics</h2>
        {% for table in tables %}
                    {{ table|safe }}
        {% endfor %}

    </body>
</html>
def some_flask_func(...):
    # ... your other codes ... #

    # If you just want to get the vanilla structure: 
    df_html = df.describe().to_html()

    # or, if you want to use Styler instead:
    df_html = df.describe().style.apply(some_style_func).render()

    # render directly from string:
    resp = make_response(render_template_string(df_html)
    return resp