4
"""Example of static use of Google Visualization Python API."""
2
# -*- coding: utf-8 -*-
4
# Copyright (C) 2009 <David Planella> <david.planella@ubuntu.com>
5
#This program is free software: you can redistribute it and/or modify it
6
#under the terms of the GNU General Public License version 3, as published
7
#by the Free Software Foundation.
9
#This program is distributed in the hope that it will be useful, but
10
#WITHOUT ANY WARRANTY; without even the implied warranties of
11
#MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
12
#PURPOSE. See the GNU General Public License for more details.
14
#You should have received a copy of the GNU General Public License along
15
#with this program. If not, see <http://www.gnu.org/licenses/>.
18
from gviz_api import gviz_api
21
from LocaleInfo import LocaleInfo
22
from datetime import datetime
25
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
26
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
27
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
29
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
12
30
<title>Language packs</title>
13
31
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
32
<script type="text/javascript">
16
34
google.load("visualization", "1", {packages:["table"]});
17
35
google.setOnLoadCallback(drawTable);
19
google.load("visualization", "1", {packages:["barchart"]});
37
google.load("visualization", "1", {packages:["columnchart"]});
20
38
google.setOnLoadCallback(draw_chart);
22
40
function drawTable() {
23
var langpack_data = new google.visualization.DataTable(%(langpacks_json)s, 0.5);
24
var langpack_table = new google.visualization.Table(document.getElementById('table_div_langpack'));
25
langpack_table.draw(langpack_data, {showRowNumber: true});
41
var langpack_table_data = new google.visualization.DataTable(%(langpacks_json)s, 0.6);
42
var langpack_table = new google.visualization.Table(
43
document.getElementById('table_div_langpack'));
45
langpack_table.draw(langpack_table_data, {
28
51
function draw_chart() {
29
var json_data = new google.visualization.DataTable(%(langpacks_json)s, 0.5);
30
var chart = new google.visualization.BarChart(
31
document.getElementById('bar_chart_div_langpack'));
52
var langpack_chart_data = new google.visualization.DataTable(%(langpacks_top_json)s, 0.6);
53
var langpack_chart = new google.visualization.ColumnChart(
54
document.getElementById('column_chart_div_langpack'));
33
chart.draw(json_data, {
36
title: 'Language packs by installation'
56
langpack_chart.draw(langpack_chart_data, {
59
title: 'Language packs by installation',
64
<style type="text/css">
42
<H1>Language packs chart</H1>
43
<div id="bar_chart_div_langpack"></div>
45
<H1>Language packs table</H1>
46
<div id="table_div_langpack"></div>
79
<div id="report-meta">
80
<p>Generated on %(gen_date)s</p>
83
<h1>Top 20 language packs</h1>
84
<div id="column_chart_div_langpack" align="center"></div>
86
<h1>Language pack installations</h1>
87
<div id="table_div_langpack" align="center"></div>
51
def read_csv(description, entries=-1):
52
f = open('langpacks_by_inst', 'r')
92
def csv2json(reader, keys):
54
reader = csv.reader(f)
55
keys = ("langpack", "installs")
56
#keys = tuple(description.keys())
66
103
data_json[key] = row.next()
67
104
data += [data_json]
72
109
# Creating the data
110
keys = ("langpack", "installs")
73
111
description = {"langpack": ("string", "Language pack"),
74
112
"installs": ("number", "Installations")}
76
data = read_csv(description, 15)
78
# Loading it into gviz_api.DataTable
114
# Read the data from the CSV file
115
f = open('/home/dpm/langpacks-by-inst/data/langpacks_by_inst.csv', 'r')
116
reader = csv.reader(f)
118
# Convert the rows read to a list and translate the ISO codes to language
120
reader_list = list(reader)
121
li = LocaleInfo.LocaleInfo()
123
for row in reader_list:
125
lang_name = li.translate(reader_list[i][0], True)
126
reader_list[i][0] = lang_name.split(';')[0]
131
# For the graph we only want to visualise the top 50 language packs, excluding English
132
reader_top_list = reader_list[1:21]
134
data = csv2json(reader_list, keys)
135
data_top = csv2json(reader_top_list, keys)
138
# Load the data it into gviz_api.DataTable
79
139
data_table = gviz_api.DataTable(description)
80
140
data_table.LoadData(data)
82
# Creating a JSon string
142
data_top_table = gviz_api.DataTable(description)
143
data_top_table.LoadData(data_top)
145
# Create a JSon string
83
146
langpacks_json = data_table.ToJSon(columns_order=("langpack", "installs"),
84
147
order_by=("installs", "desc"))
86
# Putting the JS code and JSon string into the template
149
langpacks_top_json = data_top_table.ToJSon(columns_order=("langpack", "installs"),
150
order_by=("installs", "desc"))
152
# Log the date of creation for the report
153
gen_date = datetime.utcnow()
155
# Put the JSon string into the template
87
156
print page_template % vars()
89
158
if __name__ == "__main__":