How to visualize mysql data in d3js using django?

I have data in a MySQL database and I need to visualize the data in d3.js as a bubble chart. Is it possible to do this within the framework of Django, and if so, how?

+4
source share
3 answers

Yes, you can do it with Django. All you have to do is create a Django PyDev application (python). In the settings.py file, specify the database as

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', } 

In the function definition in views.py, specify the mysql connection as,

  conn = MySQLdb.connect (host = "127.0.0.1", user = "root", # mysql root passwd = "root", # mysql root password db = "myDB") 

Use the cursor to extract data from the table and then convert to 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() ] 

Now you can use this json file to create any type of d3js.

+8
source

You can connect to MySql DB from views.py and request the data, and then transfer it to the HTML page in the required format (possibly JSON, CSV or XML). From the d3js script page call and use the passed object for data

+1
source

Perhaps you should follow this guide, which indicates exactly what you are looking for:

http://www.d3noob.org/2013/02/using-mysql-database-as-source-of-data.html

Then you can simply integrate it directly on your page using python with django-nvd3:

https://github.com/areski/django-nvd3

0
source

All Articles