Pandas Dataframe display on a webpage
The following should work:
@app.route('/analysis/<filename>')def analysis(filename): x = pd.DataFrame(np.random.randn(20, 5)) return render_template("analysis.html", name=filename, data=x.to_html()) # ^^^^^^^^^
Check the documentation for additional options like CSS styling.
Additionally, you need to adjust your template like so:
{% extends "base.html" %}{% block content %}<h1>{{name}}</h1>{{data | safe}}{% endblock %}
in order to tell Jinja you're passing in markup. Thanks to @SeanVieira for the tip.
Ok, I have managed to get some very nice results by now combining the hints I got here. In the actual Python viewer I use
@app.route('/analysis/<filename>')def analysis(filename): x = pd.DataFrame(np.random.randn(20, 5)) return render_template("analysis.html", name=filename, data=x)
e.g. I send the complete dataframe to the html template. My html template is based on bootstrap. Hence I can simply write
{% extends "base.html" %}{% block content %}<h1>{{name}}</h1>{{ data.to_html(classes="table table-striped") | safe}}{% endblock %}
There are numerous other options with bootstrap, check out here:http://getbootstrap.com/css/#tables
Base.html is essentially copied from herehttp://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xii-facelift
The next question is obviously how to plot such a frame. Anyone any experience with Bokeh?
Thank you both to Matt and Sean.
thomas
Iterating over the rows of a df
If you need to have the df
in a format that can iterate over the rows in your html, then use to_dict(orient='records')
, which produces a dict
in a format:
‘records’ : list like [{column -> value}, … , {column -> value}]
That way you can use your own way of displaying the data in your html.The sample code would now look like this:
Python code using flask
@app.route('/analysis/<filename>')def analysis(filename): x = pd.DataFrame(np.random.randn(20, 5)) return render_template("analysis.html", name=filename, data=x.to_dict(orient='records'))
HTML code with jinja
{% extends "base.html" %} {% block content %} <table class="table"> <thead> <tr> <th scope="col">Column name 1</th> <th scope="col">Column name 2</th> <th scope="col">Column name 3</th> </tr> </thead> <tbody> {% for row in data %} <tr> <td>{{row['Column name 1']}}</td> <td>{{row['Column name 2']}}</td> <td>{{row['Column name 2']}}</td> </tr> {% endfor %} </tbody> </table> {% endblock %}