栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在现有的flask html表中显示andas数据框?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在现有的flask html表中显示andas数据框?

工作示例:

python代码:

from flask import Flask, request, render_template, session, redirectimport numpy as npimport pandas as 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)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 > <table border="1" >  <thead>    <tr >      <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。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/382298.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号