How to Parse Python dictionary into google visualization datatable

992 views
Skip to first unread message

Raj

unread,
Jul 17, 2015, 12:54:35 PM7/17/15
to google-visua...@googlegroups.com
hi all,

I am writing a python script that connects to MySQL database and gets data and I store that data as python dictionary. I want to plot a graph between keys and values using Google Visualization api.
Here's the code as well. How to parse the python dictionary into gviz data table?



#!/usr/bin/python
import sys, pkg_resources, imp
import commands
import pyodbc
import MySQLdb
import gviz_api

#Connect to ens-db, database circuits
conn_ensdb = MySQLdb.Connection(db='NHS_db', host='ens-db', user='asdgghh', passwd='ilthnmk')
cursor_ensdb = conn_ensdb.cursor()
cursor_ensdb.execute("SELECT ManagedType,TotalCount from TotalManaged_DC")
rows = cursor_ensdb.fetchall()

#converting tuple output to dictionary format
my_list = [data for data in rows]
dict_with_strs=dict(my_list)                                                                                                                         //Output of dictionary will be:- {'Center': 4, ' Office ': 14, ' Core Data ': 7, 'Managed ': 25}
dict_with_ints=dict((k,int(v)) for k,v in dict_with_strs.iteritems())

print ""

print '''
<html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1.1", {packages:["bar"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([                                                                       //I want to parse this dictionary into data table. Plot Google charts between dict keys vs dict values .How can I pass dict ?
        ]);
        var options = {
          chart: {
            title: 'Company Performance',
          },
          bars: 'horizontal' // Required for Material Bar Charts.
        };
        var chart = new google.charts.Bar(document.getElementById('barchart_material'));
        chart.draw(data, options);
      }
    </script>

mariummalik22

unread,
Jul 24, 2015, 12:29:56 AM7/24/15
to Google Visualization API, preet...@gmail.com
I think you need to give data in JSON format in order to view it. You can view this link for understanding JSON: https://developers.google.com/chart/interactive/docs/php_example

mariummalik22

unread,
Jul 28, 2015, 2:25:55 AM7/28/15
to Google Visualization API, preet...@gmail.com
I have no idea about python, if you could show me your output result of the variable of python, then I could help you writing it in json
Reply all
Reply to author
Forward
0 new messages