testtmpl.py 970 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import jinja2
  2. import codecs
  3. import os
  4. import dataset
  5. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/yodb?charset=utf8mb4')
  6. fname=os.path.abspath(__file__)
  7. elmts=fname.split(os.path.sep)
  8. path2=os.path.sep.join(elmts[0:-1])
  9. keysdir=path2+os.path.sep
  10. print(keysdir)
  11. templateLoader = jinja2.FileSystemLoader(searchpath=keysdir)
  12. templateEnv = jinja2.Environment(loader=templateLoader)
  13. TEMPLATE_FILE = 'test.tmpl'
  14. template = templateEnv.get_template(TEMPLATE_FILE)
  15. #SELECT distinct ts_word FROM trending_searches ORDER BY ts_date DESC limit 21;
  16. cursor=db.query('SELECT distinct ts_word FROM trending_searches ORDER BY ts_date DESC limit 21;')
  17. display={}
  18. idx=1
  19. for c in cursor:
  20. display['a'+str(idx)]=c['ts_word']
  21. idx+=1
  22. display['client']='1'
  23. display['email']='jeweiliang@gmail.com'
  24. outputText = template.render(display=display)
  25. fw=codecs.open('c:/tmp/final.html','w','utf-8')
  26. fw.write(outputText)
  27. fw.close()
  28. os.startfile('c:/tmp/final.html')