Commit 2bd6cbcb authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Update module dataframes to add the function to_extjs_gridcolumns.

parent c09f5bbd
......@@ -15,7 +15,8 @@ from dataframes import (db2df,
get_people_per_year,
normalize_history_data,
query_history,
Timer)
Timer,
to_extjs_gridcolumns)
from event import Event
......
......@@ -10,6 +10,7 @@ import time
from datetime import date
from gluon import current
from gluon.storage import Storage
from plugin_dbui import get_id
......@@ -1282,3 +1283,69 @@ class Timer(object):
self.secs = self.end - self.start
self.msecs = self.secs * 1000 # millisecs
print '\n%s\n\telapsed time: %f ms' % (self.msg, self.msecs)
def to_extjs_gridcolumns(df):
"""Introspect the DataFrame in order to derive configuration for
``Ext.grid.column.Column`` which are used by an ``Ext.grid.Panel``.
Note:
* The first columns shows the row number.
* The type of the grid column is derived from the DataFrame.dtype
as well as from the name of the DataFrame column.
* The default xtype ``gridcolumn`` is used for string and for
dtype == object which are not resolved. Later, the user can tune
the column type.
Args:
df (pandas.DataFrame):
Returns:
list:
configuration are stored in a dictionary.
"""
li = [dict(xtype="rownumberer")]
cfgcol = Storage(dataIndex="",
flex=1,
hidden=False,
text="",
xtype="gridcolumn")
for colname, coltype in df.dtypes.sort_index().iteritems():
cfg = Storage(cfgcol)
cfg.dataIndex = cfg.text = colname
if colname.startswith("id"):
cfg.flex = 0.5
cfg.format = "0"
cfg.hidden = True
cfg.xtype = "numbercolumn"
elif coltype in (np.int8, np.int16, np.int32, np.int64):
cfg.format = "0"
cfg.xtype = "numbercolumn"
elif coltype in (np.float16, np.float32, np.float64):
cfg.format = "0.00"
cfg.summaryType = "sum"
cfg.xtype = "numbercolumn"
elif coltype == np.bool_ or colname.startswith("is"):
cfg.falseText = "False"
cfg.trueText = "True"
cfg.xtype = "booleancolumn"
elif coltype == np.datetime64 or colname in ("date",
"end_date",
"period_end",
"period_start",
"start_date"):
cfg.format = "Y-m-d"
cfg.xtype = "datecolumn"
li.append(cfg)
return li
\ No newline at end of file
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