# -*- coding: utf-8 -*- # This file is part of pygal # # A python svg graph plotting library # Copyright © 2012-2015 Kozea # # This library is free software: you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free # Software Foundation, either version 3 of the License, or (at your option) any # later version. # # This library 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 Lesser General Public License for more # details. # # You should have received a copy of the GNU Lesser General Public License # along with pygal. If not, see . from flask import Flask, render_template, Response, request import pygal from pygal.config import Config from pygal.util import cut from pygal.etree import etree from pygal.style import styles, parametric_styles from base64 import ( urlsafe_b64encode as b64encode, urlsafe_b64decode as b64decode) import string import random import pickle def get(type): from importlib import import_module module = '.'.join(type.split('.')[:-1]) name = type.split('.')[-1] return getattr(import_module(module), name) def random_label(): chars = string.ascii_letters + string.digits + u' àéèçêâäëï' return ''.join( [random.choice(chars) for i in range(random.randrange(4, 30))]) def random_value(min=0, max=15): return random.randrange(min, max, 1) def create_app(): """Creates the pygal test web app""" app = Flask(__name__) @app.before_request def before_request(): if request.args.get('etree'): etree.to_etree() elif request.args.get('lxml'): etree.to_lxml() def _random(data, order): max = 10 ** order min = 10 ** random.randrange(0, order) series = [] for i in range(random.randrange(1, 10)): values = [( random_value((-max, min)[random.randrange(0, 2)], max), random_value((-max, min)[random.randrange(0, 2)], max) ) for i in range(data)] series.append((random_label(), values)) return series def _random_series(type, data, order): max = 10 ** order min = 10 ** random.randrange(0, order) series = [] for i in range(random.randrange(1, 10)): if type == 'Pie': values = random_value(min, max) elif type == 'XY': values = [( random_value((-max, min)[random.randrange(0, 2)], max), random_value((-max, min)[random.randrange(0, 2)], max)) for i in range(data)] else: values = [random_value((-max, min)[random.randrange(1, 2)], max) for i in range(data)] series.append((random_label(), values)) return series from .tests import get_test_routes links = get_test_routes(app) @app.route("/") def index(): return render_template( 'index.jinja2', styles=styles, parametric_styles=parametric_styles, parametric_colors=( '#ff5995', '#b6e354', '#feed6c', '#8cedff', '#9e6ffe'), links=links, charts_name=pygal.CHARTS_NAMES) @app.route("/svg///") def svg(type, series, config): graph = get(type)( pickle.loads(b64decode(str(config)))) for title, values in pickle.loads(b64decode(str(series))): graph.add(title, values) return graph.render_response() @app.route("/table///") def table(type, series, config): graph = get(type)(pickle.loads(b64decode(str(config)))) for title, values in pickle.loads(b64decode(str(series))): graph.add(title, values) return graph.render_table() @app.route("/sparkline/