Sre*_*ith 4 mysql django d3.js
我在MySQL数据库中有我的数据,我需要将d3.js中的数据可视化为气泡图.我有可能在Django框架中这样做,如果是这样的话怎么样?
是的,你可以使用Django来做到这一点.您需要做的就是创建一个Django PyDev(python)应用程序.在settings.py文件中,将数据库作为,
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'myDB', # your mysql database name
'USER': '123', # your mysql user for the database
'PASSWORD': '123', # password for user
'HOST': '127.0.0.1',
'PORT': '3306',
}
Run Code Online (Sandbox Code Playgroud)
在views.py中的函数定义中给出mysql连接,
conn = MySQLdb.connect (host = "127.0.0.1",
user = "root", # mysql root
passwd = "root", # mysql root password
db = "myDB")
Run Code Online (Sandbox Code Playgroud)
使用游标从表中检索数据然后转换为json,
cursor.execute ("select <column> from <table>")
rows=dictfetchall(cursor)
object_list = []
for row in rows:
d = collections.defaultdict()
d['name'] = row['name']
object_list.append(d)
j = json.dumps(object_list)
objects_file = 'path of json file to be created'
m = open(objects_file,'w')
print >> m,j #to write to file
conn.close()
def dictfetchall(cursor):
"Returns all rows from a cursor as a dictionary"
desc = cursor.description
return [
dict(zip([col[0] for col in desc], row))
for row in cursor.fetchall()
]
Run Code Online (Sandbox Code Playgroud)
现在您可以使用此json文件来创建任何类型的d3js.
归档时间: |
|
查看次数: |
5999 次 |
最近记录: |