Login

Google Charts Templatetags (Python)

Author:
justquick
Posted:
July 1, 2008
Language:
Python
Version:
.96
Score:
0 (after 0 ratings)

The core templatetags for my project google-chartwrapper. It is an easy method of creating dynamic GoogleCharts from the GoogleChartAPI. To get the most recent version:

svn checkout http://google-chartwrapper.googlecode.com/svn/trunk/

and run python setup.py in the downloaded trunk directory. There is an included django project there with the ChartsExamples all worked out in django templates

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from django.template import Library,Node
import GChartWrapper
from django.template import resolve_variable

register = Library()

class AttrNode(Node):
    def __init__(self, args):
        self.args = map(str,args)
    def render(self,context):
        for n,arg in enumerate(self.args):
            if arg in context:
                self.args[n] = resolve_variable(arg, context)
        return self.args
def attribute(parser, token):
    return AttrNode(token.split_contents())
for tag in GChartWrapper.constants.TTAGSATTRS:
    register.tag(tag, attribute)


class ChartNode(Node):
    def __init__(self, tokens, nodelist):
        self.type = None
        self.tokens = []
        if tokens and len(tokens)>1:
            self.type = tokens[1]   
            self.tokens = tokens[2:]
        self.nodelist = nodelist
    def render(self, context): 
        args = []
        kwargs = {}
        for t in self.tokens:
            try:
                args.append(resolve_variable(t,context))
            except:        
                try:
                    args.append(float(t))
                except:
                    arg = str(t)
                    if arg.find('=')>-1:
                        k,v = arg.split('=')[:2]
                        kwargs[k] = v
                    else:
                        args.append(arg)   
        if len(args) == 1 and type(args[0]) in map(type,[[],()]):
            args = args[0]   
        if self.type in GChartWrapper.constants.CLASSES:
            chart = getattr(GChartWrapper,self.type)(args,**kwargs)
        elif self.type in GChartWrapper.constants.TYPES:
            chart = GChartWrapper.GChart(self.type,args,**kwargs)
        imgkwargs = {}
        for n in self.nodelist:
            rend = n.render(context)           
            if type(rend) == type([]):
                if rend[0] == 'img':
                    for k,v in map(lambda x: x.split('='), rend[1:]):
                        imgkwargs[k] = v
                    continue
                if rend[0] == 'axes':
                    getattr(getattr(chart, rend[0]), rend[1])(*rend[2:])
                else:
                    getattr(chart, rend[0])(*rend[1:])
        return chart.img(**imgkwargs)

def make_chart(parser, token):
    nodelist = parser.parse(('endchart',))
    parser.delete_first_token()
    tokens = token.contents.split()
    return ChartNode(tokens,nodelist)
register.tag('chart', make_chart)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 8 months ago

Comments

everythingability (on October 24, 2008):

The Django app with demo templates seems to be missing...

#

Please login first before commenting.