将Cherrypy格式的数据传递到Javascript前端图表
我是一名软件工程师,但我从未做过任何网络开发 这一次,我负责制作一个仪表板来分析一些性能统计数据 为了完成这项任务,我首先尝试从头开始学习Django,但结果只浪费了几个星期 我开始使用cherrypy构建一些小应用程序,下面是我迄今为止编写的代码。这是一些Ajax代码和一些MySQL查询代码的大杂烩 特别是,函数将Cherrypy格式的数据传递到Javascript前端图表,javascript,python,cherrypy,Javascript,Python,Cherrypy,我是一名软件工程师,但我从未做过任何网络开发 这一次,我负责制作一个仪表板来分析一些性能统计数据 为了完成这项任务,我首先尝试从头开始学习Django,但结果只浪费了几个星期 我开始使用cherrypy构建一些小应用程序,下面是我迄今为止编写的代码。这是一些Ajax代码和一些MySQL查询代码的大杂烩 特别是,函数submit222()与本项目关系不大 在下面的代码中,成功地建立了到数据库的连接和查询 res包含多个带有时间戳(x轴)和请求数(y轴)的行 我想在res中传递这些数据,以便在网页中
submit222()
与本项目关系不大
在下面的代码中,成功地建立了到数据库的连接和查询
res
包含多个带有时间戳(x轴)和请求数(y轴)的行
我想在res
中传递这些数据,以便在网页中显示时间戳和请求数的图表
使用Google chart()的图表
我现在没有工作视图文件,也找不到解决此问题的适当示例
谁能为视图编写一个简单的Javascript,从python代码中获取res
,并构建一个图表(或者如果这太多,谁能解释如何将res
传递给Javascript)
我真的试着自己解决这个问题,没有任何人的帮助,我想我无法解决这个问题
多谢各位
MEDIA_DIR = os.path.join(os.path.abspath("."), u"media")
def connect(thread_index):
# Create a connection and store it in the current thread
cherrypy.thread_data.db = MySQLdb.connect(some db, some id, some password, 'storm')
# Tell CherryPy to call "connect" for each thread, when it starts up
cherrypy.engine.subscribe('start_thread', connect)
class AjaxApp(object):
@cherrypy.expose
def index(self):
# Sample page that displays the number of records in "table"
# Open a cursor, using the DB connection for the current thread
c = cherrypy.thread_data.db.cursor()
query = """select refresh_time,
num_requests
from model group by refresh_time"""
c.execute(query)
res = c.fetchall()
c.close()
return open(os.path.join(MEDIA_DIR, u'index.html'))
@cherrypy.expose
def submit222(self, name):
cherrypy.response.headers['Content-Type'] = 'application/json'
return simplejson.dumps(dict(title="Hello, %s" % name))
config = {'/media':
{'tools.staticdir.on': True,
'tools.staticdir.dir': MEDIA_DIR,
}
}
def open_page():
webbrowser.open("http://127.0.0.1:8080/")
cherrypy.engine.subscribe('start', open_page)
cherrypy.tree.mount(AjaxApp(), '/', config=config)
cherrypy.engine.start()
如果这不是你想要的,请告诉我。开始web编程可能很难概念化客户端和服务器端将要发生的事情。坚持住
import cherrypy
import os
import json
MEDIA_DIR = os.path.join(os.path.abspath("."), "media")
class AjaxApp(object):
@cherrypy.expose
def index(self):
# Sample page that displays the number of records in "table"
# Open a cursor, using the DB connection for the current thread
return """
<html>
<head>
<script lang="javascript">
function GetData()
{
// code for IE7+, Firefox, Chrome, Opera, Safari
if(window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest();
else// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
var someData_notJSON = JSON.parse(xmlhttp.responseText);
document.getElementById('Data').innerHTML = 'asdf: ' + someData_notJSON.asdf + ' and asdfw: ' + someData_notJSON.asdfw;
}
}
xmlhttp.open("GET","/submit222", true);
xmlhttp.send();
}
</script>
</head>
<body onload="GetData();">
<div id="Data">hi</div>
</body>
</html>
"""
@cherrypy.expose
def submit222(self):
# get data from db
res = { 'asdf': 1,
'asdfw' : 3 }
return json.dumps(res)
config = {'/media':
{'tools.staticdir.on': True,
'tools.staticdir.dir': MEDIA_DIR,
}
}
cherrypy.tree.mount(AjaxApp(), '/', config=config)
cherrypy.engine.start()
import cherrypy
导入操作系统
导入json
MEDIA\u DIR=os.path.join(os.path.abspath(“.”,“MEDIA”)
AjaxApp类(对象):
@樱桃树
def索引(自):
#显示“表”中记录数的示例页
#使用当前线程的DB连接打开游标
返回“”
函数GetData()
{
//IE7+、Firefox、Chrome、Opera、Safari的代码
if(window.XMLHttpRequest)
xmlhttp=新的XMLHttpRequest();
else//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
var someData_notJSON=JSON.parse(xmlhttp.responseText);
document.getElementById('Data').innerHTML='asdf:'+someData\u notJSON.asdf+'和asdfw:'+someData\u notJSON.asdfw;
}
}
open(“GET”,“/submit222”,true);
xmlhttp.send();
}
你好
"""
@樱桃树
def submit222(自我):
#从数据库获取数据
res={'asdf':1,
'asdfw':3}
返回json.dumps(res)
配置={'/media':
{'tools.staticdir.on':True,
'tools.staticdir.dir':媒体目录,
}
}
mount(AjaxApp(),“/”,config=config)
cherrypy.engine.start()
希望这有帮助。让我直说吧。。。您想使用ajax将ress传递到前端吗?这是正确的
res
是一个二维数据结构,有两列(timestamp和num_request),我想在前端用这些数据绘制一个图表。