dataframe在html显示,python - How to display pandas dataframe as a table in html? - Stack Overflow

I am using Flask to do this. I essentially want to display my pandas dataframe on my webpage but my webpage ends up displaying this on my html page:

website < phone t email a b l e b o r d e r = " 1 " c l a s s = " d a t a f r a m e " > < t h e a d > < t r s t y l e = " t e x t - a l i g n : r i g h t ; " > < t h > < / t h > < t h > c o m p a n y < / t h > < t h > w e b s i t e < / t h > < t h > p h o n e < / t h > < t h > e m a i l < / t h > < / t r > < / t h e a d > < t b o d y > < / t b o d y > < / t a b l e >

Here is what I tried with Flask:

from backend import final_dataframe

@app.route('/results', methods=["GET", "POST"])

def results():

return render_template('results.html', tables=final_dataframe.to_html(), titles=final_dataframe.columns.values)

And here's a snippet of my html file, for which I used Jinja to display the table:

{% for table in tables %}

{{titles[loop.index]}}

{{ table|safe }}

{% endfor %}

UPDATE:

This is what happens when I do print(final_dataframe.to_html()) in flask:

companywebsitephoneemail

OK so its a problem with my backend.py file. It seems that the global variable does not update:

final_dataframe = pd.DataFrame(columns= ['company', 'website', 'phone', 'email'])

def someFunction(a,b):

contact_info = pd.DataFrame(myData, columns= ['company', 'website', 'phone', 'email'])

global final_dataframe

final_dataframe = final_dataframe.append(contact_info, ignore_index = True)

So basically, when the program gets out of the function, the global variable (final_dataframe) is not updated. It's updated inside the function however.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值