Welcome, guest | Sign In | My Account | Store | Cart

A class that makes generating Table easier (based from Gmail' sidebars) ... screenshots on... http://fedmich.googlepages.com/FMLPython.gif

Features: Collapsable mode Color Edge (Blue/Green) Highlight / Select rows

Note: some images are used (triangle, edges)... download it from http://fedmich.googlepages.com/FMLimages.zip

Extract the files on this format... \bin\<b>test.py</b> \bin\<b>FML.py</b> \images\triangle.gif \images\edges

Save the two files using it's filename and run Test.py on your python CGI webserver.

Python, 298 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
-----------------  Test.py  -----------------
from FML import Table

print "Content-Type: text/html\n\n"

T = Table()
T.AddHeader("Head 1", "Head 2")

T.TableName = "Fedmich..."
T.Collapsable = 1

T.AddRow("1", "<i>2</i>")
T.AddRow("3", "<b>4</b>")
T.AddRow("5", "6")

print T.result()


print "<br>"
T.TableName = "Fedmich 2..."
T.headers = []
T.AddHeader("HTop", "HTop2")
T.AddHeader2("Head_Below", "Head_Below 2")
T.ClearRows()
T.Color = "Green"

for i in range(1, 20):
    T.AddRow(i, "... | width=400")  #Custom Attribute...

T.Highlight(6)

T.Select(10)

print T.result()


-----------------  FML.py  -----------------
##  FML which means - FedHTML (python version)
##  by  Fedmich
##  version 1.1
##  Last modified:  10:57 AM 5/19/2006

## Configurations...
src1 = "/images/triangle_open.gif"
src2 = "/images/triangle_closed.gif"
imgfol = "/images/"
                

from glob import fnmatch
Like = fnmatch.fnmatch

TableGenerated = 0

#used for script & styles inclusions, 1 occurence only.

global _TS_OK, _SC_Toggle
_TS_OK = 0      ##  TableStyle
_SC_Toggle = 0  ##ScriptToggle

def InLocal(objName):
    return len([elem for elem in locals() if elem.lower() == objName.lower() ])
def InGlobals(objName):
    return len([elem for elem in globals() if elem.lower() == objName.lower() ])

def iif(condition,resultiftrue,resultiffalse):
    if condition:return resultiftrue
    else: return resultiffalse
    
def GetQuery(QueryString, BlankIfMissing =0 ):
    if not InGlobals("FormFieldStorage"):
        from cgi import FieldStorage
        global FormFieldStorage
        FormFieldStorage = FieldStorage()

    if FormFieldStorage.has_key(QueryString):
        retVal = FormFieldStorage[QueryString].value
    else:
        retVal = iif(BlankIfMissing,"",None)

    return retVal
    
def CheckAttrib(HTML):
    sHTML = str(HTML)
    if Like(sHTML, "* | *"):
        sp = sHTML.split(" | ")
        ActualText = sp[0]
        ExtraAttrb = sp[1]
        return (ActualText,ExtraAttrb)
    else:
        return (sHTML,"")
    
class Table:
    def __init__(self, TableAttr = ""):
        self.datas = []
        self.headers = []
        self.headers2 = []
        self.rowcount = 0
        self.selected = []
        self.highlighted = []
        self.TableName = ""
        self.TableAttrib = TableAttr
        self.Collapsable = 0
        self.Collapsed = 0
        self.Color = "blue"

    def ClearRows(self):
        self.datas = []
        self.rowcount = 0
        
    def AddRow(self, *rowdatas):
        self.datas.append(rowdatas)
        self.rowcount = len(self.datas)

    def AddHeader(self, *Headers):
        self.headers.append(Headers)
    def AddHeader2(self, *Headers):
        self.headers2.append(Headers)

    def Select(self, index):
        li = self.selected
        if index not in li:
            li.append(index)
        
    def Highlight(self, index):
        li = self.highlighted
        if index not in li:
            li.append(index)
            
    def result(self):
        global _TS_OK
        if not _TS_OK:
            TableStyle = """
                <STYLE>
                .Header{
                    background-color: #C3D9FF;
                }        
                .Normal{
                    background-color: #E8EEF7;
                }
                .Selected{
                    font-weight: bold;
                    background-color: #FFFFCC 
                }
                .Highlight {
                    background-color : #FFFFFF;
                    font-weight: bold;
                }
                .MousePointed{
                    background-color: #FFFFFF;
                }
                </STYLE>"""
            print TableStyle
            _TS_OK = 1
        
        output = ""
        if self.TableName <> "":
            if self.Collapsable:
                global _SC_Toggle
                if not _SC_Toggle:
                    ScriptToggle = """<script language="javascript">
                    function ToggleSpan(oSpan, oSpanImg){
                            if (oSpan == null)
                                return false
                                
                            if (oSpan.style.display == "none")
                                oSpan.style.display = "block"
                            else
                                oSpan.style.display = "none"
                            
                            if (oSpanImg != null){
                                src1 = "%s"
                                src2 = "%s"
                                
                                if (oSpanImg.src.match('open'))
                                    oSpanImg.src = src2
                                else
                                    oSpanImg.src = src1
                            }
                        }
                    </script>""" % ( src1, src2)
                    _SC_Toggle = 1
                    print ScriptToggle
                
                global TableGenerated
                TableGenerated += 1
                TableId = TableGenerated
                
                if self.Collapsed:
                    srcIMG = src2
                else:
                    srcIMG = src1
                
                IMGTria = "<img id='TableTria%s' src='%s' border=0 width=11 height=11>" % (TableId, srcIMG)
                output += """<a href='#FedTable' STYLE="text-decoration:none"
                            onclick="hasfunc=0; try{
                            ToggleSpan(document.getElementById('Table%s'),
                                       document.getElementById('TableTria%s'))}
                                       catch(e){} ;
                            ">%s
                            """ % (TableId, TableId, IMGTria)
                            
            output += "<b>%s</b>" % self.TableName

            if self.Collapsable:
                spanX = ""
                if self.Collapsed:
                    spanX = "style='display:None'"
                output += "</a><span id='Table%s' %s>" % (TableId, spanX)
        
        if self.TableAttrib <> "":
            output += "<table %s>\n" % self.TableAttrib 
        else:
            output += "<table border=0 cellspacing=1 celpadding=0>\n"

        HeaderCount = 0

        for hCounter in range(0,2):
            if hCounter==0:
                Headers = self.headers
            else:
                Headers = self.headers2
                
            if len(Headers):
                output += "<TR>\n"
                if type(Headers) is list:
                    for head in Headers:
                        for h in head:
                            (hName, ExtraAttrb) = CheckAttrib(h)
                            
                            output += "<th class='%s' %s>%s</th>\n" % ("Header", ExtraAttrb, hName)
                else:
                    output += "<th>%s</th>\n" % Headers

                if hCounter==0:
                    HeaderCount = 1
                        
                output += "</TR>\n"

        selcount = len(self.selected)
        highcount = len(self.highlighted)
        for i in range(0, len(self.datas)):
            row = self.datas[i]
            ClassName = "Normal"
            
            if (i +1) in self.selected:
                ClassName = "Selected"
            if (i +1) in self.highlighted:
                ClassName = "Highlight"

            (tdName, ExtraAttrb) = ("","")
            finTD = 0        
            output += """<tr class='%s'
                    onMouseOver="this.className='MousePointed'" onMouseOut="this.className='%s'">\n""" % (ClassName,ClassName)
            lRow = len(row)
            for td in row:
                li_tds = td
                if not type(td) is list:
                    li_tds = [td]

                for tds in li_tds:
                    (tdName, ExtraAttrb) = CheckAttrib(tds)
                    output += "<td %s>%s</td>\n" % (ExtraAttrb, tdName)
            
            output += "</tr>\n"
            
        output += "</table>\n"

        if self.Collapsable:
            output += "</span>"
            output = RoundEdge(output, self.Color)
        
        return output

def RoundEdge(contents, color="green"):
    result = """
    <table cellspacing="0" cellpadding="0" border="0">
      <tr>
        <td width=8 height=8 style="background:url(<imgfol>edges/<col>_e1.gif)">
        <img src="<imgfol>edges/spacer.gif" width=8 height=8 alt="" /></td>
        <td width="100%%" height=8 style="background:url(<imgfol>edges/<col>_e2.gif)"><img src="<imgfol>edges/spacer.gif" width=1 height=8 alt="" /></td>
        <td width=8 height=8 style="background:url(<imgfol>edges/<col>_e3.gif)"><img src="<imgfol>edges/spacer.gif" width=8 height=8 alt="" /></td>
      </tr>
      <tr>
        <td width=8 style="background:url(<imgfol>edges/<col>_e4.gif)"><img src="<imgfol>edges/spacer.gif" width=8 height=1 alt="" /></td>
        <td>%s</td>
        <td width=8 style="background:url(<imgfol>edges/<col>_e5.gif)"><img src="<imgfol>edges/spacer.gif" width=8 height=1 alt="" /></td>
      </tr>
      <tr>
        <td width=8 height=8 style="background:url(<imgfol>edges/<col>_e6.gif)"><img src="<imgfol>edges/spacer.gif" width=8 height=8 alt="" /></td>
        <td height=8 style="background:url(<imgfol>edges/<col>_e7.gif)"><img src="<imgfol>edges/spacer.gif" width=1 height=8 alt="" /></td>
        <td width=8 height=8 style="background:url(<imgfol>edges/<col>_e8.gif)"><img src="<imgfol>edges/spacer.gif" width=8 height=8 alt="" /></td>
      </tr> 
    </table>
    """
    result = result.replace("<imgfol>", imgfol)
    result = result.replace("<col>", color)
    result = result % contents
    return result
Created by Federico Garcia Jr. on Fri, 19 May 2006 (PSF)
Python recipes (4591)
Federico Garcia Jr.'s recipes (2)

Required Modules

  • (none specified)

Other Information and Tasks