EncyTime02.py
Digerati
#!/usr/bin/python #-*- coding: utf-8 -*- #2019. 11. 4. written by Hyeon Kim import sys import pyodbc import cgi import cgitb noIcon = "<img src='http://dh.aks.ac.kr/VR/style/noicon60.png' height='40'/>" referenceIcon = "<img src='http://dh.aks.ac.kr/VR/style/text60.png' height='40'/>" networkIcon = "<img src='http://dh.aks.ac.kr/VR/style/semantic60.png' height='40'/>" ontologyIcon = "<img src='http://dh.aks.ac.kr/VR/style/ontology60.png' height='40'/>" galleryIcon = "<img src='http://dh.aks.ac.kr/VR/style/gallery60.png' height='40'/>" def sayMessage( errNo ): if( errNo == 1 ): message = 'Database is Not Connected!' icon = 'http://dh.aks.ac.kr/VR/style/notConnected.png' else: if( errNo == 2 ): message = 'Data Set is Not Available!' icon = 'http://dh.aks.ac.kr/VR/style/notAvailable.png' else: message = 'Key Node is Not Found!' icon = 'http://dh.aks.ac.kr/VR/style/notFound.png' print " nodesArray.push({\n" print " id: 'errMessage',\n" print " label: '{0}',\n".format( message ) print " shape: 'image',\n" print " image: '{0}',\n".format( icon ) print " });\n" def displayData( node ): server = 'tcp:digerati.aks.ac.kr' database = 'pcn2019' username = 'guest' password = 'guest' try: db = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password) except: sayMessage( 1 ) return cur = db.cursor() comstring1="select sid, timeLabel, label, class, groupName, encyUrl, infoUrl, iconUrl from ency2019Timeline where indexYear>='" comstring2=node.replace(":", "' and indexYear<='") comstring3="' order by class, indexYear, indexDate, timeLabel" command = comstring1 + comstring2 + comstring3 try: cur.execute(unicode(command, "utf-8")) except: cur.close() db.close() sayMessage( 2 ) return recipe = [] for row in cur.fetchall(): recipe.append( {'sid':row[0], 'timeLabel':row[1], 'label':row[2], 'class':row[3], 'group':row[4], 'encyUrl':row[5], 'infoUrl':row[6], 'iconUrl':row[7]} ) cur.close() db.close() previousClass = None lineCount = 0 for item in recipe: currentClass=item['class'] if( currentClass != previousClass ): if( lineCount > 0 ): print " </table></br>" #print Heading print " <h2><b>§ {0}</b></h2>".format(item['class']) print " <table>" print " <tr><th width='10%%'>Time Span</th><th width='10%%'>Image</th><th width='40%%'>Label<th width='10%%'>Description</th><th width='10%%'>Network</th></tr>" previousClass = currentClass lineCount = lineCount + 1 print " <tr>" #column 1: timeLabel print " <td><b>{0}</b></td>".format(item['timeLabel']) #column 2: image if item['iconUrl'] <> None: print " <td><img src='{0}' height='40'/></td>".format(item['iconUrl']) else: print " <td>{0}</td>".format(noIcon) #column 3: label classification=item['class'] print " <td>【{0}】 <b>{1}</b></td>".format(item['group'], item['label']) #column 4: infoUrl link if item['infoUrl'] <> None: print " <td><a href='{0}'>{1}</a></td>".format(item['infoUrl'], referenceIcon) else: print " <td></td>" #column 6: Term Network Link if item['encyUrl'] <> None: print " <td><a href='{0}'>{1}</a></td>".format(item['encyUrl'], networkIcon) else: print " <td></td>" print " </tr>" #close Table print " </table>" def displayResults(template, node): print("Content-type: text/html") print f = open(template) while 1: line = f.readline() if not line: break if( '#YourData' == line.strip()): displayData( node ) else: print line f.close() def main(): reload(sys) sys.setdefaultencoding('utf8') cgitb.enable() try: node = sys.argv[1] except: node = "1591:1600" displayResults("templateTbs.htm", node) main()