当前位置: 首页 > 面试题库 >

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

阚亮
2023-03-14
问题内容

这听起来可能是个菜鸟问题,但由于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>

问题答案:

工作示例:

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。



 类似资料:
  • 问题内容: 我对Flask和Matplotlib非常陌生。我希望能够显示我在某些html中生成的简单图表,但是我很难弄清楚该怎么做。这是我的Python代码: 这是我的HTML: 问题答案: 您可以在Flask URL路由处理程序中即时生成图像: 然后,您需要将图像包含在HTML模板中:

  • 问题内容: 这听起来可能是个菜鸟问题,但由于Python不是我最好的语言之一,所以我坚持使用它。 我有一个html页面,里面有一个表格,我想在其中显示一个pandas数据框。最好的方法是什么?使用pandasdataframe.to_html? py html 问题答案: 工作示例: python代码: HTML: 否则使用 并从html中删除行 如果你检查html上的元素 如你所见,表格html

  • 我需要从表单中获取数据。 我使用JavaScript创建表单: 然后我需要从输入字段获取数据,其中。 这里是我的视图功能: 但是我得到一个错误: 帮助我从表单中获取数据。

  • 问题内容: 如何设置熊猫数据帧的IPython html显示格式,以便 数字是正确的 数字以逗号作为千位分隔符 大花车没有小数位 据我所知,有设施,我可以这样做: 对于其他数据类型也是如此。 但是在以html显示数据框时,IPython不会选择这些格式选项。我仍然需要 但上面有1,2,3。 编辑: 以下是我针对2和3的解决方案(不确定这是最好的方法),但是我仍然需要弄清楚如何使数字列正确。 问题答

  • 我正试图将数据放入flask中的表中,但由于某种原因,它为每个字符创建了一个新行,而不是仅仅将完整的字符串放入行中。 代码: 表代码:

  • 问题内容: 我试图用一个烧瓶形式的sqlalchemy请求的结果填充选择字段。 这是代码: 这是模板: 查看: 模型(仅粘贴了关联表和作者表,很多列都不用了) 我目前收到此错误: 我真正想要的是,选择字段显示作者姓名及其编号,然后将作者编号返回至应用程序(返回至位于头的名为“ add_author”的函数)。 谢谢。 问题答案: 您有两个问题: 正如肖恩·维埃拉(Sean Vieira)在回答中指