From 698548529a570b3cab576ae33b5b514d2db7ddb0 Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Tue, 10 Sep 2013 02:03:23 +0200 Subject: First pygal/chart stuff --- app/views/__init__.py | 7 ++++--- app/views/svg.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 app/views/svg.py (limited to 'app') diff --git a/app/views/__init__.py b/app/views/__init__.py index d64b945..6405d3e 100644 --- a/app/views/__init__.py +++ b/app/views/__init__.py @@ -13,8 +13,8 @@ def handle_mobile(): flask.g.is_mobile = any((x in ua) for x in mobile_checks) @app.template_filter("static_url") -def static_url(s): - return url_for("static", filename=s) +def static_url(s, **kwargs): + return url_for("static", filename=s, **kwargs) @app.template_filter("eur") def eur(s): @@ -31,10 +31,11 @@ def format_date(s, format="%Y/%m"): def page_not_found (error): return render_template("404.jinja", page = request.path), 404 -from . import categories, consts, expenses +from . import categories, consts, expenses, svg app.register_blueprint(expenses.mod) app.register_blueprint(consts.mod, url_prefix="/const") app.register_blueprint(categories.mod, url_prefix="/cat") +app.register_blueprint(svg.mod, url_prefix="/svg") app.add_url_rule("/", endpoint = "index", build_only = True) diff --git a/app/views/svg.py b/app/views/svg.py new file mode 100644 index 0000000..06666b3 --- /dev/null +++ b/app/views/svg.py @@ -0,0 +1,43 @@ +from ..flask_extend import Blueprint + +from .expenses import calc_month_exp +from . import static_url +from ..model import Category + +from pygal.style import DefaultStyle as Style +from pygal import Pie as _Pie + +mod = Blueprint('svg', __name__) + +Style.background = Style.plot_background = "rgba(255,0,0,0)" + +def Pie(*args, **kwargs): + # does not work as functools.partial, because static_url would be called without request + return _Pie(*args, + style=Style, + js = [static_url('pygal/pygal.js', _external = True)], + legend_font_size = 24, + tooltip_font_size = 24, + value_font_size = 24, + margin = 0, + legend_at_bottom = True, + pretty_print = True, + **kwargs) + +@mod.route("/month//.svg") +def month(year, month): + exp = calc_month_exp(year, month) + pie = Pie() + + expenses = {} + for c in exp.catexps: + expenses[c.cat.name] = float(c.expense) + + for c in Category.query.order_by(Category.name).all(): + pie.add(c.name, expenses.get(c.name, 0.0)) + + if not expenses: + pie.raw_series = [] + pie.add("Empty", 0.1) + + return pie.render_response() -- cgit v1.2.3-54-g00ecf