Google Charts Templatetags (Python)

 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. Google Charts Templatetags (HTML) by justquick 5 years, 12 months ago
  2. Google Closure support in django-compress by fabrice.bonny 4 years, 5 months ago
  3. Link Models to Google Calendar by JosefK 6 years, 9 months ago
  4. Generate QR Code image for a string by johnnoone 4 years, 11 months ago
  5. Unfuddle-style post-commit emails - tied to a specific Django project. by jsandell 5 years, 7 months ago

Comments

everythingability (on October 24, 2008):

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

#

(Forgotten your password?)