CoCalc Shared Fileswww / cgi-bin / mfd / mfe_data_table.pyOpen in CoCalc with one click!
Author: William A. Stein
1
import constants, mfe_source
2
3
def Data_Table_Heading():
4
s = "<table bgcolor=%s cellspacing=2 cellpadding=4 border=0>\n"%constants.TABLE_BORDER_COLOR
5
s=s+Fill_Row("<b>Field</b>", "<b>Data</b>", "<b>Notes</b>", \
6
"<b>Source</b>", "center", constants.TABLE_HEADING_COLOR)
7
return s
8
9
def Fill_Row(e1,e2,e3,e4,align="left",color=constants.TABLE_BGCOLOR):
10
return '<tr align=%s> <td bgcolor="%s" width=200>%s&nbsp</td>\
11
<td bgcolor="%s" width=250>%s&nbsp</td><td bgcolor="%s" width=200>\
12
%s&nbsp</td><td bgcolor="%s" width=100>%s&nbsp</td>\
13
</tr>\n'%(align,color,e1,color,e2,color,e3,color,e4)
14
15
def Generate_Row(M, heading, table, data, notes="", table_source=""):
16
if table_source=="":
17
table_source=table
18
if data == constants.not_computed or (type(data) == type("") and (data == "?" or data=="")):
19
return ""
20
if table_source.upper() == "NA":
21
source =""
22
else:
23
source = mfe_source.Link(M.source(table_source))
24
return Fill_Row(heading,data,notes,source,"left")
25
26
def Sort_By_Column(data, column):
27
col = []
28
for i in range(len(data)):
29
col.append([data[i][column], i])
30
col.sort()
31
data2 = []
32
for i in range(len(data)):
33
data2.append(data[col[i][1]])
34
return data2
35
36
37
def Generate_Data_Table(table, sort=1):
38
s = "<table border=1><tr><td>"
39
s = s+Data_Table_Heading()
40
41
if sort:
42
table = Sort_By_Column(table, 1)
43
44
i=0
45
for x in table:
46
if i>3:
47
break
48
if len(x) <= 6 or x[6]:
49
s = s + Generate_Row(x[0],x[1],x[2],x[3],x[4],x[5])
50
s =s+"</table></td></tr></table>"
51
return s
52