Source

mustachebox / mustachebox / backends / example_backend.py

Yohann Gabory b83bfcd 















Yohann Gabory ce2c9ab 




Yohann Gabory b04225b 
Yohann Gabory ce2c9ab 











Yohann Gabory b04225b 
Yohann Gabory ce2c9ab 











Yohann Gabory 01dd506 











Yohann Gabory 8676f1c 
Yohann Gabory f84782f 
Yohann Gabory 8676f1c 
Yohann Gabory 01dd506 
Yohann Gabory f84782f 
Yohann Gabory ce2c9ab 
Yohann Gabory f84782f 




Yohann Gabory ce2c9ab 

Yohann Gabory 01dd506 












Yohann Gabory ce2c9ab 
Yohann Gabory f84782f 




Yohann Gabory ce2c9ab 

Yohann Gabory 01dd506 












Yohann Gabory ce2c9ab 
Yohann Gabory f84782f 




Yohann Gabory ce2c9ab 
Yohann Gabory 01779e0 
Yohann Gabory ce2c9ab 
Yohann Gabory 01dd506 




Yohann Gabory 77fb0c9 
Yohann Gabory 01dd506 






Yohann Gabory ce2c9ab 

Yohann Gabory f84782f 


Yohann Gabory ce2c9ab 





Yohann Gabory f84782f 

Yohann Gabory ce2c9ab 
Yohann Gabory 01779e0 
Yohann Gabory ce2c9ab 
Yohann Gabory 01dd506 












Yohann Gabory ce2c9ab 






Yohann Gabory b04225b 

Yohann Gabory ce2c9ab 
Yohann Gabory 01779e0 








Yohann Gabory e5104eb 

Yohann Gabory 01779e0 

Yohann Gabory e5104eb 

Yohann Gabory 01779e0 


















Yohann Gabory d9887a6 
Yohann Gabory 01779e0 
# -*- coding: utf-8 -*-
# Copyright (c) 2013 Yohann Gabory <yohann@gabory.fr>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import datetime
import time
import random
from mustachebox.backends import BaseBackend


def time_serie(**kwargs):
    """
    Render a time serie
    """
    resp = {2004: ['2004'],
            2005: ['2005'],
            2006: ['2006'],
            2007: ['2007']}

    for i in range(2):
        for k, v in resp.iteritems():
            v.append(int((random.random() * random.random()) * 10000))
        resp['label'] = ['year', 'sales', 'expenses']
    return resp


class Backend(BaseBackend):
    """
    A backend get data formated as mustache need them.
    - data : contain the data to be printed as graph
    - name : the graph name
    - template_name: the template to use for rendering
    """

    def area(self, **kwargs):
        """
        Render a graph area using google chart.

        The data are formated as follow ::

            {
                2004: ['2004', 7160, 546],
                2005: ['2005', 5654, 5435],
                2006: ['2006', 7656, 6545],
                2007: ['2007', 5435, 6545],
                'label': ['year', 'sales', 'expenses']
            }

        the keyword argument, elem is used if present.

        """

        self.template = 'area'
        response = {}
        if "elem" in kwargs:
            response['elem'] = kwargs['elem']
        response['data'] = time_serie(**kwargs)
        return response

    def bar_chart(self, **kwargs):
        """
        Render a barchart graph using google chart.

        The data are formated as follow ::

            {
                2004: ['2004', 7160, 546],
                2005: ['2005', 5654, 5435],
                2006: ['2006', 7656, 6545],
                2007: ['2007', 5435, 6545],
                'label': ['year', 'sales', 'expenses']
            }
        """
        self.template = "barchart"
        response = {}
        if "elem" in kwargs:
            response['elem'] = kwargs['elem']
        response['data'] = time_serie(**kwargs)
        return response

    def column_chart(self, **kwargs):
        """
        Render a barchart graph using google chart.

        The data are formated as follow ::

            {
                2004: ['2004', 7160, 546],
                2005: ['2005', 5654, 5435],
                2006: ['2006', 7656, 6545],
                2007: ['2007', 5435, 6545],
                'label': ['year', 'sales', 'expenses']
            }
        """
        self.template = 'columnchart'
        response = {}
        if "elem" in kwargs:
            response['elem'] = kwargs['elem']
        response['data'] = time_serie(**kwargs)
        return response

    def pie_chart(self, **kwargs):
        """
        Define a generic pie chart using google chart

        The data are formated as follow::

            {'label': ['name', 'count'],
             'records': [
                             ['eat': 12],
                             ['read': 5],
                             ['work': 23],
                             ['sleep': 4],
                             ['wathc TV': 10]
                           ]
            }
        """
        self.template = "pie_chart"
        response = {}
        if "elem" in kwargs:
            response['elem'] = kwargs['elem']
        label = ['name', 'count']
        activities = []
        words = ['eat', 'read', 'work', 'sleep', 'watch TV']
        for elem in words:
            activities.append([elem, int(random.random() * 10)])

        response['data'] = {'label': label, 'records': activities}
        return response

    def line_chart(self, **kwargs):
        """
        Define a generic line chart using D3js

        Data are formated as follow ::

            [
              {'date': 1107151200000,
               'value': 289
              },
              {'date': 1109743200000,
               'value': 766
              }
               <...>
            ]
        """
        response = []
        self.template = "line_chart"
        date = datetime.datetime(2005, 1, 1, 0, 0, 0)
        for i in range(1500):
            date += datetime.timedelta(days=2)
            response.append({
                'date': time.mktime(date.timetuple()) * 1000,
                'value': (i * 10) + int(random.random() * 1000)})
        return response

    def multiserie_linechart(self, **kwargs):
        """
        Define a multi-line graph using D3js

        data are formated as follow ::

            [
              {'date': 1109743200000,
              'value': 453,
              'serie': "serie1"
              },
              {'date': 1107151200000,
               'value': 435,
               'serie': "serie2",
              },
              ...
            ]
        """
        self.template = "multiline_chart"
        response = []
        for a in range(3):  # we render a 3 series chart
            date = datetime.datetime(2005, 1, 1, 0, 0, 0)
            for i in range(200):
                if a % 3 == 0:
                    mult = -10
                elif a % 2 == 0:
                    mult = 1
                else:
                    mult = 10
                date += datetime.timedelta(days=2)
                response.append({
                    'date': time.mktime(date.timetuple()) * 1000,
                    'value': (i * mult) + int(random.random() * 1000),
                    'serie': a})
        return response