Python Pandas Dataframe 在网页上的显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22180993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas Dataframe display on a webpage
提问by tschm
I am using Flask but this probably applies to a lot of similar frameworks.
我正在使用 Flask,但这可能适用于许多类似的框架。
I construct a pandas Dataframe, e.g.
我构建了一个熊猫数据框,例如
@app.route('/analysis/<filename>')
def analysis(filename):
x = pd.DataFrame(np.random.randn(20, 5))
return render_template("analysis.html", name=filename, data=x)
The template analysis.html looks like
模板 analysis.html 看起来像
{% extends "base.html" %}
{% block content %}
<h1>{{name}}</h1>
{{data}}
{% endblock %}
This works but the output looks horrible. It doesn't use linebreaks etc.
I have played with data.to_html()and data.to_string()What's the easiest way to display a frame?
这有效,但输出看起来很糟糕。它不使用换行符等。我玩过data.to_html()和data.to_string()显示帧的最简单方法是什么?
采纳答案by MattDMo
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 documentationfor additional options like CSS styling.
检查文档以获取其他选项,例如 CSS 样式。
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 @SeanVieirafor the tip.
为了告诉 Jinja 你正在传递标记。感谢@SeanVieira的提示。
回答by tschm
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
好的,现在结合我在这里得到的提示,我已经设法获得了一些非常好的结果。在我使用的实际 Python 查看器中
@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
例如,我将完整的数据帧发送到 html 模板。我的 html 模板基于引导程序。因此我可以简单地写
{% 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
引导程序还有许多其他选项,请在此处查看:http: //getbootstrap.com/css/#tables
Base.html is essentially copied from here http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xii-facelift
Base.html 基本上是从这里复制的 http://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
托马斯
回答by frmdstryr
You can use enaml-webto display and interact with pandas dataframes.
A few examples:
几个例子:
- With filtering - https://github.com/frmdstryr/smd-search
- Simple viewer - https://github.com/codelv/enaml-web/tree/master/examples/dataframe_viewer
- 带过滤 - https://github.com/frmdstryr/smd-search
- 简单的查看器 - https://github.com/codelv/enaml-web/tree/master/examples/dataframe_viewer
Note: Interaction (sorting, filtering, etc...) requires a server with websocket support.
注意:交互(排序、过滤等)需要一个支持 websocket 的服务器。
回答by Mark
Iterating over the rows of a df
迭代 df 的行
If you need to have the dfin a format that can iterate over the rows in your html, then use to_dict(orient='records'), which produces a dictin a format:
如果您需要具有df可以迭代 html 中的行的格式,请使用to_dict(orient='records'),它会dict以以下格式生成 a :
‘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:
这样您就可以使用自己的方式在 html 中显示数据。示例代码现在看起来像这样:
Python code using flask
使用烧瓶的 Python 代码
@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
带有 jinja 的 HTML 代码
{% 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 %}

