I would like to show a pandas dataframe as a boostrap-html table with flask, thus I tried the following:
The data (.csv table):
Name Birth Month Origi
The problem here is with the data.csv
file. When you do read_csv()
on it:
In [45]: pd.read_csv("/tmp/a.csv")
Out[45]:
Name Birth Month Origin Age Gender
0 Carly Jan Uk 10 F
1 Rachel Sep UK 20 F
2 Nicky Sep MEX 30 F
3 Wendy Oct UK 40 F
4 Judith Nov MEX 39 F
In [46]: df = pd.read_csv("/tmp/a.csv")
In [47]: df.columns
Out[47]: Index([u'Name Birth Month Origin Age Gender'], dtype='object')
As you can see, there is only one column, instead of four, because it can't understand that 'Birth Month' is supposed to be one column. To fix this, you can open the file and change the first line to:
"Name" "Birth Month" "Origin" "Age" "Gender"
And, then while reading the csv:
In [62]: pd.read_csv("/tmp/a.csv", sep='\s+', quotechar='"')
Out[62]:
Name Birth Month Origin Age Gender
0 Carly Jan Uk 10 F
1 Rachel Sep UK 20 F
2 Nicky Sep MEX 30 F
3 Wendy Oct UK 40 F
4 Judith Nov MEX 39 F
Or you could have also just changed Birth Month
to Birth_Month
For the '404 Not Found' error, the problem is that you have not defined any route for '/'. So, (after editing the header of the csv) I would do something like:
from flask import *
import pandas as pd
app = Flask(__name__)
@app.route("/tables")
def show_tables():
data = pd.read_csv("/tmp/a.csv", sep='\s+', quotechar='"')
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'])
@app.route("/")
def show_home():
return "Hello Guys! Visit: <a href='/tables'> this link </a>"
if __name__ == "__main__":
app.run(debug=True)