Skip to content

Commit ecaa002

Browse files
committed
started adding new files from Chris Meyers
1 parent bdfbd93 commit ecaa002

3 files changed

Lines changed: 186 additions & 0 deletions

File tree

‎README‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ Peter Wentworth and his students - for working on the original Python 3 fork
160160
Brad Miller - for adding pop-up question dialogs to visualizations
161161
David Pritchard - for some enhancements to front-end
162162
Peter Robinson - for v3/make_visualizations.py
163+
Chris Meyers - for custom visualizations such as v3/matrix.py and v3/htmlFrame.py
163164

164165

165166
For advice and feedback from an instructor's perspective:

‎v3/htmlFrame.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# h t m l F r a m e . p y
2+
#
3+
# Chris Meyers. 09/25/2013
4+
#
5+
# Holder for attributes to be applied to a template and
6+
# a simple function to apply attributes to template and
7+
# send it to the <div> for display
8+
9+
from pg_logger import setHTML
10+
11+
dft_template = """
12+
<html><body>
13+
<h3>%(banner)s</h3>
14+
<div>%(item1)s</div>
15+
<div>%(item2)s</div>
16+
<div>%(item3)s</div>
17+
</html></body>
18+
"""
19+
20+
class HtmlFrame :
21+
def __init__ (self, template=dft_template, banner="") :
22+
self.outputOn = True
23+
self.template = template
24+
self.banner = banner
25+
self.item1 = self.item2 = self.item3 = ""
26+
27+
def makeEofPage(self) :
28+
pass
29+
30+
def makeFrame (self,template=None) :
31+
if not template : template = self.template
32+
content = template % self.__dict__
33+
setHTML(content)
34+

‎v3/matrix.py‎

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# m a t r i x . p y
2+
#
3+
# Support for 2d matrix that renders to an HTML table
4+
#
5+
# Chris Meyers. 09/25/2013
6+
#
7+
8+
dftTableAttr = 'cellspacing="0" cellpadding="10"'
9+
10+
class Matrix :
11+
def __init__ (self, nrows=1, ncols=1, data=None,
12+
dftFormat="", dftStyle="", title="",
13+
tableAttr=dftTableAttr, tableHeaders=None,
14+
Expand=True) :
15+
self.nrows = nrows
16+
self.ncols = ncols
17+
self.values = {}
18+
self.expanded = Expand
19+
if Expand :
20+
# get attributes only on the main Matrix
21+
self.dftFormat = dftFormat
22+
self.dftStyle = dftStyle
23+
self.title = title
24+
self.tableAttr = tableAttr
25+
self.tableHeaders = tableHeaders
26+
self.format = Matrix(nrows, ncols, Expand=False)
27+
self.style = Matrix(nrows, ncols, Expand=False)
28+
if data :
29+
if type(data) == type({}) : data=dictToLol(data)
30+
if type(data) == type([]) :
31+
self.populate(data)
32+
33+
def __getitem__(self, coords) :
34+
row, col = coords
35+
return self.values.get((row,col))
36+
37+
def __setitem__(self, coords, value) :
38+
row, col = coords
39+
self.values[(row,col)] = value
40+
self.nrows = max(self.nrows,row+1)
41+
self.ncols = max(self.ncols,col+1)
42+
if self.expanded :
43+
self.format.nrows = self.nrows
44+
self.format.ncols = self.ncols
45+
self.style.nrows = self.nrows
46+
self.style.ncols = self.ncols
47+
return value
48+
49+
#===========================================
50+
51+
def setrowVals(self, row, values) :
52+
"set each column to a seperate value"
53+
col = 0
54+
for col in range(len(values)) :
55+
self.__setitem__((row,col),values[col])
56+
col += 1
57+
58+
def setrowVal(self, row, value) :
59+
"set all columns to the same value"
60+
col = 0
61+
while col < self.ncols :
62+
self.__setitem__((row,col),value)
63+
col += 1
64+
65+
def getrow (self, row) :
66+
vals = []
67+
for c in range(self.ncols) :
68+
vals.append(self.__getitem__( (row,c) ))
69+
return vals
70+
71+
#===========================================
72+
73+
def setcolVals(self, col, values) :
74+
"set each row to a seperate value"
75+
row = 0
76+
for row in range(len(values)) :
77+
self.__setitem__((row,col),values[row])
78+
row += 1
79+
80+
def setcolVal(self, col, value) :
81+
"set all rowumns to the same value"
82+
row = 0
83+
while row < self.nrows :
84+
self.__setitem__((row,col),value)
85+
row += 1
86+
87+
def getcol (self, col) :
88+
vals = []
89+
for r in range(self.nrows) :
90+
vals.append(self.__getitem__( (r,col) ))
91+
return vals
92+
93+
#===========================================
94+
95+
def populate(self, lists) :
96+
"Fill self from a list of lists"
97+
nRows = len(lists)
98+
nCols = max([len(l) for l in lists])
99+
for row in range(len(lists)) :
100+
vals = lists[row]
101+
if type(vals) != list : vals = [vals] # make sing col
102+
self.setrowVals(row, vals)
103+
104+
def renderHtml(self,wrap=None) :
105+
lins = ["","<table %s>" % self.tableAttr]
106+
if self.title : lins[0] = "<div>%s</div>" % self.title
107+
headers = self.tableHeaders
108+
if headers :
109+
lins.append("<tr><th>"+"</th><th>".join(map(str,headers))+
110+
"</th></tr>")
111+
for row in range(self.nrows) :
112+
rowLin = [" <tr>"]
113+
vals = self.getrow(row)
114+
if self.format : formats = self.format.getrow(row)
115+
else : formats = ['']*self.ncols
116+
if self.style : styles = self.style.getrow(row)
117+
else : styles = ['']*self.ncols
118+
for c in range(self.ncols) :
119+
val = vals[c]; style=styles[c]; format=formats[c]
120+
if val == None : val = ""
121+
if not format : format = self.dftFormat
122+
if format :
123+
if type(format)==type("") : val = format % val
124+
else : val = format(val)
125+
if not style : style = self.dftStyle
126+
if style : cell = '<td style="%s">%s</td>' % (style,val)
127+
else : cell = '<td>%s</td>' % val
128+
if wrap and c>0 and c%wrap==0 : cell="</tr><tr>"+cell
129+
rowLin.append(cell)
130+
rowLin.append("</tr>")
131+
lins.append("".join(rowLin))
132+
lins.append("</table>")
133+
return "\n".join(lins)
134+
135+
def __str__ (self) :
136+
return "Matrix-%dx%d" % (self.nrows,self.ncols)
137+
138+
#===========================================
139+
140+
typeSeq = (type([]), type((1,2)))
141+
142+
def dictToLol(dic) :
143+
"Convert dict to a list of lists"
144+
keys = dic.keys(); keys.sort()
145+
lists = []
146+
for key in keys :
147+
val = dic[key]
148+
if type(val) not in typeSeq : val = [val]
149+
lists.append([key]+list(val))
150+
return lists
151+

0 commit comments

Comments
 (0)