4

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

2 Answers 2

8

Yes, you can do this using Django. All you need to do is to create a Django PyDev(python) application. In the settings.py file give 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 give mysql connection as,

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

Using cursor retrieve the data from table and then convert into 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.

Sign up to request clarification or add additional context in comments.

3 Comments

is dictfetchall() a python function??
no, I have given the definition in the code now..please check
is setting the db parameters necessary in settings.py? Seems like you can just connect to your db tables in the views.py.
1

You Can connect to MySql DB from views.py and query for data and then pass it to the HTMLpage in the required format(maybe JSON, CSV, or XML).From the page call the d3js script and use the passed object for data

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.