Как отобразить информационный фрейм pandas в виде таблицы колб-бустрапа?
Я хотел бы показать фрейм данных pandas в виде таблицы boostrap-html с флягой, поэтому я попробовал следующее:
Данные (таблица .csv):
Name Birth Month Origin Age Gender
Carly Jan Uk 10 F
Rachel Sep UK 20 F
Nicky Sep MEX 30 F
Wendy Oct UK 40 F
Judith Nov MEX 39 F
Код Python (python_script.py
):
from flask import *
import pandas as pd
app = Flask(__name__)
@app.route("/tables")
def show_tables():
data = pd.read_csv('table.csv')
data.set_index(['Name'], inplace=True)
data.index.name=None
females = data.loc[data.Gender=='f']
return render_template('view.html',tables=[females.to_html(classes='female')],
titles = ['na', 'Female surfers'])
if __name__ == "__main__":
app.run(debug=True)
templates
каталог (view.html
):
<!doctype html>
<title>Simple tables</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
<div class=page>
<h1>Surfer groups</h1>
{% for table in tables %}
<h2>{{titles[loop.index]}}</h2>
{{ table|safe }}
{% endfor %}
</div>
Бустрапstyle.css
:
body { font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;}
a, h1, h2 { color: #377ba8; }
h1, h2 { margin: 0; }
h1 { border-bottom: 2px solid #eee; }
h2 { font-size: 1.2em; }
table.dataframe, .dataframe th, .dataframe td {
border: none;
border-bottom: 1px solid #C8C8C8;
border-collapse: collapse;
text-align:left;
padding: 10px;
margin-bottom: 40px;
font-size: 0.9em;
}
.male th {
background-color: #add8e6;
color: white;
}
.female th {
background-color: #77dd77;
color: white;
}
tr:nth-child(odd) { background-color:#eee; }
tr:nth-child(even) { background-color:#fff; }
tr:hover { background-color: #ffff99;}
Пока, когда я бегуpython_script.py
:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger pin code: 123-221-151
У меня есть две проблемы: ошибка Not Found и когда я иду вhttp://127.0.0.1:5000/tables
Я получилbuiltins.KeyError KeyError: 'Name'
, Любая идея о том, как показать данные пандас в виде таблицы с пандами и колбу ?.