Skip to content

Instantly share code, notes, and snippets.

@bcdejp
Created February 21, 2015 03:51
Show Gist options
  • Save bcdejp/2f784b870c444effe73d to your computer and use it in GitHub Desktop.
Save bcdejp/2f784b870c444effe73d to your computer and use it in GitHub Desktop.
グラフ表示用のCGI
#!/usr/bin/pythonCGI
# -*- coding: utf-8 -*-
from jinja2 import Environment, FileSystemLoader
import MySQLdb
import datetime
def mychart(environ, start_response):
env = Environment(loader=FileSystemLoader('./', encoding='utf8'))
tpl = env.get_template('template.html')
#テンプレートへ挿入するデータの作成
title = u"Tmperature Chart"
temp_list = []
connector = MySQLdb.connect(host="localhost", db="logging", user="user", passwd="passwd", charset="utf8")
cursor = connector.cursor()
#sql = "select * from temperature"
sql = "select * from logging.temperature where DATE_ADD(date, INTERVAL 24 HOUR) > NOW()"
cursor.execute(sql)
records = cursor.fetchall()
for record in records:
temp_list.append({'date':record[0].strftime("%Y-%m-%d %H:%M"), 'temp':record[1]})
cursor.close()
connector.close()
#テンプレートへ挿入するデータの作成
title = u"Tmperature Chart"
#テンプレートへの挿入
html = tpl.render({'title':title, 'temp_list':temp_list})
start_response('200 OK', [('Content-Type', 'text/html')])
return [html.encode('utf-8')]
if __name__ == '__main__':
from flup.server.fcgi import WSGIServer
WSGIServer(mychart).run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment