如何在现有的烧瓶html表中显示pandas数据框?

发布于 2021-01-29 18:34:58

这听起来可能是个菜鸟问题,但由于Python不是我最好的语言之一,所以我坚持使用它。

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

py

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

file = r'C:\Users\myuser\Desktop\Test.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>
关注者
0
被浏览
229
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    工作示例:

    python代码:

    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>
    

    否则使用

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

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

    如果您检查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>
    

    如您所见,表格html中有tbody和thead。这样您就可以轻松应用CSS。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看