我有一个mysql查询,它返回一个结果表。 如下:
我想要实现的是在最后一列的值为“MM”时突出显示整行。
我过去曾使用过一小段JS代码,它只适用于单个单元格,而不适用于整行。
我的代码是这样的:
import pandas as pd
from pandas import DataFrame
from pandas.tseries.offsets import BDay
import sys
import mysql.connector
import time
import datetime
from datetime import date,timedelta
import os
## testing dates
currentdate = pd.datetime.today()
today = currentdate.strftime("%Y-%m-%d")
yesterday = currentdate - BDay(1)
yest = yesterday.strftime("%Y-%m-%d")
#print today
#print yest
## where to dump the file
develop_path = "/var/www/html/exDiv/"
report_file = "exDivReport." + today + ".html"
## function to get data and print out
def get_data_from_hk():
cnx = mysql.connector.connect(
user='some_user',
password='some_pass',
host='some_host',
database='some_database'
)
cursor = cnx.cursor()
query = ("SELECT c.description, c.feedcode, date(exdividenddate), dividend, (case when c.metadata like '%3=MM%' then 'MM' when c.metadata is NULL then 'NM' when c.metadata not like '%3=MM%' then 'NM' else c.metadata end) as metadata FROM dividends d join contracts c using(contracttag) where d.kind=0 and d.exdividenddate=getNextTradingDate(1, curdate());")
cursor.execute(query)
dbdata_hk=cursor.fetchall()
cnx.close()
print dbdata_hk
return dbdata_hk
def generate_report():
report_colnames = ['description', 'feedcode', 'date(exdividenddate)', 'dividend', 'metadata']
dbdata_list_hk = get_data_from_hk()
df = pd.DataFrame(dbdata_list_hk, columns=report_colnames)
##test: if the last column contains 'MM' then highlight table row
##highlight = lambda x: '{:,.0f}'.format(x) if x = 'MM' else '{:,.0f}'.format(x)
with open(os.path.join(develop_path,report_file), 'w') as f:
f.write("Stocks with ex-dividend on next trading day
")
f.write(df.to_html(
index=False,
##the whole row needs to be highlighted
##formatters={('description', 'metadata'): highlight},
escape=False,
justify='center'
))
f.write(style_text)
return df
## colouring js
style_text = '''
.market_make {
background-color: #FBB117;
}
var elements = document.getElementsByClassName('market_make');
for(var i=0; i
elements[i].parentElement.setAttribute("bgcolor", "#FBB117");
}
'''
# run the report
d=generate_report()
print d
pandas创建的html部分是这样的:
Stocks with ex-dividend on next trading day
descriptionfeedcodedate(exdividenddate)dividendmetadata
HHI DivHHI2015-07-0234.793976NMCCB9392015-07-020.000000MMnull Combo + Stock - LegsFXI-H2015-07-0235.327749NMCSE10882015-07-020.000000MMPIN13392015-07-020.011800NM除了JS之外,还有更有效的方法吗?
我可以将它与一些CSS结合使用吗?
有什么想法吗?