如何将 Pandas 数据框显示到现有的 Flask html 表中? [英] How to show a pandas dataframe into a existing flask html table?

查看:19
本文介绍了如何将 Pandas 数据框显示到现有的 Flask html 表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来可能是个菜鸟问题,但我坚持下去,因为 Python 不是我最好的语言之一.

我有一个 html 页面,里面有一个表格,我想在其中显示一个 Pandas 数据框.最好的方法是什么?使用pandasdataframe.to_html?

py

from flask import Flask;导入熊猫作为 pd;从熊猫导入数据帧,read_csv;文件 = r'C:UsersmyuserDesktopTest.csv'df = pd.read_csv(文件)df.to_html(header="true", table_id="table")

html

<table id="table"><thead></thead><tr></tr>

解决方案

工作示例:

python 代码:

from flask import Flask、request、render_template、session、redirect将 numpy 导入为 np将熊猫导入为 pdapp = Flask(__name__)df = pd.DataFrame({'A': [0, 1, 2, 3, 4],'B': [5, 6, 7, 8, 9],'C': ['a', 'b', 'c--', 'd', 'e']})@app.route('/', methods=("POST", "GET"))def html_table():return render_template('simple.html', tables=[df.to_html(classes='data')], titles=df.columns.values)如果 __name__ == '__main__':app.run(host='0.0.0.0')

html:

<html lang="zh-cn"><头><meta charset="UTF-8"><title>标题</title><身体>{% for table in table %}{{titles[loop.index]}}{{ 桌子|安全 }}{% 结束为 %}

否则使用

return render_template('simple.html', tables=[df.to_html(classes='data', header="true")])

并从 html 中删除 {{titles[loop.index]}}

如果你检查 html 上的元素

<html lang="en"><head><meta charset="UTF-8"><title>标题</title><body style=""><table border="1" class="dataframe data"><头><tr style="text-align: right;"><th></th><th>A</th><th>B</th><th>C</th></tr></thead><tr>第 0 个第 0 个<td>0</td><td>5</td><td>a</td></tr><tr>第 1 个<td>1</td><td>6</td><td>b</td></tr><tr>第 2 个<td>2</td><td>7</td><td>c--</td></tr><tr>第 3 个<td>3</td><td>8</td><td>d</td></tr><tr>第 4 个<td>4</td><td>9</td><td>e</td></tr></tbody></body></html>

如您所见,它在表格 html 中有 tbody 和 thead.这样你就可以轻松地应用 css.

This may sound a noob question, but I'm stuck with it as Python is not one of my best languages.

I have a html page with a table inside it, and I would like to show a pandas dataframe in it. What is the best way to do it? Use pandasdataframe.to_html?

py

from flask import Flask;
import pandas as pd;
from pandas import DataFrame, read_csv;

file = r'C:UsersmyuserDesktopTest.csv'
df = pd.read_csv(file)
df.to_html(header="true", table_id="table")

html

<div class="table_entrances" style="overflow-x: auto;">

  <table id="table">

    <thead></thead> 
    <tr></tr>

  </table>

</div>

解决方案

working example:

python code:

from flask import Flask, request, render_template, session, redirect
import numpy as np
import pandas as pd


app = Flask(__name__)

df = pd.DataFrame({'A': [0, 1, 2, 3, 4],
                   'B': [5, 6, 7, 8, 9],
                   'C': ['a', 'b', 'c--', 'd', 'e']})


@app.route('/', methods=("POST", "GET"))
def html_table():

    return render_template('simple.html',  tables=[df.to_html(classes='data')], titles=df.columns.values)



if __name__ == '__main__':
    app.run(host='0.0.0.0')

html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

{% for table in tables %}
            {{titles[loop.index]}}
            {{ table|safe }}
{% endfor %}
</body>
</html>

or else use

return render_template('simple.html',  tables=[df.to_html(classes='data', header="true")])

and remove {{titles[loop.index]}} line from html

if you inspect element on html

<html lang="en"><head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body style="">


            <table border="1" class="dataframe data">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>A</th>
      <th>B</th>
      <th>C</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>0</td>
      <td>5</td>
      <td>a</td>
    </tr>
    <tr>
      <th>1</th>
      <td>1</td>
      <td>6</td>
      <td>b</td>
    </tr>
    <tr>
      <th>2</th>
      <td>2</td>
      <td>7</td>
      <td>c--</td>
    </tr>
    <tr>
      <th>3</th>
      <td>3</td>
      <td>8</td>
      <td>d</td>
    </tr>
    <tr>
      <th>4</th>
      <td>4</td>
      <td>9</td>
      <td>e</td>
    </tr>
  </tbody>
</table>


</body></html>

as you can see it has tbody and thead with in table html. so you can easily apply css.

这篇关于如何将 Pandas 数据框显示到现有的 Flask html 表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆