Commit ea92f705 authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Create the pandas controller to explore graph rendering with the pandas library.

parent 0b2a38ae
# -*- coding: utf-8 -*-
""" Controllers for building graphs using pandas library
"""
import matplotlib
from datetime import datetime
from pandas import DataFrame
from plugin_dbui import Selector
from StringIO import StringIO
def savefig(ax, format):
"""Save the figure as a string.
Args:
ax (matplotlib.AxesSubplot): axes subplot
format (str): possible values are pdf, png and svg.
Return:
str:
"""
fig = ax.get_figure()
fi = StringIO()
fig.savefig(fi, format=format)
data = fi.getvalue()
fi.close()
fig.clear()
matplotlib.pyplot.close(fig)
return data
def index():
"""Prototype to generate the number of publication per month.
"""
fields = ('cumulative','id_graphs', 'year_start','year_end')
selector = Selector(virtdb.graph_selector, exclude_fields=fields)
# graph_cfg = db.graphs[selector.id_graphs]
# alias
publications = db.publications
year_end = selector.year_end
year_start = selector.year_start
# matplotlib backend
matplotlib.use("Agg")
# query directive to count publications including
# foreign key constraints and user requirements
# related to team, project, authors roles and year
query = selector.query(publications)
if year_start and not year_end:
query &= publications.submitted[0:4] == year_start
elif year_start and year_end:
q_start = publications.submitted[0:4] >= year_start
q_end = publications.submitted[0:4] <= year_end
query &= ((q_start) & (q_end))
# count the number of publications
# they are grouped per year and and per month
group_by = [publications.submitted[:4], publications.submitted[5:7]]
count = publications.id.count()
rows = db(query).select(publications.submitted,
count,
groupby=group_by,
orderby=group_by)
# build the dataframe
data = []
for row in rows:
# protection -- ignore publication with invalid submitted date
submitted = row.publications.submitted
if len(submitted) == 4:
continue
dt = datetime.strptime(submitted[:7], "%Y-%m")
month = dt.strftime("%b")
data.append([month, row[count]])
df = DataFrame(data, columns=['month', 'count'])
# move to a DataSerie in which values are ordered by month
df = df.set_index('month')
# cumulative serie
df = df.cumsum()
# generate the plot
ax = df.plot()
# delegate the rendering to the view
return dict(data=savefig(ax, "svg"), title="foo")
......@@ -207,11 +207,15 @@ metricLeaf = PanelWithUrlSelector(virtdb.metric_selector,
baseUrl=URL('metrics', 'index'),
extField='format')
pandasLeaf = PanelWithUrlSelector(virtdb.graph_selector,
baseUrl=URL('pandas', 'index'))
reportNode = Node(T('Reports'))
reportNode.add_child(T('dashboard'), dashBoardLeaf)
reportNode.add_child(T('lists'), listLeaf)
reportNode.add_child(T('metrics'), metricLeaf)
reportNode.add_child(T('graphs'), graphLeaf)
reportNode.add_child('pandas', pandasLeaf)
#-------------------------------------------------------------------------------
#
......
{{
#--------------------------------------------------------------------------
#
# The python controller return image as a string:
# - data (string)
# - title (string)
#
#--------------------------------------------------------------------------
import urllib
#
# The title
#
response.write(H2(title, _class="dbui-h2 dbui-small-cap"))
# encode special charactier bo be used in the image URI
data = urllib.quote(data)
}}
<img src="data:image/svg+xml;utf8,{{=data}}" height=400></img>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment